Home > Backend Development > PHP Tutorial > How to generate PDF files with PHP

How to generate PDF files with PHP

墨辰丷
Release: 2023-03-29 21:14:01
Original
9660 people have browsed it

This article mainly introduces the method of generating PDF files with PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

The code is as follows:

<?php
/*
*功能:生成用户诊断报告PDF文件
*创建时间:--
*/
//phpinfo();exit;
//引入MPDF类文件
set_time_limit();
include &#39;/include/MPDF/mpdf.php&#39;;
//实例化mpdf
$mpdf=new mPDF(&#39;utf-&#39;,&#39;A&#39;,&#39;&#39;,&#39;宋体&#39;,,,,);
//设置字体,解决中文乱码
$mpdf->useAdobeCJK = true;
$mpdf->SetAutoFont(AUTOFONT_ALL);
//获取要生成的静态文件
$html=file_get_contents(&#39;template.html&#39;);
echo $html;exit;
//设置PDF页眉内容
$header=&#39;<table width="%" style="margin: auto;border-bottom: px solid #FBD; vertical-align: middle; font-family:
serif; font-size: pt; color: #;"><tr>
<td width="%"></td>
<td width="%" align="center" style="font-size:px;color:#AAA">页眉</td>
<td width="%" style="text-align: right;"></td>
</tr></table>&#39;;
//设置PDF页脚内容
$footer=&#39;<table width="%" style=" vertical-align: bottom; font-family:
serif; font-size: pt; color: #;"><tr style="height:px"></tr><tr>
<td width="%"></td>
<td width="%" align="center" style="font-size:px;color:#AAA">页脚</td>
<td width="%" style="text-align: left;">页码:{PAGENO}/{nb}</td>
</tr></table>&#39;;
//添加页眉和页脚到pdf中
$mpdf->SetHTMLHeader($header);
$mpdf->SetHTMLFooter($footer);
//设置pdf显示方式
$mpdf->SetDisplayMode(&#39;fullpage&#39;);
//设置pdf的尺寸为mm*mm
//$mpdf->WriteHTML(&#39;<pagebreak sheet-size="mm mm" />&#39;);
//创建pdf文件
$mpdf->WriteHTML($html);
//删除pdf第一页(由于设置pdf尺寸导致多出了一页)
//$mpdf->DeletePages(,);
//输出pdf
$mpdf->Output();
exit;
?>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP simply implements the method of uploading files, videos, etc. using the FTP class

Detailed explanation of using websocket examples in PHP

Detailed explanation of php api data interface writing examples

The above is the detailed content of How to generate PDF files with PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template