如何在tpl文件中调用tcpdf非静态方法 [英] How to call tcpdf non-static method in tpl file

查看:112
本文介绍了如何在tpl文件中调用tcpdf非静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Symfony\Component\Debug\Exception\FatalThrowableError:不在对象上下文中时使用 $this在供应商/tecnickcom/tcpdf/tcpdf.php:17060在 TCPDF::serializeTCPDtagParameters(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)在 content_5d5c68664b85e2_58996534(对象(SmartyCustomTemplate))(供应商/smarty/smarty/libs/sysplugins/smarty_template_resource_base.php:123)在 Smarty_Template_Resource_Base->getRenderedTemplateCode(object(SmartyCustomTemplate))(供应商/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:114)在 Smarty_Template_Compiled->render(object(SmartyCustomTemplate))(供应商/smarty/smarty/libs/sysplugins/smarty_internal_template.php:216)在 Smarty_Internal_Template->render(false, 0)(供应商/smarty/smarty/libs/sysplugins/smarty_internal_templatebase.php:232)在 Smarty_Internal_TemplateBase->_execute(object(SmartyCustomTemplate), null, null, null, 0)(供应商/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)(类/Smarty/SmartyCustom.php:112)在 SmartyCustomCore->fetch('/home/data/data/data/delivery-slip.tpl')(classes/pdf/HTMLTemplateDeliverySlip.php:139)在 HTMLTemplateDeliverySlipCore->getContent()(classes/pdf/PDF.php:128)在 PDFCore->render()(覆盖/控制器/管理员/AdminPdfController.php:18)在 AdminPdfController->generatePDF(object(PrestaShopCollection), 'DeliverySlip')(controllers/admin/AdminPdfController.php:170)在 AdminPdfControllerCore->generateDeliverySlipPDFByIdOrder(2837)(controllers/admin/AdminPdfController.php:82)在 AdminPdfControllerCore->processGenerateDeliverySlipPDF()(类/控制器/AdminController.php:988)在 AdminControllerCore->postProcess()(控制器/管理员/AdminPdfController.php:30)在 AdminPdfControllerCore->postProcess()(类/控制器/Controller.php:280)在 ControllerCore->run()(类/Dispatcher.php:515)在 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'=>$黑色,'bgcolor'=>$white,'text'=>true,'font'=>'helvetica','fontsize'=>12,'stretchtext'=>4]}{assign var=params value=TCPD::serializeTCPDtagParameters($order->shipping_number, 'C39', '70.2', '34.5', '74.8', '21.3', '0.4', $stuff, 'N')}<tcpdf method="write1DBarcode" params="{$params}"/>{* {$bc} *}<表格边框=1">{* 商标 *}<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;"/>{/如果}

</th><th class="header" width="71" height="42"><div class="center" >MONTANT 彩铃</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>{* 旁边的标志(价格参考...)*}<tr><td width="71" height="29" ><div class="center">{displayPricecurrency=$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>{* 目的地日期 d'exp val poids *}<tr><th class="header" width="185" height="10">目的地</th><th class="header" width="91" height="10" >DATE D'EXPEDITION</th><th class="header" width="71" height="10" >VALEUR<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" >{displayPricecurrency=$order->id_currency price=$order->total_products}</td><td width="51" height="30"></td></tr><tr><td width="213" height="62"><span class="left">特殊说明:</span><br/>{* {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>

覆盖\classes\pdf\PDFGenerator.php

setRTL(Context::getContext()->language->is_rtl);}公共函数 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');}}

覆盖\类\pdf\PDF.php

pdf_renderer = new PDFGenerator((bool)Configuration::get('PS_PDF_USE_CACHE'), $orientation, $format);$this->template = $template;/** 我们需要一个不会转义 HTML 的 Smarty 实例.* 因为在 BO Smarty 中不会自动转义* 在 FO Smarty 中自动转义,我们使用* 一个我们确信它不会逃脱的新 Smarty* HTML.*/$this->smarty = 克隆 $smarty;$this->smarty->escape_html = false;/* 我们需要获取 LazyRegister 的旧实例* 因为有些函数已经定义了* 我们需要先检查旧的*/$original_lazy_register = SmartyLazyRegister::getInstance($smarty);/* 对于PDF,我们从Smarty恢复了一些功能* 它们已在 PrestaShop 1.7 中删除,因此* 新主题不使用它们.虽然 PDF 还没有* 重新设计,因此每个 PDF 控制器都必须扩展此类.*/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);//使用了两次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) {//仅当批量模式时$this->send_bulk_flag = true;}}}

覆盖\控制器\管理员\AdminPdfController.php

smarty, $orientation, $format);$pdf->render();}}

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

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

3.PS3: 我设置了define('K_TCPD_CALLS_IN_HTML', true);//是假的,所以我可以在tpl文件中调用tcpdf.

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

如有任何帮助,将不胜感激.

解决方案

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

您的文件 override\classes\pdf\PDF.php 为 smarty 添加了一些东西.我不太确定,如果 smarty 的版本匹配(您可能需要检查)但是 理论上你可以赋值:

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

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

如果可行,您应该可以:

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

希望这有效;o)

I have a problem generating Barcode in Delivery Slip template in Prestashop 1.7.6.x (or just 1.7) with the same template i was using in Prestashop 1.6

when i call the method via it give me error Using "$this when not in object context" this is the stack trace via Symfony debugger

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 : when i exclude the barcode everything works well

2.PS2: the same code is still operating on Prestashop 1.6 without Problems

3.PS3: i set define('K_TCPDF_CALLS_IN_HTML', true); //was false so i can call tcpdf in tpl file.

4.PS4 : config file of tcpdf is at vendor\tecnickcom\tcpdf\config\tcpdf_config.php

any help would be apperciated thank you .

解决方案

I'm not familiar with smarty, so the following is only a guess, but it might work...

Your file override\classes\pdf\PDF.php adds stuff to smarty. I'm not quite sure, if the version of smarty matches (you might have to check) but you theoretically can assign values:

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

under the assumption, that your PDFGenerator ($this->pdf_renderer) actually extends TCPDF.

If that works, you should be able to do:

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

hope this works ;o)

这篇关于如何在tpl文件中调用tcpdf非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