使用tcpdf合成PDF文件

1.使用composer加载tcpdf

composer require tecnickcom/tcpdf

2.引入

require "vendor/autoload.php";

3.代码

	protected $pdf;

    public function __construct()
    {
        $this->pdf = new \TCPDF();
    }

	// 设置PDF参数
    protected function setPdfAttr()
    {
        $this->pdf->setCreator(PDF_CREATOR);
        $this->pdf->setAuthor('Dya');
        $this->pdf->SetSubject('TCPDF Tutorial');
        $this->pdf->SetKeywords('TCPDF, PDF, example, test, guide');

        //页眉页脚
        $this->pdf->setPrintHeader(false);
        $this->pdf->setPrintFooter(true);
        // 页脚信息
        $this->pdf->setFooterData(array(0,64,0), array(0,64,128));
        $this->pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        $this->pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
        // 自动分页 (第二个参数可以设置距离底部多少距离时分页)
        $this->pdf->setAutoPageBreak(true, 15);

        // 设置边距(左 上 右 下) 右边距默认左侧值 下边距是bool值(是否覆盖默认页边距)
        $this->pdf->setMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        // 定义默认的单间距字体 (设置为等宽字体)
        $this->pdf->SetDefaultMonospacedFont('courier');
        // 设置图像比例因子
        $this->pdf->setImageScale(1.25);
    }

两种生成方式
1)html生成PDF

/**
     * html转PDF文档
     * @param $html string 内容
     * @param $title string 标题
     * @param string $fileName 文件名
     */
    public function htmlPdf($html = '', $title = '', $fileName = 'doc.pdf')
    {
        $this->setPdfAttr();
        $this->pdf->setTitle($title);

        // 新增页面
        $this->pdf->AddPage();

        // 设置字体
        $this->pdf->SetFont('stsongstdlight', 'B', 20, '', true);

        // 文章标题
        $this->pdf->Cell(0, 0, '空调销售安装合同', '0', 1, 'C');

        $this->pdf->SetFont('stsongstdlight', '', 14, '', true);
        $this->pdf->writeHTMLCell('0', '0', null, null, $html);
        // PDF输出   I:在浏览器中打开,D:下载,F:在服务器生成pdf ,S:只返回pdf的字符串
        $this->pdf->Output($fileName, 'I');

    }

效果如图:
在这里插入图片描述

2)手动制表

/**
     * 绘制表格
     * @param $title
     * @param $fileName
     */
    public function tablePdf($title, $fileName)
    {
        $this->setPdfAttr();
        $this->pdf->setTitle($title);
        // 新增页面
        $this->pdf->AddPage();

        // 设置字体
        $this->pdf->SetFont('stsongstdlight', 'B', 24, '', true);
        // 参数 1:w 2:h 3:内容 4:边框 5:是否换行 6:内容居中等
        $this->pdf->Cell(0, 0, '财务收款收据', '0', 1, 'C');

        $this->pdf->SetFont('stsongstdlight', '', 18, '', true);
        $this->pdf->Cell(90, 30, 'TEL:888888', '0', 0, 'L');
        // 颜色 RGB颜色代码
        $this->pdf->setTextColor(255, 0, 0);
        $this->pdf->Cell(90, 30, 'NO.20221215', '0', 0, 'R');

        // 换行
        $this->pdf->Ln();

        // 重置颜色
        $this->pdf->setTextColor();
        $this->pdf->SetFont('stsongstdlight', '', 16, '', true);
        $this->pdf->Cell(90, 15, '     客户名称:张三', '0', 0, 'L');
        $this->pdf->Cell(90, 15, '日期:   2022年12月15日     ', '0', 1, 'R');

        $h = 15;
        // 绘表  表头
        $this->pdf->Cell(40, $h, '规格及名称', '1', 0, 'C');
        $this->pdf->Cell(15, $h, '单位', '1', 0, 'C');
        $this->pdf->Cell(20, $h, '数量', '1', 0, 'C');
        $this->pdf->Cell(25, $h, '单价', '1', 0, 'C');
        $this->pdf->Cell(45, $h, '金额', '1', 0, 'C');
        $this->pdf->Cell(35, $h, '备注', '1', 1, 'C');

        $data = [
            ['name'=>'项目一','unit'=>'件','num'=>13,'price'=>55.00,'money'=>'715.00','remark'=>''],
            ['name'=>'项目二','unit'=>'件','num'=>13,'price'=>54.00,'money'=>'702.00','remark'=>''],
            ['name'=>'项目三','unit'=>'件','num'=>19,'price'=>54.00,'money'=>'1026.00','remark'=>''],
            ['name'=>'项目四','unit'=>'件','num'=>24,'price'=>54.00,'money'=>'1296.00','remark'=>''],
        ];
        // 内容
        foreach ($data as $item) {
            $this->pdf->Cell(40, $h, $item['name'], '1', 0, 'C');
            $this->pdf->Cell(15, $h, $item['unit'], '1', 0, 'C');
            $this->pdf->Cell(20, $h, $item['num'], '1', 0, 'C');
            $this->pdf->Cell(25, $h, $item['price'], '1', 0, 'C');
            $this->pdf->Cell(45, $h, $item['money'], '1', 0, 'C');
            $this->pdf->Cell(35, $h, $item['remark'], '1', 1, 'C');
        }
        $total = sprintf('%.2F', array_sum(array_column($data, 'money')));
        $this->pdf->Cell(100, $h, '合计金额:', 'LTB', 0, 'L');
        $this->pdf->Cell(45, $h, $total, 'TBR', 0, 'R');
        $this->pdf->Cell(35, $h, '', '1', 0, 'C');

        $this->pdf->Output($fileName, 'I');

    }

效果如图:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值