在自定义 pdf Prestashop 中隐藏页眉和页脚

Hide header and footer in custom pdf Prestashop

我在 Prestashop 1.7.4.1 的模块中创建了打印自定义 pdf 的方法。一切正常,但它在每一页上打印带有商店徽标的页眉和带有电子发票信息的页脚。我怎样才能隐藏它们,因为我的模板占据了打印页面的所有尺寸?

我试图从 tcpdf 示例添加到我的 pdf 目标代码,但我似乎没有在 presta 中使用 TCPDF:

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

这是我的 class:

class HTMLTemplateCustomPdf 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()
    {
        //here I get content

        return $this->smarty->fetch(_PS_MODULE_DIR_ . 'ps_first_module/views/templates/hook/pdf.tpl');
    }

    /**
     * Returns the template filename
     * @return string filename
     */
    public function getFilename()
    {
        return 'custom_pdf.pdf';
    }

    /**
     * Returns the template filename when using bulk rendering
     * @return string filename
     */
    public function getBulkFilename()
    {
        return 'custom_pdf.pdf';
    }

这是我创建 pdf 对象的地方:

if (Tools::isSubmit('print')) {
    if (Shop::getContext() != Shop::CONTEXT_GROUP && Shop::getContext() != Shop::CONTEXT_ALL) {
        require_once _PS_MODULE_DIR_ . 'ps_first_module/HTMLTemplateCustomPdf.php';
        $orientation = 'L';
        $pdf = new PDF($custom_object, 'CustomPdf', Context::getContext()->smarty, $orientation);
        $pdf->render();
    }
}

编辑: 这是我的 PDFGenerator.php 覆盖。我应该把它放在 root/override/classes/pdf 还是 my_module/override/classes/pdf 中?

<?php
class PDFGenerator extends PDFGeneratorCore
{
    /**
     * @param bool $use_cache
     * @param string $orientation
     */
    public function __construct($use_cache = false, $orientation = 'L')
    {
        parent::__construct($orientation, 'mm', 'A4', true, 'UTF-8', $use_cache, false);
        $this->setRTL(Context::getContext()->language->is_rtl);
    }

    /**
     * Write a PDF page
     */
    public function writePage()
    {
        if(!$this->header){
       $this->SetHeaderMargin(0);
       }
       else{
           $this->SetHeaderMargin(5);
       }

       if(!$this->footer){
           $this->SetFooterMargin(0);
       }
       else{
           $this->SetFooterMargin(21);
       }

       $this->setMargins(10, 10, 10);
       $this->AddPage();
       $this->writeHTML($this->content, true, false, true, false, '');
    }
}

我试过 1.7.2 版,文件属性提到 Producer:TCPDF 6.2.12 (http://www.tcpdf.org)。此外,class 函数 render() 中的 Pdf 为:

$this->pdf_renderer->createHeader($template->getHeader());
$this->pdf_renderer->createFooter($template->getFooter());

因此,最好的方法是 class HTMLTemplateCustomPdf 将函数 getHeader() 和 getFooter() 包含到 return false(或空),否则它将使用 HTMLTemplateCore 中的函数。

在 PDFGenerator 的覆盖中,您可以执行如下操作:

public function writePage()
{
    if(!$this->header){
        $this->SetHeaderMargin(0);
    }
    else{
        $this->SetHeaderMargin(5);
    }

    if(!$this->footer){
        $this->SetFooterMargin(0);
    }
    else{
        $this->SetFooterMargin(21);
    }

    $this->setMargins(10, 40, 10);
    $this->AddPage();
    $this->writeHTML($this->content, true, false, true, false, '');
}

如果需要,您还可以在 $this->setMargins(10, 40, 10);

中设置不同的边距