HTML 转 PDF 或 图片

knp-snappy 安装
1
composer require knplabs/knp-snappy
WINDOWS 系统安装扩展包 wkhtmltopdf-windows
1
2
3
4
composer require wemersonjanuario/wkhtmltopdf-windows

#二进制文件路径(32位和64位)
vendor\wemersonjanuario\wkhtmltopdf-windows\bin
LINUX 系统扩展包
1
2
3
4
5
6
7
#i386 的二进制文件
composer require h4cc/wkhtmltopdf-i386
composer require h4cc/wkhtmltoimage-i386

#amd64
composer require h4cc/wkhtmltopdf-amd64
composer require h4cc/wkhtmltoimage-amd64
实现代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
use Knp\Snappy\Pdf;
use Knp\Snappy\Image;

require 'vendor/autoload.php';

$baseDir = __DIR__;
$pdfBinaryFile = $baseDir . '/vendor/wemersonjanuario/wkhtmltopdf-windows/bin/64bit/wkhtmltopdf.exe';
$imageBinaryFile = $baseDir . '/vendor/wemersonjanuario/wkhtmltopdf-windows/bin/64bit/wkhtmltoimage.exe';

if (strpos(PHP_OS, 'Linux') !== false) {
$h4ccPath = $baseDir . '/vendor/h4cc/';
$systemBit = exec('getconf LONG_BIT');
if ($systemBit == '32') {
$pdfBinaryFile = $h4ccPath . 'wkhtmltopdf-i386/bin/wkhtmltopdf-i386';
$imageBinaryFile = $h4ccPath . 'wkhtmltoimage-i386/bin/wkhtmltoimage-i386';
} else {
$pdfBinaryFile = $h4ccPath . 'wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64';
$imageBinaryFile = $h4ccPath . 'wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64';
}
}

$html = <<<HTML
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
</head>
<body>
<p style="font-family: 'Microsoft YaHei'">Hello World, 你好 世界!</p>
</body>
</html>
HTML;

// HTML 转 PDF
$pdfSnappy = new Pdf($pdfBinaryFile);
$pdfSnappy->generateFromHtml($html, $baseDir . '/test.pdf', [], true);

// HTML 转 图片
$imageSnappy = new Image($imageBinaryFile);
$imageSnappy->generateFromHtml($html, $baseDir . '/test.png', [
'width' => 100,
'height' => 100,
'quality' => 100
], true);

// 生成海报(海报背景 + 二维码 + LOGO + 文字描述)
$backgroundImage = $baseDir . '/bg.jpg';
$qrcodeImage = $baseDir . '/qrcode.png';
$logoImage = $baseDir . '/logo.png';
$title = "海报标题AAAABBBBCCCCDDDD";
$outputImage = $baseDir . '/poster.png';

list ($width, $height) = getimagesize($backgroundImage);
$snappy = new Image($imageBinaryFile);
$snappy->generateFromHtml(generateHtml($backgroundImage, $qrcodeImage, $logoImage, $title), $outputImage, [
'width' => $width,
'height' => $height,
'quality' => 100
], true);

/**
* 生成HTML
*
* @param string $backgroundImage
* @param string $qrcodeImage
* @param string $logoImage
* @param string $title
*
* @return string
*/
function generateHtml($backgroundImage, $qrcodeImage, $logoImage, $title)
{
$html = <<<HTML
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title></title>
<style>
body, html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-family: "Microsoft YaHei"
}
.container {
position: relative;
display: flex;
width: 100%;
height: 100%;
}
.img-bg {
position: absolute;
top: 0;
left: 0;
width: 100%;
}
.content {
position: relative;
z-index: 999;
left: 25%;
top: 37.5%;
width: 50%;
height: 25%;
border-radius: 7px;
}
.img-qrcode {
width: 100%;
height: 100%;
}
.img-logo {
position: absolute;
top: 40%;
left: 40%;
width: 20%;
height: 20%;
}
.title {
width: 90%;
margin: 2px auto;
overflow: hidden;
font-size: 20px;
display: -webkit-box;
-webkit-line-clamp: 2;
text-overflow: ellipsis;
-webkit-box-orient: vertical;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<img src="{$backgroundImage}" class="img-bg">
<div class="content">
<img src="{$qrcodeImage}" class="img-qrcode">
<img src="{$logoImage}" class="img-logo">
<div class="title">{$title}</div>
</div>
</div>
</body>
</html>
HTML;

return $html;
}

Linux 字体安装(生成PDF或图片时乱码或方块)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1. /usr/share/fonts 新建目录 windows
2. 上传字体文件至 windows 目录下
3. 建立字体索引信息
cd /usr/share/fonts/windows
mkfontscale
mkfontdir
4. 更新字体缓存
fc-cache

#查看系统中的字体
fc-list

#查看系统中的中文字体
fc-list :lang=zh

Mac 生成图片失败

安装

1
2
3
4
5
6
7
8
# 下载地址
https://wkhtmltopdf.org/downloads.html
# 终端
wkhtmltopdf --version
wkhtmltoimage --version
# 查看路径
which wkhtmltopdf
which wkhtmltoimage

运行

1
2
3
4
5
6
7
8
9
10
11
<?php
// 生成失败原因:wkhtmltoimg 0.12.6 中默认禁用本地文件访问,需配置 enable-local-file-access 为 true

use Knp\Snappy\Image;

$options = [
'quality' => 100,
'enable-local-file-access' => true
];
$imageSnappy = new Image('/usr/local/bin/wkhtmltoimage');
$imageSnappy->generateFromHtml($html, $outputImage, $options, true);
0%