• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

第三方库PHP实现创建PDF文件和编辑PDF文件

武飞扬头像
东小记
帮助3

目录

引入Setasign/fpdf、Setasign/fpdi

解决写入中文时乱码问题

1.下载并放置中文语言包(他人封装):https://github.com/DCgithub21/cd_FPDF

2.编写并运行生成字体文件的程序文件(addFont.php)

中文字体举例(其他字体同样操作)

3.修改部分文件内容

实现功能-创建PDF文件

实现功能-编辑PDF文件

感谢阅读,欢迎讨论(本文仅记录项目集成的功能)


引入Setasign/fpdf、Setasign/fpdi

  1.  
    composer require setasign/fpdf ^1.8
  2.  
    composer require setasign/fpdi ^2.3

fpdi官网:FPDI free PDF document importer ▷ setasign.com

fpdf官网:FPDF 

fpdf中文使用手册 

学新通

解决写入中文时乱码问题

1.下载并放置中文语言包(他人封装):GitHub - DCgithub21/cd_FPDF: 使用FPDF输出中文

 ttf2pt1.zip:字体转换工具程序

解压文件夹并修改名称:fpdf_chinese;

将文件夹放置到vendor/setasign目录下,与setasign/fpdf、setasign/fpdi同等级 ;

2.编写并运行生成字体文件的程序文件(addFont.php)

  1.  
    <?php
  2.  
    // 引入fPdf中文语言包
  3.  
    require '../vendor/setasign/fpdf/makefont/makefont.php';
  4.  
    // 设置中文字体文件的路径
  5.  
    $fontFilePath = './simhei.ttf';
  6.  
    // 执行方法,转换字体文件
  7.  
    MakeFont($fontFilePath);
  1.  
    # 执行脚本文件
  2.  
    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文件

  1.  
    class Setasign extends Pdf
  2.  
    {
  3.  
    /* todo 边框的参数值 */
  4.  
    const Border = 1; // todo 设置边框
  5.  
    const NoBorder = 0; // todo 设置无边框
  6.  
    const BorderL = 'L'; // todo 设置左边边框
  7.  
    const BorderR = 'R'; // todo 设置右边边框
  8.  
    const BorderT = 'T'; // todo 设置顶部边框
  9.  
    const BorderB = 'B'; // todo 设置底部边框
  10.  
     
  11.  
    /* todo 文字的对齐方向 */
  12.  
    const AlignJ = 'J'; // todo 自动调整
  13.  
    const AlignL = 'L'; // todo 左边对齐
  14.  
    const AlignC = 'C'; // todo 居中对齐
  15.  
    const AlignR = 'R'; // todo 右边对齐
  16.  
     
  17.  
    /* todo 内容的换行方向 */
  18.  
    const LnRight = 0; // todo 向右移动
  19.  
    const LnNextStart = 1; // todo 向下一行开始
  20.  
    const LnDown = 2; // todo 向下面移动
  21.  
     
  22.  
    /* todo 背景是否填满 */
  23.  
    const FillBgTransparent = 0; // todo 透明背景
  24.  
    const FillBg = 1; // todo 填满背景
  25.  
     
  26.  
    /* 创建pdf文件 */
  27.  
    public static function createPdf()
  28.  
    {
  29.  
    // 引入中文的fpdf库
  30.  
    require root_path('vendor\\setasign\\fpdf_chinese') . 'chinese.php';
  31.  
    $pdf = new \PDF_Chinese();
  32.  
    $pdf->SetAutoPageBreak(true);
  33.  
    $pdf->AddPage();
  34.  
    $pdf->AddGBFont('simhei',iconv("UTF-8","gbk",'黑体'));
  35.  
    $pdf->SetFont('simhei');
  36.  
    $width = $pdf->GetPageWidth(); // 页面的宽度
  37.  
    $height = $pdf->GetPageHeight(); // 页面的高度
  38.  
     
  39.  
    $pdf->SetTitle(iconv("UTF-8", "gbk", "test-pdf"));
  40.  
    $str = "自动换行,自动换行,自动换行,自动换行,自动换行,自动换行自动换行,自动换行,自动换行,自动换行,自动换行,自动换行";
  41.  
    $pdf->MultiCell($width - 20, 8, iconv("utf-8", "gbk", $str));
  42.  
    for ($i = 1;$i < 100;$i ) {
  43.  
    $str = "这是第{$i}行的文字";
  44.  
    $pdf->MultiCell($width - 20, 8, iconv("utf-8", "gbk", $str), null, Setasign::AlignC);
  45.  
    }
  46.  
    $pdf->Output(root_path('app\\controller').'/result.pdf',"F");
  47.  
    }
  48.  
    }
