本文目录导读:

我来为你详细讲解PHP生成和导出PDF文件的几种主流方法。
使用TCPDF库(推荐)
安装
composer require tecnickcom/tcpdf
基本使用示例
<?php
require_once __DIR__ . '/vendor/autoload.php';
use TCPDF;
// 创建PDF实例
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// 设置文档信息
$pdf->SetCreator('Your App');
$pdf->SetAuthor('作者名称');
$pdf->SetTitle('文档标题');
$pdf->SetSubject('文档主题');
// 设置页眉页脚
$pdf->setHeaderFont(['dejavusans', '', 10]);
$pdf->setFooterFont(['dejavusans', '', 8]);
$pdf->SetHeaderMargin(10);
$pdf->SetFooterMargin(10);
// 设置自动分页
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// 添加页面
$pdf->AddPage();
// 设置字体
$pdf->SetFont('dejavusans', '', 14);
$pdf->Cell(0, 10, 'Hello World!', 0, 1, 'C');
// 输出PDF
$pdf->Output('example.pdf', 'I'); // I: 浏览器显示, D: 下载, F: 保存到服务器
使用FPDF库(轻量级)
安装
composer require setasign/fpdf
示例代码
<?php
require_once __DIR__ . '/vendor/autoload.php';
use FPDF\FPDF;
// 创建PDF实例
$pdf = new FPDF();
$pdf->AddPage();
// 设置字体
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(40, 10, 'Hello World!');
// 添加表格
$pdf->SetFont('Arial', '', 12);
for ($i = 1; $i <= 10; $i++) {
$pdf->Cell(0, 10, "行 $i", 0, 1);
}
// 输出PDF
$pdf->Output('D', 'example.pdf'); // D: 下载, I: 浏览器显示, F: 保存文件
使用Dompdf库(HTML转PDF)
安装
composer require dompdf/dompdf
示例代码
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Dompdf\Dompdf;
use Dompdf\Options;
// 配置选项
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true);
// 创建实例
$dompdf = new Dompdf($options);
// 加载HTML内容
$html = '
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
h1 { color: #333; }
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; }
</style>
</head>
<body>
<h1>HTML转PDF示例</h1>
<table>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>城市</th>
</tr>
<tr>
<td>张三</td>
<td>28</td>
<td>北京</td>
</tr>
</table>
</body>
</html>
';
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
// 输出PDF
$dompdf->stream("document.pdf", array("Attachment" => false));
// Attachment = true 强制下载,false 在浏览器中显示
使用MPDF库(复杂HTML支持)
安装
composer require mpdf/mpdf
示例代码
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Mpdf\Mpdf;
// 创建实例
$mpdf = new Mpdf([
'mode' => 'utf-8',
'format' => 'A4',
'margin_top' => 20,
'margin_bottom' => 20,
'margin_left' => 15,
'margin_right' => 15
]);
// 设置页眉
$mpdf->SetHeader('Company Name || {PAGENO}');
// 设置页脚
$mpdf->SetFooter('Generated by PHP');
// 写入HTML内容
$mpdf->WriteHTML('
<h1>MPDF Example</h1>
<p>This is a paragraph with <strong>bold</strong> and <em>italic</em> text.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
');
// 输出PDF
$mpdf->Output('example.pdf', 'I');
// I: 浏览器输出, D: 下载, F: 保存文件, S: 返回字符串
实用的导出函数封装
<?php
class PDFExport {
// 使用TCPDF导出含表格的PDF
public static function exportTablePDF($data, $headers, $title = '') {
require_once __DIR__ . '/vendor/autoload.php';
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator('Your App');
$pdf->SetAuthor('System');
$pdf->SetTitle($title);
$pdf->SetSubject('Data Export');
// 设置边距
$pdf->SetMargins(15, 20, 15);
$pdf->SetAutoPageBreak(TRUE, 20);
$pdf->AddPage();
// 标题
$pdf->SetFont('dejavusans', 'B', 18);
$pdf->Cell(0, 15, $title, 0, 1, 'C');
$pdf->Ln(10);
// 表头
$pdf->SetFont('dejavusans', 'B', 10);
$pdf->SetFillColor(52, 73, 94);
$pdf->SetTextColor(255, 255, 255);
foreach ($headers as $header) {
$pdf->Cell(35, 8, $header, 1, 0, 'C', true);
}
$pdf->Ln();
// 数据行
$pdf->SetFont('dejavusans', '', 9);
$pdf->SetTextColor(0, 0, 0);
foreach ($data as $row) {
foreach ($row as $cell) {
$pdf->Cell(35, 8, $cell, 1, 0, 'C');
}
$pdf->Ln();
}
return $pdf->Output($title . '.pdf', 'D');
}
// 使用Dompdf导出HTML模板
public static function exportHTMLPDF($html, $filename = 'document.pdf') {
require_once __DIR__ . '/vendor/autoload.php';
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream($filename, ["Attachment" => true]);
}
}
// 使用示例
// $headers = ['姓名', '年龄', '城市'];
// $data = [
// ['张三', '28', '北京'],
// ['李四', '25', '上海']
// ];
// PDFExport::exportTablePDF($data, $headers, '用户列表');
文件保存和下载
<?php
// 保存到服务器
$pdf->Output('/path/to/save/document.pdf', 'F');
// 强制下载
$pdf->Output('document.pdf', 'D');
// 浏览器内联显示
$pdf->Output('document.pdf', 'I');
// 返回字符串
$pdfString = $pdf->Output('document.pdf', 'S');
推荐选择
- TCPDF - 最稳定,中文支持好,适合复杂PDF
- Dompdf - 适合HTML模板转PDF,CSS支持好
- FPDF - 轻量级,适合简单PDF
- MPDF - 复杂的HTML和CSS渲染
根据你的具体需求选择合适的库,如果需要处理中文内容,TCPDF和MPDF是更好的选择。