Confused about prestashop PDF generated process. W

2019-05-07 10:36发布

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.

2条回答
迷人小祖宗
2楼-- · 2019-05-07 11:32

thank you for this topic,

I would like to ask you for the case of generating pdf in front?

i.e this is a part of history.tpl

 `{if $order.details.invoice_url}
    <a href="{$order.details.invoice_url}"><i class="material-icons">&#xE415;</i></a>`

i found $order.details.invoice_url is define in OrderPresenter.php and OrderPresenter use HistoryController to get the invoice_url.

So i take a look in historyController

if ((bool) Configuration::get('PS_INVOICE') && OrderState::invoiceAvailable($order->current_state) && count($order->getInvoicesCollection())) {
$url_to_invoice = $context->link->getPageLink('pdf-invoice', true, null, 'id_order='.$order->id);
            if ($context->cookie->is_guest) {
                $url_to_invoice .= '&amp;secure_key='.$order->secure_key;
            }
        }

getPageLink use "pdf-invoice" ,i take a look for getPageLink Method and see that "pdf-invoice" is a controller... the trouble is i don't know where is this controller? help please

查看更多
该账号已被封号
3楼-- · 2019-05-07 11:39

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.

查看更多
登录 后发表回答