hi,欢迎访问本站!
当前位置: 首页学习笔记正文

ThinkPHP5使用TCPDF将html转换为pdf

用户投稿 学习笔记 20阅读

本文主要介绍使用ThinkPHP5加TCPDF生成pdf文件。

使用composer引入TCPDF compser使用国内镜像: composer config -g repo.packagist composer https://packagist.phpcomposer.com 引入TCPDF:composer require tecnick.com/tcpdf方式:前端提交信息,后端根据模板生成html,然后生成pdf文件  代码 class Index extends Controller{ public function index(){ if($this->request->isPost()){ $param=$this->request->param(); $file=$this->request->file(); $imgPath=$this->saveImg($file); $this->view->assign([ 'title'=>$param['title'], 'subtitle'=>$param['subtitle'], 'img'=>'/public/template/upload/'.$imgPath ]); //渲染模板 $content=$this->fetch('template/temp1'); //生成html文件 $File=new \think\Template\driver\File(); $File->write(ROOT_PATH.'public/template/html/'.md5($param['title']).'.html',$content); //生成pdf文件 $this->htmlToPdf($content,'pdf测试',$param['title']); //返回结果 return '生成pdf文件成功'; }else{ return $this->view->fetch(); } } //保存表单的图片 public function saveImg($files){ $filePath=ROOT_PATH.'public/template/upload/'; foreach ($files as $file){ $info=$file->move($filePath); if($info){ echo $info; } } return $info->getSaveName(); } //生成pdf public function htmlToPdf($html='',$title="标题",$fileName=""){ $pdf=new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // 设置打印模式 //设置文件信息,头部的信息设置 $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor("作者"); $pdf->SetTitle($title); $pdf->SetSubject('TCPDF Tutorial'); $pdf->SetKeywords('TCPDF, PDF, example, test, guide');//设置关键字 // 是否显示页眉 $pdf->setPrintHeader(false); // 设置页眉显示的内容 $pdf->SetHeaderData('logo.png', 60, 'owndraw.com', '', array(0,64,255), array(0,64,128)); // 设置页眉字体 $pdf->setHeaderFont(Array('deja2vusans', '', '12')); // 页眉距离顶部的距离 $pdf->SetHeaderMargin('5'); // 是否显示页脚 $pdf->setPrintFooter(true); // 设置页脚显示的内容 $pdf->setFooterData(array(0,64,0), array(0,64,128)); // 设置页脚的字体 $pdf->setFooterFont(Array('dejavusans', '', '10')); // 设置页脚距离底部的距离 $pdf->SetFooterMargin('10'); // 设置默认等宽字体 $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // 设置行高 $pdf->setCellHeightRatio(1); // 设置左、上、右的间距 $pdf->SetMargins('10', '10', '10'); // 设置是否自动分页 距离底部多少距离时分页 $pdf->SetAutoPageBreak(TRUE, '15'); // 设置图像比例因子 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); $pdf->setFontSubsetting(true); $pdf->AddPage("A4","Landscape",true,true); // 设置字体 $pdf->SetFont('stsongstdlight', '', 14, '', true); $pdf->writeHTML($html);//HTML生成PDF //$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); $showType= 'F';//PDF输出的方式。I,在浏览器中打开;D,以文件形式下载;F,保存到服务器中;S,以字符串形式输出;E:以邮件的附件输出。 ob_end_clean(); $path=ROOT_PATH.'public/template/pdf/'; //判断保存目录是否存在,不存在则进行创建 if(!is_dir($path)){ mkdir($path,'0777',true); } $pdf->Output($path."{$fileName}.pdf", $showType); }}

 

可能遇到的坑(1)提示TCPDF错误: 该问题是因为php的缓冲区已经有数据了,所有解决方法是在执行Output之前先清空缓冲区:ob_end_clean();(2)如果非(F:保存到服务器模式)文件名称包含中文,生成的文件名称中,中文丢失 该问题只要调试一下就可找出问题: 可以看到,因为使用正则替换了,讲中文全部替换为 '' 了,所有直接将该行进行注释。(3)使用(F:保存到服务器模式): 将文件保存到服务器的模式,则$pdf->Output('参数一:填写完整的保存路径','F'); 如果不填写完整路径,则会报错:fopen(): remote host file access not supported这是因为在保存pdf文件的时候,TCPDF会判断路径中是否有(://),如果没有的话,会在路径之前添加file:// 此处还可能存在一个问题:failed to open stream: No such file or directory这是因为保存的路径不存在,所以需要先判断保存的文件夹是否存在,不存在的话先创建在进行保存(is_dir,mkdir)

效果(模板随便写的,见谅哈哈)

结语 如果不足之处,敬请指正,谢谢!

标签:
声明:无特别说明,转载请标明本文来源!
发布评论
正文 取消