首页 > 编程语言 >php将html生成为pdf文件的几个代码包比较

php将html生成为pdf文件的几个代码包比较

时间:2022-11-19 11:25:40浏览次数:70  
标签:mpdf filename dompdf html content pdf php

最近需要开发一个生成pdf文件的程序 ,试用了下几个生成pdf包的效果

<div style="width: 80%;padding-right: 3rem;padding-left:3rem;margin-right:3rem;margin-left:3rem;">
        <table border="1" style="width: 100%; margin-bottom: 1rem; color: #212529;vertical-align: top;border-color: #dee2e6;border-collapse: collapse;border: 1px solid black;">
            <thead>
                <tr style="height: 4rem;">
                    <th>#</th>
                    <th>First</th>
                    <th>Last</th>
                    <th>Handle</th>
                </tr>
            </thead>
            <tbody>
                <tr style="height: 3rem;">
                    <th scope="row"><input type="checkbox"></th>
                    <td>Mark</td>
                    <td>Otto</td>
                    <td>@mdo</td>
                </tr>
                <tr style="height: 3rem;">
                    <th scope="row"><input type="checkbox"></th>
                    <td>Jacob</td>
                    <td>Thornton</td>
                    <td>@fat</td>
                </tr>
                <tr style="height: 3rem;">
                    <th><input type="checkbox"></th>
                    <td colspan="2">Larry the Bird</td>
                    <td>@twitter</td>
                </tr>
            </tbody>
        </table>
    </div>

 1 tcpdf

composer require tecnickcom/tcpdf

require_once './tcpdf/vendor/autoload.php';
$content="..."; 
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 
$pdf->SetFont('msyh', '', 8);
$pdf->setPrintHeader(false); $pdf->AddPage(); 
$pdf->writeHTML($content, true, false, false, false, '');
$pdf->Output('example.pdf', 'I');

  

中文解决方案:

1. 下载要设置的字体,如名为simfang.ttf,放在./vendor/tecnickcom/tcpdf/tools目录中

2.在tools目录中按住shift,点击鼠标右键,点击“在此处打开命令行窗口”,执行语句

php ./tcpdf_addfont.php -b -t simfang -i simfang.ttf

 便会在./vendor/tecnickcom/tcpdf/fonts/文件夹下面会生成simfang.ctg.z , simfang.php和simfang.z几个文件

2 mpdf

composer require mpdf/mpdf

require_once './mpdf/vendor/autoload.php';
$content="...";
$mpdf = new \Mpdf\Mpdf(['UTF-8', 'A4', '', '', 32, 25, 27, 25, 16, 13]);
$mpdf->writeHTML($content);
$mpdf->Output(); //输出到浏览器
//$mpdf->Output('中文.pdf', true); //下载

 

 

中文解决方案:

vendor/mpdf/mpdf/src/Config/ConfigVariables.php

'autoScriptToLang' => true,
'autoLangToFont' => true,

 

3 dompdf

composer require dompdf/dompdf

require_once './dompdf/vendor/autoload.php';
$content="...";
$dompdf = new \Dompdf\Dompdf();
$dompdf->loadHtml($content);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->output();
// $dompdf->stream(); //输出到浏览器
$dompdf->stream('my.pdf', ['Attachment' => 0]);  //下载

  

中文解决方案:

下载Dompdf的工具包:https://github.com/dompdf/utils

 下载字体文件

php load_font.php simsun path/to/simsun.ttf

 4 wkhtmltopdf

wkhtmltopdf可以直接把任何一个可以在浏览器中浏览的网页直接转换成一个pdf,首先说明一下它不是一个php 类,而是一个把html页面转换成pdf的一个软件(需要安装在服务器上),但是它并不是一个简单的桌面软件,而且它直接cmd批处理的,使用php中的 shell_exec()函数就可以调用它。 使用版本  wkhtmltox-0.12.4     用时css放在页面上,先生成html文件,再把HTML文件转成pdf文件
$filename = 'pdf/' . $stage_id;
file_put_contents("{$filename}.html", $content);
$res = exec("wkhtmltopdf {$filename}.html {$filename}.pdf", $output);
if (file_exists("{$filename}.pdf")) {
    $file_size = filesize("{$filename}.pdf");
    Header("Content-type:application/octet-stream");
    Header("Accept-Ranges:bytes");
    Header("Accept-Length:" . $file_size);
    Header("Content-Disposition:attachment;filename=" . $title . '.pdf');
    echo file_get_contents("{$filename}.pdf");
}
exit;

 解决分页问题 

wkhtmltopdf 很好用,但也有些不尽人意。就是当一个html页面很长我需要在指定的地方分页那怎么办呢? wkhtmltopdf 开发者在开发的时候并不是没有考虑到这一点,  wkhtmltopdf 有个很好的方法,就是在那个div的样式后添加一个:page-break-inside:avoid;就ok了。    中文字体问题 把Windows里面的字体复制一份,字体文件后缀名改为TTC, 上传到服务器/usr/share/fonts/里  

 

标签:mpdf,filename,dompdf,html,content,pdf,php
From: https://www.cnblogs.com/caroline2016/p/16905676.html

相关文章