学新通

实现功能-编辑PDF文件

  1.  
    <?php
  2.  
    namespace Pdf;
  3.  
     
  4.  
    use setasign\Fpdi\Fpdi;
  5.  
    class Setasign extends Pdf
  6.  
    {
  7.  
    /* todo 边框的参数值 */
  8.  
    const Border = 1; // todo 设置边框
  9.  
    const NoBorder = 0; // todo 设置无边框
  10.  
    const BorderL = 'L'; // todo 设置左边边框
  11.  
    const BorderR = 'R'; // todo 设置右边边框
  12.  
    const BorderT = 'T'; // todo 设置顶部边框
  13.  
    const BorderB = 'B'; // todo 设置底部边框
  14.  
     
  15.  
    /* todo 文字的对齐方向 */
  16.  
    const AlignJ = 'J'; // todo 自动调整
  17.  
    const AlignL = 'L'; // todo 左边对齐
  18.  
    const AlignC = 'C'; // todo 居中对齐
  19.  
    const AlignR = 'R'; // todo 右边对齐
  20.  
     
  21.  
    /* todo 内容的换行方向 */
  22.  
    const LnRight = 0; // todo 向右移动
  23.  
    const LnNextStart = 1; // todo 向下一行开始
  24.  
    const LnDown = 2; // todo 向下面移动
  25.  
     
  26.  
    /* todo 背景是否填满 */
  27.  
    const FillBgTransparent = 0; // todo 透明背景
  28.  
    const FillBg = 1; // todo 填满背景
  29.  
     
  30.  
    public static function editPdf()
  31.  
    {
  32.  
    $pdf = new Fpdi();
  33.  
    // 获取pdf的页数
  34.  
    $pageCount = $pdf->setSourceFile(root_path('app\\controller').'/result.pdf');
  35.  
    // 设置全局字体、字体大小、字体颜色、每一个都需要单独字体则再设置覆盖
  36.  
    $pdf->AddGBFont('mryh', iconv("utf-8", "gbk", "微软雅黑"));
  37.  
    $pdf->SetFont("mryh", '', 20);
  38.  
    $pdf->SetTextColor(0,0,0);
  39.  
    $width = $pdf->GetPageWidth(); // 页面的宽度
  40.  
    // 加载第一页
  41.  
    $pdf->AddPage();
  42.  
    $tpl = $pdf->importPage(1);
  43.  
    $pdf->useTemplate($tpl, 0, 0);
  44.  
    $pdf->SetXY(10, 170);
  45.  
    $str = "第一页设置的内容";
  46.  
    $pdf->MultiCell($width - 20, 8,iconv("utf-8", "gbk", $str),null, Setasign::AlignC);
  47.  
     
  48.  
    // 加载第二页
  49.  
    $pdf->AddPage();
  50.  
    $tpl = $pdf->importPage(2);
  51.  
    $pdf->useTemplate($tpl, 0, 0, null, null, true);
  52.  
    $str = "第二页设置的内容";
  53.  
    $pdf->SetXY(40, 170);
  54.  
    $pdf->Write(10, iconv("utf-8", "gbk", $str));
  55.  
     
  56.  
    // 输出文件
  57.  
    $pdf->Output(root_path('app\\controller').'/result1.pdf',"F");
  58.  
    }
  59.  
    }
学新通

参考用法的博客链接

 PHP FPDF::SetFont方法代码示例 - 纯净天空

感谢阅读,欢迎讨论(本文仅记录项目集成的功能)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhffibji
系列文章
更多 icon
同类精品
更多 icon
继续加载