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

效果:

相关推荐
JaguarJack11 分钟前
使用 PHP 和 WebSocket 构建实时聊天应用 完整指南
后端·php
子榆.31 分钟前
Flutter 与开源鸿蒙(OpenHarmony)性能调优实战:从启动速度到帧率优化的全链路指南
flutter·开源·harmonyos
DechinPhy36 分钟前
使用Python免费合并PDF文件
开发语言·数据库·python·mysql·pdf
LCG米39 分钟前
基于LoRa的远距离低功耗农业传感器网络设计与实现(SX1278+STM32L071)
网络·stm32·php
子榆.42 分钟前
Flutter 与开源鸿蒙(OpenHarmony)安全加固实战:防逆向、防调试、数据加密全攻略
flutter·开源·harmonyos
低调电报1 小时前
我的第一个开源项目:鸿蒙分布式“口袋健身”教练
分布式·开源·harmonyos
子榆.1 小时前
Flutter 与开源鸿蒙(OpenHarmony)深度集成实战(二):实现跨设备分布式数据同步
flutter·开源·harmonyos
yesyesyoucan1 小时前
跨格式数据转换技术解析:文本/表格文件的无损重构方法与开源工具实践
重构·开源
ai_xiaogui1 小时前
Debian系统PVE虚拟机安装详解:ISO镜像上传+硬件配置+图形化安装指南
运维·debian·php·panelai兼容测试·图形化安装指南·iso镜像上传配置·debian pve虚拟机安装
子榆.2 小时前
Flutter 与开源鸿蒙(OpenHarmony)上架华为应用市场全流程指南:从合规检测到审核通过
flutter·华为·开源