【问题标题】:Hide header and footer in custom pdf Prestashop在自定义 pdf Prestashop 中隐藏页眉和页脚
【发布时间】:2018-08-17 21:32:47
【问题描述】:

我在 Prestashop 1.7.4.1 的模块中创建了打印自定义 pdf 的方法。一切正常,但它打印带有商店徽标的页眉和页脚,每页上都有有关电子发票的信息。如果我的模板占用打印页面的所有大小,我该如何隐藏它们?

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

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

这是我的课:

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, '');
    }
}

【问题讨论】:

    标签: pdf-generation prestashop prestashop-1.7


    【解决方案1】:

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

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

    因此,最好的方法是在您的类 HTMLTemplateCustomPdf 中包含函数 getHeader() 和 getFooter() 以返回 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); 中设置不同的边距

    【讨论】:

    • 好的,但现在我在文档顶部有很大的空白。我接受这个答案,但也许你现在知道如何删除这个边距了。
    • 我想知道是否会有余量。我认为您需要覆盖 pdfgenerator writePage() 以设置不同的边距。
    • 好的,我找到了如何覆盖类,但我不明白覆盖。你能向我解释一下如何在我的自定义 pdf 中使用它吗?覆盖这个类后我要做什么?
    • @pelkas13 我添加了覆盖示例。让我知道它是否按您的需要工作。
    • 你能检查我的编辑吗?我试图将此文件放入 my_module/override/classes/pdf 但似乎我的模块无法正常工作。打印方向也不是我想要的横向。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    相关资源
    最近更新 更多