本文目录导读:

在PHP中生成PDF有多种方法,以下是几种常用的方案:
使用TCPDF(最流行)
<?php
require_once('tcpdf/tcpdf.php');
// 创建PDF实例
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// 设置文档信息
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Your Name');
$pdf->SetTitle('My PDF Document');
$pdf->SetSubject('TCPDF Tutorial');
// 去除页眉页脚
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// 添加页面
$pdf->AddPage();
// 设置字体
$pdf->SetFont('helvetica', '', 12);
$html = '
<h1>Hello World</h1>
<p>This is a sample PDF generated with TCPDF.</p>
<table border="1">
<tr>
<th>Name</th>
<th>Email</th>
</tr>
<tr>
<td>John Doe</td>
<td>john@example.com</td>
</tr>
</table>';
// 输出HTML内容
$pdf->writeHTML($html, true, false, true, false, '');
// 输出PDF (I:浏览器显示, D:下载, F:保存到服务器)
$pdf->Output('document.pdf', 'D');
?>
使用FPDF(简单轻量)
<?php
require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
// 设置字体
$pdf->SetFont('Arial', 'B', 16);
// 添加文本
$pdf->Cell(40, 10, 'Hello World!');
// 创建表格
$pdf->Ln();
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(40, 10, 'Name: John Doe');
$pdf->Ln();
$pdf->Cell(40, 10, 'Email: john@example.com');
// 输出PDF
$pdf->Output();
?>
使用Dompdf(支持CSS/HTML)
<?php
require_once('dompdf/autoload.inc.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 { width: 100%; border-collapse: collapse; }
.table th, .table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
</style>
</head>
<body>
<h1>Invoice #12345</h1>
<p>Date: 2024-01-01</p>
<table class="table">
<tr>
<th>Product</th>
<th>Price</th>
</tr>
<tr>
<td>Item 1</td>
<td>$20.00</td>
</tr>
<tr>
<td>Item 2</td>
<td>$30.00</td>
</tr>
</table>
</body>
</html>';
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
// 输出PDF
$dompdf->stream("document.pdf", array("Attachment" => false));
?>
使用mPDF(支持复杂文档)
<?php
require_once('vendor/autoload.php');
$mpdf = new \Mpdf\Mpdf([
'mode' => 'utf-8',
'format' => 'A4',
'margin_left' => 15,
'margin_right' => 15,
'margin_top' => 16,
'margin_bottom' => 16,
'margin_header' => 10,
'margin_footer' => 10
]);
$mpdf->WriteHTML('<h1>Report</h1>');
$mpdf->WriteHTML('<p>Generated on: ' . date('Y-m-d') . '</p>');
// 添加分页
$mpdf->AddPage();
$html = '<table>
<thead>
<tr><th>ID</th><th>Name</th><th>Amount</th></tr>
</thead>
<tbody>
<tr><td>1</td><td>Product A</td><td>$50</td></tr>
<tr><td>2</td><td>Product B</td><td>$30</td></tr>
</tbody>
</table>';
$mpdf->WriteHTML($html);
// 输出PDF
$mpdf->Output('report.pdf', 'D');
?>
完整的示例(带中文支持)
<?php
require_once('tcpdf/tcpdf.php');
class PDFGenerator {
private $pdf;
public function __construct() {
$this->pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$this->setup();
}
private function setup() {
// 设置页面
$this->pdf->SetMargins(15, 30, 15);
$this->pdf->SetHeaderMargin(10);
$this->pdf->SetFooterMargin(10);
// 设置元数据
$this->pdf->SetCreator('My App');
$this->pdf->SetAuthor('Developer');
$this->pdf->SetTitle('Generated PDF');
$this->pdf->SetSubject('Document');
// 添加页眉页脚
$this->pdf->setHeaderCallback(function($pdf) {
$pdf->SetFont('helvetica', 'B', 12);
$pdf->Cell(0, 10, 'My Company - Official Document', 0, false, 'C', 0, '', 0, false, 'M', 'M');
});
$this->pdf->setFooterCallback(function($pdf) {
$pdf->SetY(-15);
$pdf->SetFont('helvetica', 'I', 8);
$pdf->Cell(0, 10, 'Page ' . $pdf->getAliasNumPage() . '/' . $pdf->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'M', 'M');
});
// 添加第一页
$this->pdf->AddPage();
}
public function addContent($html) {
$this->pdf->writeHTML($html, true, false, true, false, '');
}
public function addTable($data) {
$html = '<table border="1" cellpadding="5" style="width:100%;">';
$html .= '<tr style="background-color:#f0f0f0;">';
foreach($data['header'] as $header) {
$html .= '<th>' . $header . '</th>';
}
$html .= '</tr>';
foreach($data['rows'] as $row) {
$html .= '<tr>';
foreach($row as $cell) {
$html .= '<td>' . $cell . '</td>';
}
$html .= '</tr>';
}
$html .= '</table>';
$this->pdf->writeHTML($html, true, false, true, false, '');
}
public function download($filename = 'document.pdf') {
$this->pdf->Output($filename, 'D');
}
public function save($path) {
$this->pdf->Output($path, 'F');
}
}
// 使用示例
$pdfGen = new PDFGenerator();
$pdfGen->addContent('<h1>Sales Report</h1>');
$pdfGen->addContent('<p>Date: ' . date('Y-m-d H:i:s') . '</p>');
$data = [
'header' => ['Product', 'Price', 'Quantity', 'Total'],
'rows' => [
['Product A', '$50', '2', '$100'],
['Product B', '$30', '3', '$90'],
['Product C', '$20', '5', '$100']
]
];
$pdfGen->addTable($data);
$pdfGen->download('sales_report.pdf');
?>
基于HTML模板生成PDF
<?php
require_once('vendor/autoload.php');
use Dompdf\Dompdf;
class HtmlToPdf {
public static function convert($htmlTemplate, $data) {
// 渲染模板
$html = self::renderTemplate($htmlTemplate, $data);
// 生成PDF
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4');
$dompdf->render();
return $dompdf->output();
}
private static function renderTemplate($template, $data) {
foreach($data as $key => $value) {
$template = str_replace('{{' . $key . '}}', $value, $template);
}
return $template;
}
}
// 使用示例
$htmlTemplate = '
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial; font-size: 12px; }
.header { background: #007bff; color: white; padding: 20px; }
.content { padding: 20px; }
</style>
</head>
<body>
<div class="header">
<h1>Invoice</h1>
<p>Invoice Number: {{invoice_number}}</p>
</div>
<div class="content">
<p>Date: {{date}}</p>
<p>Customer Name: {{customer_name}}</p>
<p>Total Amount: {{total_amount}}</p>
</div>
</body>
</html>';
$data = [
'invoice_number' => 'INV-2024-001',
'date' => '2024-01-15',
'customer_name' => 'John Doe',
'total_amount' => '$150.00'
];
$pdfData = HtmlToPdf::convert($htmlTemplate, $data);
file_put_contents('invoice.pdf', $pdfData);
?>
安装方法
使用Composer安装:
# TCPDF composer require tecnickcom/tcpdf # Dompdf composer require dompdf/dompdf # mPDF composer require mpdf/mpdf # FPDF(需要手动下载) # 从官网下载: http://www.fpdf.org/
选择建议
- TCPDF:功能最全面,支持各种PDF特性,适合复杂文档
- Dompdf:支持CSS/HTML,对前端开发者友好,适合简单到中等复杂度的文档
- mPDF:支持中文最好,适合中文文档生成
- FPDF:最轻量,适合简单应用
根据你的具体需求选择合适的库,如果生成简单文档,建议使用Dompdf;如果需要复杂的排版和样式,推荐使用TCPDF;如果主要是中文文档,用mPDF效果最好。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播