PHP生成PDF-mPDF的使用

PHP生成PDF-mPDF的使用

一、说明

mPDF是一个PHP库,它是从UTF-8编码的HTML生成PDF文件。它的使用比较简单,性能我简单测试了下还是较快的,毕竟PDF这种1000页已经算多的了。

二、快速使用

安装

$ composer require mpdf/mpdf

composer安装信息(有下面的这些依赖)

Using version ^8.0 for mpdf/mpdf
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 5 installs, 0 updates, 0 removals
  - Installing myclabs/deep-copy (1.9.5): Downloading (100%)
  - Installing paragonie/random_compat (v9.99.99): Downloading (100%)
  - Installing setasign/fpdi (v2.3.3): Downloading (100%)
  - Installing psr/log (1.1.3): Downloading (100%)
  - Installing mpdf/mpdf (v8.0.5): Downloading (100%)
paragonie/random_compat suggests installing ext-libsodium (Provides a modern crypto API that can be used to generate random bytes.)
setasign/fpdi suggests installing setasign/fpdf (FPDI will extend this class but as it is also possible to use TCPDF or tFPDF as an alternative. There's no fixed dependency configured.)
Writing lock file
Generating autoload files

简单使用

WriteHTML('

Hello world!

'); $mpdf->Output();//直接输出到浏览器 //$mpdf->Output('testMpdf.pdf');//保存为文件

三、性能测试

简单测试结果

生成100页的pdf耗时:2.21秒

生成1000页的pdf耗时:21.03秒

四、高级应用

mPDF还支持很多html特性,像表格,标签,字体,css等等的配置详细的可以参考官方文档

文档地址

Github地址

你可能感兴趣的:(PHP)