How to call tcpdf non-static method in tpl file

蓝咒 提交于 2020-06-17 14:03:08

问题


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 .


回答1:


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)



来源:https://stackoverflow.com/questions/57582078/how-to-call-tcpdf-non-static-method-in-tpl-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!