PHP生成PDF格式文件以TCPDF为基础,TCPDF是一个用于快速生成PDF文件的PHP5函数包。TCPDF基于FPDF进行扩展和改进。支持UTF-8,Unicode,HTML和 XHTML。在基于PHP开发的Web应用中,使用它来输出PDF文件是绝佳的选择。但毕竟这款开源软件是外国人开发的,对中文的支持也不是那么尽如人 意,因此我们需要对它作进一步的强化。首先要到TCPDF官网下载TCPDF最新版。访问http://www.tcpdf.org ,单击导航条上的”Download”链接,即可下载到最新版本的TCPDF压缩包。因为里面包含了许多的TCPDF范例和字体文件,因此下载的文件比较大,大概有10M左右。下载完后解压它,会得到一个名为tcpdf的目录,此目录的结构大概是这样的:
直接来代码啦/**
* 使用文档
* https://wenku.baidu.com/view/ce3f52d950e2524de5187e20.html?sxts=1544025155358
* https://wenku.baidu.com/view/9352b049852458fb770b5688.html
*/
include_once(APPLICATION_PATH.'/application/library/Pdf/Config/tcpdf_config_wjy.php');
include_once(APPLICATION_PATH.'/application/library/Pdf/Lib/include/tcpdf_fonts.php');
include_once(APPLICATION_PATH.'/application/library/Pdf/Lib/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', true);
//设置文件信息
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 001');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
//设置页眉页脚
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));
// set header and footer fonts
$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);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
$l['a_meta_charset'] = 'UTF-8';
$l['a_meta_dir'] = 'ltr';
$l['a_meta_language'] = 'cn';
// TRANSLATIONS --------------------------------------
$l['w_page'] = 'page';
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set default font subsetting mode
$pdf->setFontSubsetting(true);
// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
// $pdf->SetFont('dejavusans', '', 14, '', true);
// $pdf->SetFont('stsongstdlight', '', 20, '', true);
$pdf->SetFont('droid_sans_fallback', '', 20, '', true);//自定义字体后面具体怎么制作
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();
// set text shadow effect
$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));
// Set some content to print
$html = '
Welcome to TCPDF !
';// Print text using writeHTMLCell()
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
$txt = "您好!";
$pdf->Write(20, $txt);
$image_file = K_PATH_IMAGES.'logo_voucher.png';
$pdf->Image($image_file, 10, 10, 15, '', 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);
// test Cell stretching
$pdf->Cell(0, 0, 'TEST CELL STRETCH: no stretch', 1, 1, 'C', 0, '', 0);
$pdf->Cell(0, 0, 'TEST CELL STRETCH: scaling', 1, 1, 'C', 0, '', 1);
$pdf->Cell(0, 0, 'TEST CELL STRETCH: force scaling', 1, 1, 'C', 0, '', 2);
$pdf->Cell(0, 0, 'TEST CELL STRETCH: spacing', 1, 1, 'C', 0, '', 3);
$pdf->Cell(0, 0, 'TEST CELL STRETCH: force spacing', 1, 1, 'C', 0, '', 4);
$pdf->Ln(15);
$pdf->Cell(45, 0, 'TEST CELL STRETCH: scaling', 1, 1, 'C', 0, '', 1);
$pdf->Cell(45, 0, 'TEST CELL STRETCH: force scaling', 1, 1, 'C', 0, '', 2);
$pdf->Cell(45, 0, 'TEST CELL STRETCH: spacing', 1, 1, 'C', 0, '', 3);
$pdf->Cell(45, 0, 'TEST CELL STRETCH: force spacing', 1, 1, 'C', 0, '', 4);
// Image method signature:
// Image($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox=false, $hidden=false, $fitonpage=false)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Example of Image from data stream ('PHP rules')
$imgdata = base64_decode('iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABlBMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDrEX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==');
// The '@' character is used to indicate that follows an image data stream and not an image file name
$pdf->Image('@'.$imgdata);
// ---------------------------------------------------------
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$showType = 'D';//PDF输出的方式。I,在浏览器中打开;D,以文件形式下载;F,保存到服务器中;S,以字符串形式输出;E:以邮件的附件输出。
ob_end_clean();
$pdf->Output('example_001.pdf', $showType);
主要问题:
用HTML排版时有些CSS 样式不支持,导致很尴尬,遇到的有vertical-align、margin、padding没起作用,百度、GOOGLE说就是不支持,具体网址没保存下来,想知道的自行搜吧。如果想用不支持的属性只能一点一点去画啦。
自定义字体:
自带工具还是很方便 的。也有例子,自行看吧。自定义字体效率不是很快。