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

21 篇文章 0 订阅

目录

引入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.下载并放置中文语言包(他人封装):GitHub - DCgithub21/cd_FPDF: 使用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");
    }
}

参考用法的博客链接

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

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东小记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值