Laravel 使用TCPDF生成PDF文档 - tcpdf

上次,简单的提了使用PHPWord生成Word文档,这次,就简单的讲下tcpdf生成PDF文档。效果图如下:

这里写图片描述

首先,引入tcpdf类库,使用composer

composer require tecnickcom/tcpdf

代码示例 :
        // 设置文档信息
        $pdf->SetCreator('Hello world');
        $pdf->SetAuthor('dyk');
        $pdf->SetTitle('TCPDF示例');
        $pdf->SetSubject('TCPDF示例');
        $pdf->SetKeywords('TCPDF, PDF, PHP');

        // 设置页眉和页脚信息
        $pdf->SetHeaderData('tcpdf_logo.jpg', 30, 'www.marchsoft.cn', '三月软件!', [0, 64, 255], [0, 64, 128]);
        $pdf->setFooterData([0, 64, 0], [0, 64, 128]);

        // 设置页眉和页脚字体
        $pdf->setHeaderFont(['stsongstdlight', '', '10']);
        $pdf->setFooterFont(['helvetica', '', '8']);

        // 设置默认等宽字体
        $pdf->SetDefaultMonospacedFont('courier');

        // 设置间距
        $pdf->SetMargins(15, 15, 15);//页面间隔
        $pdf->SetHeaderMargin(5);//页眉top间隔
        $pdf->SetFooterMargin(10);//页脚bottom间隔

        // 设置分页
        $pdf->SetAutoPageBreak(true, 25);

        // 设置自动换页
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        // 设置图像比例因子
        $pdf->setImageScale(1.25);

        // 设置默认字体构造子集模式
        $pdf->setFontSubsetting(true);

        // 设置字体 stsongstdlight支持中文
        $pdf->SetFont('stsongstdlight', '', 14);

        // 添加一页
        $pdf->AddPage();

        $pdf->Ln(5);//换行符

        $html = '
            <table width="400" border="1">
                <tr>
                    <th align="left">消费项目</th>
                    <th align="right">一月</th>
                    <th align="right">二月</th>
                </tr>
                <tr>
                    <td align="left">衣服</td>
                    <td align="right">$241.10</td>
                    <td align="right">$50.20</td>
                </tr>
                <tr>
                    <td align="left">化妆品</td>
                    <td align="right">$30.00</td>
                    <td align="right">$44.45</td>
                </tr>
                <tr>
                    <td align="left">食物</td>
                    <td align="right">$730.40</td>
                    <td align="right">$650.00</td>
                </tr>
                <tr>
                    <th align="left">总计</th>
                    <th align="right">$1001.50</th>
                    <th align="right">$744.65</th>
                </tr>
            </table>
        ';

        $pdf->writeHTML($html, true, false, true, false, '');

        //输出PDF
        $pdf->Output('t.pdf', 'I');//I输出、D下载 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

官方文档上有示例,附:https://tcpdf.org/examples/

官方文档:https://tcpdf.org/

有兴趣的话,可以深入的了解一下

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值