如何通过Prestashop自定义模块生成无页眉页脚的PDF文件?
Prestashop自定义模块生成纯净PDF解决方案
当前生成PDF的异常效果:
现有实现代码
自定义HTMLTemplate类代码
<?php class HTMLTemplatePrintLabelPdf extends HTMLTemplate { public $custom_model; public function __construct($custom_object, $smarty) { $this->custom_model = $custom_object; $this->smarty = $smarty; } /** * Returns the template's HTML content * @return string HTML content */ public function getContent() { $this->smarty->assign(array( 'custom_model' => $this->custom_model, )); return $this->smarty->fetch(_PS_MODULE_DIR_ . 'pancorderlabel/pdf/printlabel.tpl'); } public function getLogo() { return false; } /** * Returns the template filename * @return string filename */ public function getFilename() { return 'pdf/printlabel.pdf'; } /** * Returns the template filename when using bulk rendering * @return string filename */ public function getBulkFilename() { return 'pdf/printlabel.pdf'; } }
PDF生成触发代码
$pdf = new PDF($custom_object, 'PrintLabelPdf', Context::getContext()->smarty); $pdf->render();
可行解决方案
按以下步骤修改即可去掉多余的页眉、页脚和右侧冗余代码:
- 重写页眉页脚方法,覆盖父类默认实现,直接返回空内容,在
HTMLTemplatePrintLabelPdf类中新增两个方法:
// 移除页眉 public function getHeader() { return ''; } // 移除页脚 public function getFooter() { return ''; }
- 修改PDF渲染的边距和打印配置,去掉右侧多余内容区域,修改触发代码:
$pdf = new PDF($custom_object, 'PrintLabelPdf', Context::getContext()->smarty); // 四个参数分别为左、上、右、下边距,单位毫米,可根据实际需求调整 $pdf->setMargins(0, 0, 0, 0); // 强制关闭页眉页脚输出 $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); $pdf->render();
- 检查
pancorderlabel/pdf/printlabel.tpl模板文件,删除所有调试输出、注释代码,可在模板内新增样式适配PDF尺寸,避免内容溢出:
<style> body { margin: 0; padding: 0; /* 若为固定尺寸标签可直接指定宽高,示例为100mm*150mm的标签尺寸 */ width: 100mm; height: 150mm; } </style>
内容的提问来源于stack exchange,提问作者WMMB
相关产品推荐
相关产品推荐

