PHP导出成PDF功能开发教程
<p><strong>准备工作</strong></p><p>首先查询了相关的类库,有FPDF,zendPDF,TcPDF等等。首先看了下先选择了FPDF,可以说除了中文字符以外没有什么问题,中文乱码而且看了下最新版本没有很好的解决方案,所以只能放弃。后来就专门找支持中文的发现了TcPDF,开始也是中文字体支持不是很好,但是发现了有人做了中文的语言包才使得TcPDF更加完美起来。</p>
<p> </p>
<p><strong>简介</strong></p>
<p>TCPDF 是一个流行的用于生成 PDF 文档的 PHP 类。TCPDF是当前唯一完整支持 UTF-8 Unicode 以及从右至左书写的语言包括双向文稿的 PHP 库。</p>
<p> </p>
<p><strong>TCPDF说明文档</strong></p>
<p><strong>一、首先调用TCPDF文件</strong></p>
<p>require_once('tcpdf.php');</p>
<p><strong>二、实例化TCPDF类 页面方向(P =肖像,L =景观)、测量(mm)、页面格式</strong></p>
<p> $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false); </p>
<p>更多文档信息你可以查看此链接:</p>
<p>https://www.cnblogs.com/520fyl/p/5396374.html</p>
<p> </p>
<p><strong>以下是使用<strong>TCPDF的示例,做法很简单,看代码吧</strong></strong></p>
<div class="cnblogs_code">
<pre>vendor('Pdf'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$pdf</span> = <span style="color: rgba(0, 0, 255, 1)">new</span> \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, <span style="color: rgba(0, 0, 255, 1)">true</span>, 'UTF-8', <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$pdf</span>-><span style="color: rgba(0, 0, 0, 1)">SetCreator(PDF_CREATOR);
</span><span style="color: rgba(128, 0, 128, 1)">$pdf</span>->SetAuthor('sunnier'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$pdf</span>->SetTitle('123'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$pdf</span>->SetSubject('123'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$pdf</span>->SetKeywords('sunnier'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> set default header data</span>
<span style="color: rgba(128, 0, 128, 1)">$pdf</span>->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE,<span style="color: rgba(0, 0, 0, 1)"> PDF_HEADER_STRING);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> set header and footer fonts</span>
<span style="color: rgba(128, 0, 128, 1)">$pdf</span>->setHeaderFont(<span style="color: rgba(0, 0, 255, 1)">Array</span>(PDF_FONT_NAME_MAIN, '',<span style="color: rgba(0, 0, 0, 1)"> PDF_FONT_SIZE_MAIN));
</span><span style="color: rgba(128, 0, 128, 1)">$pdf</span>->setFooterFont(<span style="color: rgba(0, 0, 255, 1)">Array</span>(PDF_FONT_NAME_DATA, '',<span style="color: rgba(0, 0, 0, 1)"> PDF_FONT_SIZE_DATA));
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> set default monospaced font</span>
<span style="color: rgba(128, 0, 128, 1)">$pdf</span>-><span style="color: rgba(0, 0, 0, 1)">SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> set margins</span>
<span style="color: rgba(128, 0, 128, 1)">$pdf</span>->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP,<span style="color: rgba(0, 0, 0, 1)"> PDF_MARGIN_RIGHT);
</span><span style="color: rgba(128, 0, 128, 1)">$pdf</span>-><span style="color: rgba(0, 0, 0, 1)">SetHeaderMargin(PDF_MARGIN_HEADER);
</span><span style="color: rgba(128, 0, 128, 1)">$pdf</span>-><span style="color: rgba(0, 0, 0, 1)">SetFooterMargin(PDF_MARGIN_FOOTER);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> set auto page breaks</span>
<span style="color: rgba(128, 0, 128, 1)">$pdf</span>->SetAutoPageBreak(<span style="color: rgba(0, 0, 255, 1)">TRUE</span>,<span style="color: rgba(0, 0, 0, 1)"> PDF_MARGIN_BOTTOM);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> set image scale factor</span>
<span style="color: rgba(128, 0, 128, 1)">$pdf</span>-><span style="color: rgba(0, 0, 0, 1)">setImageScale(PDF_IMAGE_SCALE_RATIO);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> set some language-dependent strings (optional)</span>
<span style="color: rgba(0, 0, 255, 1)">global</span> <span style="color: rgba(128, 0, 128, 1)">$l</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$pdf</span>->setLanguageArray(<span style="color: rgba(128, 0, 128, 1)">$l</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ---------------------------------------------------------
// set font</span>
<span style="color: rgba(128, 0, 128, 1)">$pdf</span>->SetFont('simfang', '', 10<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Print a table
// add a page</span>
<span style="color: rgba(128, 0, 128, 1)">$pdf</span>-><span style="color: rgba(0, 0, 0, 1)">AddPage();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 随便写HTML</span>
<span style="color: rgba(128, 0, 128, 1)">$html</span> = 'adsf'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> output the HTML content</span>
<span style="color: rgba(128, 0, 128, 1)">$pdf</span>->writeHTML(<span style="color: rgba(128, 0, 128, 1)">$html</span>, <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 0, 255, 1)">false</span>, <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 0, 255, 1)">false</span>, ''<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> reset pointer to the last page</span>
<span style="color: rgba(128, 0, 128, 1)">$pdf</span>-><span style="color: rgba(0, 0, 0, 1)">lastPage();
</span><span style="color: rgba(128, 0, 128, 1)">$pdf</span>->Output('哈哈哈'.pdf', 'I');</pre>
</div>
<p>以上,可以不断添加addPage,添加多页,最后Output输出自己的名字的文件即可。 <br>演示图: </p>
<p><img src="https://img2018.cnblogs.com/blog/649207/201905/649207-20190530153544728-2019097979.jpg" alt=""></p>
<p>可以看到其中可以插入图片也是没有问题的,写法就按照html写就OK,当然不只是支持HTML,只是这里比较适合这种方式。</p>
<h1><strong>以上附资源下载</strong></h1>
<p>链接: https://pan.baidu.com/s/1gfjC0yJ </p>
<p>密码: k8r8</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/qichao123/p/10949903.html
頁:
[1]