zencart下产品内容自动生成pdf
作者:stoat
发布时间:2019-09-17
分类:技术
老项目了,太久不碰zencart,翻到了记录一下,当时需求是产品信息结合前端页面做采集后整理得出整理生成PDF。主要是利用mPDF库,结合css生成带样式的pdf,以提高可读性,给商品(药品)批量采集后生成说明文档。
最终生成样式截图

MPDF库有个优点,可以自定义页头页尾,之前也试过tcpdf,但是感觉有坑,具体忘记了,同时要注意编码问题,有可能会有乱码。
代码贴一下。
include('lib/mpdf.php');
//$mpdf=new mPDF('UTF-8','A4','','',15,15,36,15);
$mpdf=new mPDF('UTF-8','Letter','','',5,5,36,5);
$mpdf->useAdobeCJK = true;
$mpdf->SetAutoFont(AUTOFONT_ALL);
$mpdf->SetDisplayMode('fullpage');
$css = 'lib/style.css';
$cssContent = file_get_contents($css);
$mpdf->showWatermarkText = true;
$mpdf->SetAutoFont();
$footer = '';
$mpdf->SetHTMLFooter( $footer);
$header = '
'.($category_name).' Monoclonal to '.$product_info->fields['products_name'].'
';
$mpdf->SetHTMLHeader( $header);
$mpdf->WriteHTML($cssContent,1);
//设置file_get_content的user_agent可用,防止采集为空
ini_set('user_agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36');
$url1= 'http://'.$_SERVER['HTTP_HOST'].'/index.php?main_page=product_info_a&products_id='.$vo;
$strContent1 = file_get_contents($url1);
$mpdf->WriteHTML($strContent1);
$url2= 'http://'.$_SERVER['HTTP_HOST'].'/index.php?main_page=product_info_b&products_id='.$vo;
$strContent2 = file_get_contents($url2);
$mpdf->WriteHTML($strContent2);
$mpdf->DeletePages(3);
$mpdf->Output($path_pdf.$products_model.'.pdf');
exit;
原文地址:zencart下产品内容自动生成pdf by 雪鼬博客
标签:none
评论已关闭