如何在tpl文件中调用tcpdf非静态方法

How to call tcpdf non-static method in tpl file

我在 Prestashop 1.7 的送货单模板中生成条形码时遇到问题。6.x(或仅 1.7)使用我在 Prestashop 1.6 中使用的相同模板

当我通过 它给我错误使用“$this when not in object context” 这是通过 Symfony 调试器的堆栈跟踪

Symfony\Component\Debug\Exception\FatalThrowableError:
Using $this when not in object context

 at vendor/tecnickcom/tcpdf/tcpdf.php:17060
 at TCPDF::serializeTCPDFtagParameters(array('6856616461MA', 'C39', '70.2', '34.5', '74.8', '21.3', '0.4', array('position' => 'R', 'label' => '*6856616461MA*', 'border' => true, 'padding' => 3, 'fgcolor' => array(0, 0, 0), 'bgcolor' => array(255, 255, 255), 'text' => true, 'font' => 'helvetica', 'fontsize' => 12, 'stretchtext' => 4), 'N'))
    (var/cache/dev/smarty/compile/ea/e9/5b/eae95be0cc705554fab57fc3b0d03cf1e7597307_0.file.delivery-slip.tpl.php:31)
 at content_5d5c68664b85e2_58996534(object(SmartyCustomTemplate))
    (vendor/smarty/smarty/libs/sysplugins/smarty_template_resource_base.php:123)
 at Smarty_Template_Resource_Base->getRenderedTemplateCode(object(SmartyCustomTemplate))
    (vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:114)
 at Smarty_Template_Compiled->render(object(SmartyCustomTemplate))
    (vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:216)
 at Smarty_Internal_Template->render(false, 0)
    (vendor/smarty/smarty/libs/sysplugins/smarty_internal_templatebase.php:232)
 at Smarty_Internal_TemplateBase->_execute(object(SmartyCustomTemplate), null, null, null, 0)
    (vendor/smarty/smarty/libs/sysplugins/smarty_internal_templatebase.php:116)
 at Smarty_Internal_TemplateBase->fetch('/home/data/data/pdf/delivery-slip.tpl', null, null, null, false, true, false)
    (classes/Smarty/SmartyCustom.php:112)
 at SmartyCustomCore->fetch('/home/data/data/data/delivery-slip.tpl')
    (classes/pdf/HTMLTemplateDeliverySlip.php:139)
 at HTMLTemplateDeliverySlipCore->getContent()
    (classes/pdf/PDF.php:128)
 at PDFCore->render()
    (override/controllers/admin/AdminPdfController.php:18)
 at AdminPdfController->generatePDF(object(PrestaShopCollection), 'DeliverySlip')
    (controllers/admin/AdminPdfController.php:170)
 at AdminPdfControllerCore->generateDeliverySlipPDFByIdOrder(2837)
    (controllers/admin/AdminPdfController.php:82)
 at AdminPdfControllerCore->processGenerateDeliverySlipPDF()
    (classes/controller/AdminController.php:988)
 at AdminControllerCore->postProcess()
    (controllers/admin/AdminPdfController.php:30)
 at AdminPdfControllerCore->postProcess()
    (classes/controller/Controller.php:280)
 at ControllerCore->run()
    (classes/Dispatcher.php:515)
 at DispatcherCore->dispatch()
    (adminfolder/index.php:97)

pdf/delivery-slip.tpl

{$style_tab}
{assign var=nejma value="*"}
{assign var=code value="`$nejma``$order->shipping_number``$nejma`"}
{assign var=black value=[0,0,0]}{assign var=white value=[255,255,255]}
{assign var=stuff value=['position'=>'R','label'=>$code, 'border'=>true, 'padding'=>3, 'fgcolor'=>$black, 'bgcolor'=>$white, 'text'=>true, 'font'=>'helvetica', 'fontsize'=>12, 'stretchtext'=>4]}
{assign var=params value=TCPDF::serializeTCPDFtagParameters($order->shipping_number, 'C39', '70.2', '34.5', '74.8', '21.3', '0.4', $stuff, 'N')}

