PHP导出生成PDF文件开源组件:mPDF使用详情

最近在使用php要实现一个把旅游线路导出成pdf文件下载,在全网搜索了一遍有几个常用的开源组件,在PHP中生成PDF文件,比如FPDFTCPDFmPDF等。在对比了一圈后就

mPDF开源地址:

https://github.com/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文档说明

http://mpdf.github.io/

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');

效果:

相关推荐
Sally璐璐7 小时前
OpenVPN:深度解析开源 VPN 解决方案
运维·开源
余大侠在劈柴8 小时前
pdf.js 开发指南:在 Web 项目中集成 PDF 预览功能
前端·javascript·学习·pdf
解牛之术9 小时前
Android展示加载PDF
android·pdf
MSTIFIY9 小时前
【PyMuPDF】PDF图片处理过程内存优化分析
图像处理·pdf·内存优化
翟天保Steven9 小时前
将CSDN文章导出为PDF
pdf
无懈可击10 小时前
FormCreate低代码表单设计器 v3.3 版本发布,功能大更新!
vue.js·低代码·开源
whysqwhw12 小时前
配置私有 AI 代码文档系统
开源
云妙算12 小时前
被1600万家庭信赖的智能音箱Sonos,用什么方式让AWS云成本打3折?
后端·开源·aws
Eiceblue13 小时前
用Python向PDF添加文本:精确插入文本到PDF文档
开发语言·python·pdf
Albert_Lsk13 小时前
【2025/07/14】GitHub 今日热门项目
人工智能·开源·github·开源协议