首页 > 技术文章 > LARAVEL 使用TCPDF生成PDF文档,html转成pdf - TCPDF

qing123 2022-02-19 11:11 原文

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

这里写图片描述

首先,引入TCPDF类库,使用COMPOSER
composer require tecnickcom/tcpdf
代码示例 :
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
     // 设置文档信息 $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下载

  

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

 

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

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

推荐阅读