<tcpdf method="write1DBarcode" params="{$params}"/>
{* {$bc} *}
<table border="1">
        {* logo *}
   <tr> 
      <th width="185" height="72" colspan="2" rowspan="2" >
         <div class="center" >{if $logo_path}
            <img src="{$logo_path}" style="width:auto; height:72px;" />
            {/if}
         </div>
      </th>
      <th class="header" width="71" height="42">
         <div class="center" >MONTANT CRBT</div>
      </th>
      <th class="header" width="71" height="42">
         <div class="center">CCP N°</div>
      </th>
      <th class="header" width="71" height="42">
         <div class="center">CODE DE COMMANDE</div>
      </th>
   </tr>
        {* beside logo (price ref ...) *}
    <tr>
        <td width="71" height="29" >
            <div class="center">{displayPrice currency=$order->id_currency price=$order->total_paid-$order->total_paid_real|string_format:"%.2f"}</div>
        </td>
        <td width="71" height="29">
            <div class="center">6571486/V</div>
        </td>
        <td width="71" height="29">
            <div class="center">{$order->getUniqReference()}</div>
        </td>
    </tr>
        {* ligne expediteur et code a barre *}
    <tr>
        <th class="header" width="185" height="10" >EXPEDITEUR</th>
        <th class="header" width="213" height="10">CODE A BARRE</th>
    </tr>
    <tr>
        <td class="center" width="185" height="80">{$shop_address}<br/>{$shop_phone}</td>
        {* <td  width="213" height="49" colspan="3"></td> *}
    </tr>
        {* destinataire date d'exp val poids *}
    <tr>
        <th class="header" width="185" height="10">DESTINATAIRE</th>
        <th class="header" width="91" height="10" >DATE D'EXPEDITION</th>
        <th class="header" width="71" height="10" >VALEUR</th>
        <th class="header" width="51" height="10" >POIDS</th>
    </tr>
    <tr>
      <td width="185" rowspan="2" class="center">{$delivery_address}</td>
      <td width="91" height="30"></td>
      <td width="71" height="30" class="header" >{displayPrice currency=$order->id_currency price=$order->total_products}</td>
      <td width="51" height="30"></td>
    </tr>
   <tr>
        <td width="213" height="62">
            <span class="left"> Instructions particulière de l'expediteur:</span><br/>
            {* <span class="bold"> {l s='□' pdf='true'} Remettre l'envoi au destinataire en personne.</span><br/> *}
            <span class="bold center">Livrer l'envoi à l'adresse.</span>
        </td>
   </tr>
</table>

override\classes\pdf\PDFGenerator.php

<?php

class PDFGenerator extends PDFGeneratorCore
{

    /**
     * @param bool $use_cache
     * @param string $orientation
     * @param string $format
     */

    public function __construct($use_cache = false, $orientation = 'P', $format = 'A5')
    {
        TCPDF::__construct($orientation, 'mm', $format, true, 'UTF-8', $use_cache, false);
        $this->setRTL(Context::getContext()->language->is_rtl);

    }
    public function writePage()
    {
        $this->SetHeaderMargin(3);
        $this->SetFooterMargin(3);
        $this->setMargins(3, 3, 3);
        $this->AddPage();
        $this->writeHTML($this->content, true, false, true, false, '');
        $this->output('document.pdf','I');
    }
}

override\classes\pdf\PDF.php

<?php

class PDF extends PDFCore
{

    public function __construct($objects, $template, $smarty, $orientation = 'P', $format = 'A5')
    {
        parent::__construct($objects, $template, $smarty, $orientation);
        $this->pdf_renderer = new PDFGenerator((bool)Configuration::get('PS_PDF_USE_CACHE'), $orientation, $format);
        $this->template = $template;
                /*
         * We need a Smarty instance that does NOT escape HTML.
         * Since in BO Smarty does not autoescape
         * and in FO Smarty does autoescape, we use
         * a new Smarty of which we're sure it does not escape
         * the HTML.
         */
        $this->smarty = clone $smarty;
        $this->smarty->escape_html = false;

        /* We need to get the old instance of the LazyRegister
         * because some of the functions are already defined
         * and we need to check in the old one first
         */
        $original_lazy_register = SmartyLazyRegister::getInstance($smarty);

        /* For PDF we restore some functions from Smarty
         * they've been removed in PrestaShop 1.7 so
         * new themes don't use them. Although PDF haven't been
         * reworked so every PDF controller must extend this class.
         */
        smartyRegisterFunction($this->smarty, 'function', 'convertPrice', array('Product', 'convertPrice'), true, $original_lazy_register);
        smartyRegisterFunction($this->smarty, 'function', 'convertPriceWithCurrency', array('Product', 'convertPriceWithCurrency'), true, $original_lazy_register);
        smartyRegisterFunction($this->smarty, 'function', 'displayWtPrice', array('Product', 'displayWtPrice'), true, $original_lazy_register);
        smartyRegisterFunction($this->smarty, 'function', 'displayWtPriceWithCurrency', array('Product', 'displayWtPriceWithCurrency'), true, $original_lazy_register);
        smartyRegisterFunction($this->smarty, 'function', 'displayPrice', array('Tools', 'displayPriceSmarty'), true, $original_lazy_register);
        smartyRegisterFunction($this->smarty, 'modifier', 'convertAndFormatPrice', array('Product', 'convertAndFormatPrice'), true, $original_lazy_register); // used twice
        smartyRegisterFunction($this->smarty, 'function', 'displayAddressDetail', array('AddressFormat', 'generateAddressSmarty'), true, $original_lazy_register);
        smartyRegisterFunction($this->smarty, 'function', 'getWidthSize', array('Image', 'getWidth'), true, $original_lazy_register);
        smartyRegisterFunction($this->smarty, 'function', 'getHeightSize', array('Image', 'getHeight'), true, $original_lazy_register);

        $this->objects = $objects;
        if (!($objects instanceof Iterator) && !is_array($objects)) {
            $this->objects = array($objects);
        }

        if (count($this->objects) > 1) { // when bulk mode only
            $this->send_bulk_flag = true;
        }

    }
}

override\controllers\admin\AdminPdfController.php

<?php

class AdminPdfController extends AdminPdfControllerCore
{
    public function generatePDF($object, $template)
    {
        switch($template) {
            case PDF::TEMPLATE_DELIVERY_SLIP:
                $format = 'A6';
                $orientation = 'L';
                break;
            default:
                $format = 'A5';
                $orientation ='P';
        }

        $pdf = new PDF($object, $template, Context::getContext()->smarty, $orientation, $format);
        $pdf->render();
    }
}

1.PS : 当我排除条形码时一切正常

2.PS2:相同的代码仍在 Prestashop 1.6 上运行,没有问题

3.PS3:我设置定义('K_TCPDF_CALLS_IN_HTML',真); // 为假,所以我可以在 tpl 文件中调用 tcpdf。

4.PS4 : tcpdf 的配置文件位于 vendor\tecnickcom\tcpdf\config\tcpdf_config.php

如有任何帮助,我们将不胜感激。

我对 smarty 不熟悉,所以以下只是一个猜测,但它可能有效...

您的文件 override\classes\pdf\PDF.php 向 smarty 添加了内容。我不太确定,如果 smarty 的版本匹配(你可能需要检查)但是 you theoretically can assign values:

 $this->smarty->assign('tcpdf', $this->pdf_renderer);

假设您的 PDFGenerator ($this->pdf_renderer) 实际上扩展了 TCPDF

如果可行,您应该能够做到:

{assign var=params value=$tcpdf->serializeTCPDFtagParameters($order->shipping_number,...)}

希望这有效 ;o)