最近在使用php要实现一个把旅游线路导出成pdf文件下载,在全网搜索了一遍有几个常用的开源组件,在PHP中生成PDF文件,比如FPDF
、TCPDF
、mPDF
等。在对比了一圈后就
mPDF开源地址:
mPDF版本说明
- PHP >=5.6 <7.3.0 is supported for mPDF >= 7.0
- PHP 7.3 is supported since mPDF v7.1.7
- PHP 7.4 is supported since mPDF v8.0.4
- PHP 8.0 is supported since mPDF v8.0.10
- PHP 8.1 is supported as of mPDF v8.0.13
- PHP 8.2 is supported as of mPDF v8.1.3
- PHP 8.3 is supported as of mPDF v8.2.1
mPDF文档说明
mPDF安装
bash
composer require mpdf/mpdf
mPDF使用方法
php
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello world!</h1>');
$mpdf->Output();
ThinkPHP项目中使用
php
//丽途网 www.litour.cn 入境旅游管理系统
$stylesheet = '';
$mpdf = new Mpdf([
'tempDir' => WEB_ROOT . 'pdf',
'mode' => 'utf-8',
// 'orientation' => 'L',
'format' => 'A4',
// 'margin_left' => 10,
// 'margin_right' => 10,
// 'margin_top' => 10,
// 'margin_bottom' => 10,
// 'margin_header' => 10,
// 'margin_footer' => 10,
]);
$mpdf->SetDisplayMode('fullpage');
$stylesheet .= file_get_contents(WEB_ROOT.'/css/pdf.css');
$url = cmf_get_domain().'/enquiry/'.$name;
$html = file_get_contents($url);
$footer = 'xxxxx';//设置每一页的页脚,支持html标签
$mpdf->SetHTMLFooter($footer);
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);
$filename = $name.'.pdf';
$path = WEB_ROOT . '/pdf/'.$filename;
$mpdf->Output($path,'f');
return download($path, 'my_'.$name.'.pdf');
效果: