1.composer安装
? ? ? ? 1.composer.json添加
"require": {
"setasign/fpdf": "^1.8",
"setasign/fpdi": "^2.0"
}
? ? ? ? 2.执行命令?composer require?setasign/fpdi,扩展安装成功后创建vendor/setasign,但此时不支持中文,需要安装支持中文的字体库--simhei

?
2.安装字体黑体simhei
? ? ? ? 将文章最后压缩包解压后,将目录font下4个文件复制到vendor/setasign/fpdf/font下。chinese.php是源文件,适配到自己项目中。PdfChinese.php是我适配自己项目的文件。注意:
在设置字体时,使用 AddGBFont 或AddBig5Font ,chinese.php 334行会报错,$font[‘cw’]为null,原因是使用超全局变量获取GB_widths或Big5_widths失败,可以将这两个变量定义到类里,然后使用this获取。
function AddGBFont($family='GB', $name='STSongStd-Light-Acro')
{
// Add GB font with proportional Latin
$cw = $this->getGBWidths();
$CMap = 'GBKp-EUC-H';
$registry = array('ordering'=>'GB1', 'supplement'=>2);
$this->AddCIDFonts($family,$name,$cw,$CMap,$registry);
}
/**
* 获取GBWidths
*
* @return int[]
*/
public function getGBWidths()
{
return array(
' ' => 207, '!' => 270, '"' => 342, '#' => 467, '$' => 462, '%' => 797, '&' => 710, '\'' => 239,
'(' => 374, ')' => 374, '*' => 423, '+' => 605, ',' => 238, '-' => 375, '.' => 238, '/' => 334, '0' => 480, '1' => 480,
'2' => 480, '3' => 480, '4' => 480, '5' => 480, '6' => 480, '7' => 480, '8' => 480, '9' => 480, ':' => 238, ';' => 238,
'<' => 605, '=' => 605, '>' => 605, '?' => 344, '@' => 748, 'A' => 684, 'B' => 560, 'C' => 695, 'D' => 739, 'E' => 563,
'F' => 511, 'G' => 729, 'H' => 793, 'I' => 318, 'J' => 312, 'K' => 666, 'L' => 526, 'M' => 896, 'N' => 758, 'O' => 772,
'P' => 544, 'Q' => 772, 'R' => 628, 'S' => 465, 'T' => 607, 'U' => 753, 'V' => 711, 'W' => 972, 'X' => 647, 'Y' => 620,
'Z' => 607, '[' => 374, '\\' => 333, ']' => 374, '^' => 606, '_' => 500, '`' => 239, 'a' => 417, 'b' => 503, 'c' => 427,
'd' => 529, 'e' => 415, 'f' => 264, 'g' => 444, 'h' => 518, 'i' => 241, 'j' => 230, 'k' => 495, 'l' => 228, 'm' => 793,
'n' => 527, 'o' => 524, 'p' => 524, 'q' => 504, 'r' => 338, 's' => 336, 't' => 277, 'u' => 517, 'v' => 450, 'w' => 652,
'x' => 466, 'y' => 452, 'z' => 407, '{' => 370, '|' => 258, '}' => 370, '~' => 605
);
}
3.项目实战
????????尝试具体FPDF的操作方法,仅供参考。注意可能涉及字符集转换,UTF-8转GB2312。
#1.调用
$add_water_info = [
'iphone' => mb_convert_encoding($phone, "GB2312", 'UTF-8'),
'ip' => mb_convert_encoding($ip, "GB2312", 'UTF-8'),
'c_time' => mb_convert_encoding($c_time, "GB2312", 'UTF-8'),
];
$file_type = 'subscription';
$url = $this->addWater($add_water_info, $file_type);
$data = ['url' => $url];
#2 实现方法
/**
* PDF文件加水印-
*/
private function addWater($add_info, $file_type)
{
$pdf = new PdfChinese();
//设置字体要在实例化之后就设置 否则会报错
$pdf->AddGBFont('simhei', '黑体');
$file = \Yii::$app->basePath . '/static/file/' . $file_type . '.pdf';
//获取页数
$pageCount = $pdf->setSourceFile($file);
//遍历所有页面
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
//导入页面
$templateId = $pdf->importPage($pageNo);
//获取导入页面的大小
$size = $pdf->getTemplateSize($templateId);
//创建页面(横向或纵向取决于导入的页面大小)
if ($size['width'] > $size['height']) {
$pdf->AddPage('L', array($size['width'], $size['height']));
} else {
$pdf->AddPage('P', array($size['width'], $size['height']));
}
//使用导入的页面
$pdf->useTemplate($templateId);
$pdf->SetFont('simhei', 'B', 9);
if ($pageNo == 2) { //资料只第2页
//手机号
$pdf->SetXY(43, 262.5); //设置位置
$pdf->Cell(0, 7, $add_info['iphone'], 3, 1);
//ip地址
$pdf->SetXY(99, 262.5);
$pdf->Cell(0, 7, $add_info['ip'], 3, 1);
//订阅时间
$pdf->SetXY(157, 262.5);
$pdf->Cell(0, 7, $add_info['c_time'], 3, 1);
}
}
// save file
$outfile = '/tmp/' . uniqid('wk') . '.pdf';
$pdf->Output('F', $outfile, false);
ob_clean();
return $outfile;
}
效果如下,


4.巨人的肩膀-文章参考
#1.composer安装 https://www.cnblogs.com/caicaizi/p/9131845.html
#2.FPDF文件操作具体姿势 https://www.cnblogs.com/qingsong/p/5031294.html
#采坑 https://blog.csdn.net/xiangdaoquan/article/details/115130453
#进阶篇,自己添加字符集 https://blog.csdn.net/weixin_30599769/article/details/99583609
|