【第三方库】PHP实现创建PDF文件和编辑PDF文件
目录
引入Setasign/fpdf、Setasign/fpdi
解决写入中文时乱码问题
1.下载并放置中文语言包(他人封装):https://github.com/DCgithub21/cd_FPDF
2.编写并运行生成字体文件的程序文件(addFont.php)
中文字体举例(其他字体同样操作)
3.修改部分文件内容
实现功能-创建PDF文件
实现功能-编辑PDF文件
感谢阅读,欢迎讨论(本文仅记录项目集成的功能)
引入Setasign/fpdf、Setasign/fpdi
composer require Setasign/fpdf ^1.8
composer require Setasign/fpdi ^2.3
fpdi官网:FPDI free PDF document importer ▷ setasign.com
fpdf官网:FPDF
fpdf中文使用手册
解决写入中文时乱码问题
1.下载并放置中文语言包(他人封装):https://github.com/DCgithub21/cd_FPDF
ttf2pt1.zip:字体转换工具程序
解压文件夹并修改名称:fpdf_chinese;
将文件夹放置到vendor/setasign目录下,与setasign/fpdf、setasign/fpdi同等级 ;
2.编写并运行生成字体文件的程序文件(addFont.php)
<?php
// 引入fPdf中文语言包
require '../vendor/setasign/fpdf/makefont/makefont.php';
// 设置中文字体文件的路径
$fontFilePath = './simhei.ttf';
// 执行方法,转换字体文件
MakeFont($fontFilePath);
# 执行脚本文件
php addFont.php simhei.ttf
中文字体举例(其他字体同样操作)
引入中文字体:微软雅黑、下载中文字体:微软雅黑
下载地址:http://m.diyiziti.com/Download/537/
3.修改部分文件内容
vendor/setasign/fpdi/src/FpdfTpl.php
vendor/setasign/fpdf_chinese/chinese.php
vendor/setasign/fpdf_chinese/fpdf.php
实现功能-创建PDF文件
class Setasign extends Pdf
{/* todo 边框的参数值 */const Border = 1; // todo 设置边框const NoBorder = 0; // todo 设置无边框const BorderL = 'L'; // todo 设置左边边框const BorderR = 'R'; // todo 设置右边边框const BorderT = 'T'; // todo 设置顶部边框const BorderB = 'B'; // todo 设置底部边框/* todo 文字的对齐方向 */const AlignJ = 'J'; // todo 自动调整const AlignL = 'L'; // todo 左边对齐const AlignC = 'C'; // todo 居中对齐const AlignR = 'R'; // todo 右边对齐/* todo 内容的换行方向 */const LnRight = 0; // todo 向右移动const LnNextStart = 1; // todo 向下一行开始const LnDown = 2; // todo 向下面移动/* todo 背景是否填满 */const FillBgTransparent = 0; // todo 透明背景const FillBg = 1; // todo 填满背景/* 创建pdf文件 */public static function createPdf(){// 引入中文的fpdf库require root_path('vendor\\setasign\\fpdf_chinese') . 'chinese.php';$pdf = new \PDF_Chinese();$pdf->SetAutoPageBreak(true);$pdf->AddPage();$pdf->AddGBFont('simhei',iconv("UTF-8","gbk",'黑体'));$pdf->SetFont('simhei');$width = $pdf->GetPageWidth(); // 页面的宽度$height = $pdf->GetPageHeight(); // 页面的高度$pdf->SetTitle(iconv("UTF-8", "gbk", "test-pdf"));$str = "自动换行,自动换行,自动换行,自动换行,自动换行,自动换行自动换行,自动换行,自动换行,自动换行,自动换行,自动换行";$pdf->MultiCell($width - 20, 8, iconv("utf-8", "gbk", $str));for ($i = 1;$i < 100;$i++) {$str = "这是第{$i}行的文字";$pdf->MultiCell($width - 20, 8, iconv("utf-8", "gbk", $str), null, Setasign::AlignC);}$pdf->Output(root_path('app\\controller').'/result.pdf',"F");}
}
实现功能-编辑PDF文件
<?php
namespace Pdf;use setasign\Fpdi\Fpdi;
class Setasign extends Pdf
{/* todo 边框的参数值 */const Border = 1; // todo 设置边框const NoBorder = 0; // todo 设置无边框const BorderL = 'L'; // todo 设置左边边框const BorderR = 'R'; // todo 设置右边边框const BorderT = 'T'; // todo 设置顶部边框const BorderB = 'B'; // todo 设置底部边框/* todo 文字的对齐方向 */const AlignJ = 'J'; // todo 自动调整const AlignL = 'L'; // todo 左边对齐const AlignC = 'C'; // todo 居中对齐const AlignR = 'R'; // todo 右边对齐/* todo 内容的换行方向 */const LnRight = 0; // todo 向右移动const LnNextStart = 1; // todo 向下一行开始const LnDown = 2; // todo 向下面移动/* todo 背景是否填满 */const FillBgTransparent = 0; // todo 透明背景const FillBg = 1; // todo 填满背景public static function editPdf(){$pdf = new Fpdi();// 获取pdf的页数$pageCount = $pdf->setSourceFile(root_path('app\\controller').'/result.pdf');// 设置全局字体、字体大小、字体颜色、每一个都需要单独字体则再设置覆盖$pdf->AddGBFont('mryh', iconv("utf-8", "gbk", "微软雅黑"));$pdf->SetFont("mryh", '', 20);$pdf->SetTextColor(0,0,0);$width = $pdf->GetPageWidth(); // 页面的宽度// 加载第一页$pdf->AddPage();$tpl = $pdf->importPage(1);$pdf->useTemplate($tpl, 0, 0);$pdf->SetXY(10, 170);$str = "第一页设置的内容";$pdf->MultiCell($width - 20, 8,iconv("utf-8", "gbk", $str),null, Setasign::AlignC);// 加载第二页$pdf->AddPage();$tpl = $pdf->importPage(2);$pdf->useTemplate($tpl, 0, 0, null, null, true);$str = "第二页设置的内容";$pdf->SetXY(40, 170);$pdf->Write(10, iconv("utf-8", "gbk", $str));// 输出文件$pdf->Output(root_path('app\\controller').'/result1.pdf',"F");}
}