php-pdf


php-pdf使用

版本
1
"dompdf/dompdf": "^1.2"
代码
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
$ttfPath = 'xxx/xxx/ttf';//字体文件目录
$config = new Options();
$config->setFontDir($ttfPath);//设置字体文件,防止中文乱码
$config->setDefaultFont('simsun');//默认字体

$html = '<html lang="">'; //写法类似html
$html .= '<head><title></title>';
$html .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>';
$html .= '<style>';
$html .= '* {line-height:150%}
table {width:100%; max-width:600px;border-collapse: collapse;}
table, th, td {
border: 1px solid black;overflow:hidden;max-height:50px;line-height:25px;font-size: 16px} td {
padding-left:15px;}';
$html .= '</style>';
$html .= '</head>';
$html .= '<body>';
$html .= '<div style="text-align:center;font-size: 30px">';
$html .= '内容';
$html .= '</div>';
$html .= '</body>';
$html .= '</html>';

$dompdf = new Dompdf($config);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4');

$dompdf->render();

$res = $dompdf->output();
$path = 'xxx.pdf';//附件临时存放地址
file_put_contents($path, $res);//下载文件到本地临时存放