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

效果:

相关推荐
CoderJia程序员甲6 小时前
GitHub 热榜项目 - 日榜(2025-09-06)
ai·开源·github·ai编程·github热榜
草梅友仁9 小时前
草梅 Auth 1.6.0 发布密码强度组件 Twilio 短信支持 | 2025 年第 36 周草梅周报
前端·开源·github
CoderJia程序员甲10 小时前
GitHub 热榜项目 - 日榜(2025-09-03)
ai·开源·github·开源项目·github热榜
老马啸西风11 小时前
sensitive-word 敏感词性能提升14倍优化全过程 v0.28.0
安全·开源·nlp·word·敏感词·sensitive-word
janthinasnail13 小时前
使用Docker安装Stirling-PDF(PDF工具)
docker·pdf
前端世界14 小时前
Python 正则表达式实战:用 Match 对象轻松解析拼接数据流
python·正则表达式·php
云水木石15 小时前
开源鸿蒙+龙芯CPU,能擦出怎样的火花?
华为·开源·harmonyos
智慧地球(AI·Earth)15 小时前
开源 + 免费!谷歌推出 Gemini CLI,Claude Code 的强劲对手
人工智能·语言模型·开源
在狂风暴雨中奔跑15 小时前
告别繁琐,使用WorkManager解决后台工作的难题
开源
特立独行的猫a16 小时前
开源OpenHarmony润开鸿HH-SCDAYU800A开发板开箱体验
开源·harmonyos·openharmony·hh-scdayu800a