<?php
// 引入 TCPDF 库文件
require_once('tcpdf/tcpdf.php');
// 创建一个新的 TCPDF 类的实例,页面方向为横向 (L) 或纵向 (P)
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// 设置文档信息
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('John Doe');
$pdf->SetTitle('TCPDF Example');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// 设置页眉和页脚信息
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// 设置默认等宽字体
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// 设置间距
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// 设置自动分页
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// 设置图像比例因子
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// 添加一个页面
$pdf->AddPage();
// 设置字体
$pdf->SetFont('helvetica', '', 12);
// 输出一些文本
$html = <<<EOD
<h1>Welcome to TCPDF!</h1>
<p>This is an example of HTML content in a TCPDF document.</p>
EOD;
$pdf->writeHTML($html, true, false, true, false, '');
// 输出 PDF 文档到浏览器
$pdf->Output('example.pdf', 'I');
?>
require_once
引入 TCPDF 的核心库文件。SetCreator
、SetAuthor
等方法设置 PDF 文档的元数据。AddPage()
方法添加新的页面。SetFont()
设置字体,并通过 writeHTML()
输出 HTML 内容。Output()
方法将生成的 PDF 文件输出到浏览器或保存为文件。希望这段代码和解释能帮助你快速上手使用 TCPDF 生成 PDF 文件。
上一篇:php strip_tags
下一篇:php7
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站