tcpdf将html转化为pdf的方法

归档: 学习酷文


引入方法:composer require tecnickcom/tcpdf


github官方链接:https://github.com/tecnickcom/TCPDF        


 <?php

                

                use TCPDF;


                $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

                $pdf->SetCreator(PDF_CREATOR);

                $pdf->setPrintHeader(false); //页眉

                $pdf->setPrintFooter(false); //页脚

                $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

                $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);  默认边距

                $pdf->SetAutoPageBreak(true, 5); //距离底部进行分页

                $pdf->setCellHeightRatio(1); //行高

                $pdf->SetFont('stsongstdlight', '', 12); //这个字体兼容性强。中文也支持

                $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

                $pdf->setFontSubsetting(true);

                $pdf->SetMargins(5, 5, 5, true); //边距

                $pdf->AddPage("A4","Landscape",true,true); //页面设置

               

                $pdf->writeHTML($this->getHtml($order, $jsTime, $companyName), true, false, true, false, ''); //$this->getHtml() 获取html自定义方法

               

                $fileName = '结算单-pdf-'.time().'.pdf';

                $path =  ROOT_PATH.'public/uploads/'.date('YMd');

                if(!is_dir($path)) {

                    mkdir($path, 0777, true);

                }

                $path .= '/'.$fileName;

                $pdf->Output($path, 'F'); 没有返回值 用file_exists 判读文件是否生成


2024-05-11 02:18:36

黯赏雅亭|ASYT - 应用中心

Copyright @ 2018 - 2024 黯赏雅亭|ASYT -- 傲世衍天 版权所有 渝ICP备19008745号-2