Search code examples
prestashoptcpdfprestashop-1.6

Confused about prestashop PDF generated process. Where is the code to handle the logic?


I want to deep understand prestahop inside structure and amend some parts. I am stoped in the PDF. I want can't find the controller used to handle the AdminPdf and generateDeliverySlipPDF

    {if $order->delivery_number}
      <a class="btn btn-default _blank" href="{$link->getAdminLink('AdminPdf')|escape:'html':'UTF-8'}&amp;submitAction=generateDeliverySlipPDF&amp;id_order={$order->id}">
      <i class="icon-truck"></i>
    </a>
   {/if}

Who can help me figure out the inside processes? I can't find the methods to handle generateDeliverySlipPDF.


Solution

  • AdminPdfController is located at /controllers/admin/AdminPdfController.php.

    The submitAction=generateDeliverySlipPDF part of the url will call the method processGenerateDeliverySlipPDF() inside this controller.

    Here is this method:

    public function processGenerateDeliverySlipPDF()
    {
        if (Tools::isSubmit('id_order')) {
            $this->generateDeliverySlipPDFByIdOrder((int)Tools::getValue('id_order'));
        } elseif (Tools::isSubmit('id_order_invoice')) {
            $this->generateDeliverySlipPDFByIdOrderInvoice((int)Tools::getValue('id_order_invoice'));
        } elseif (Tools::isSubmit('id_delivery')) {
            $order = Order::getByDelivery((int)Tools::getValue('id_delivery'));
            $this->generateDeliverySlipPDFByIdOrder((int)$order->id);
        } else {
            die(Tools::displayError('The order ID -- or the invoice order ID -- is missing.'));
        }
    }
    

    In this Controller you'll find other methods as this one to generate Invoices, Order, ... and other PDFs.

    Feel free to ask if you need more informations.


    EDIT:

    If you want to change format in a proper way you'll have to override those classes:

    /override/classes/pdf/PDFGenerator.php:

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

    /override/classes/pdf/PDF.php:

    <?php
    
    /**
     * @since 1.5
     */
    class PDF extends PDFCore
    {
    
        /**
         * @param $objects
         * @param $template
         * @param $smarty
         * @param string $orientation
         */
        public function __construct($objects, $template, $smarty, $orientation = 'P', $format = 'A4')
        {
            parent::__construct($objects, $template, $smarty, $orientation);
            $this->pdf_renderer = new PDFGenerator((bool)Configuration::get('PS_PDF_USE_CACHE'), $orientation, $format);
        }
    }
    

    /override/controllers/admin/AdminPdfController.php:

    <?php
    
    class AdminPdfController extends AdminPdfControllerCore
    {
        public function generatePDF($object, $template)
        {
            switch($template) {
                case PDF::TEMPLATE_DELIVERY_SLIP:
                    $format = array(210, 50000); // Replace with your desired size
                    break;
                default:
                    $format = 'A4';
            }
    
            $pdf = new PDF($object, $template, Context::getContext()->smarty, 'P', $format);
            $pdf->render();
        }
    }
    

    Now you can specify the format for each PDF. You will find informations about $format at this place

    This code has not been tested but should work as expected. Let me know if you encounter any issue.

    You will need to delete /cache/class_index.php after adding those overrides to clear Prestashop internal classes path cache.