Reading list Switch to dark mode

    Custom PDF in prestashop using TCPDF library

    Updated 15 July 2016

    Custom PDF

    Custom PDF

    Create custom PDF in prestashop using prestashop library which they have used to render PDF templates like Invoice, Delivery slip and others.

    Right, now there is no such way to create your own PDF documented file in prestashop because prestashop has created predefined templates which they render whenever it calls. Even you can not easily edit that templates with help of third party modules. If you need to edit that template you have to edit in core files of prestashop.

    Lets see what is the process or classes which will be needed when you create your own custom PDF document in prestashop.

    Prestashop is using TCPDF library to render all the pdf content. For information about tcpdf, you can check tcpdf official website. They have used many examples to render PDF in php. Visit tcpdf . But prestashop has extended this library and customized this library accordingly and created a class PDFGenerator. This class have various methods to create content like footer content, header content, body content and others.

    Lets start with code step by step.

    Searching for an experienced
    Prestashop Company ?
    Find out More

    Step : 1) If you want to create custom PDF document then you need to extend HTMLTemplate class in your own class.

    In this class, you can see three abstract method defined, which you need to use in your own custom class, if you don’t use them, then it will throw error.

    • abstract public function getContent()
    • abstract public function getFilename()
    • abstract public function getBulkFilename()

    Step : 2) After defining all three method in your own class, you can use other methods which are defined in HTMLTemplate, like

    getHeader() for setting header content,

    getFooter() for setting footer content,

    getContent() for setting main content of the body,

    Step : 3) Now, you have all the content in your own methods. Now you need to put content on tpl files.

    Lets take an example to explain, how to put content on tpl file.

    public function getHeader()
    {
         //---------------------------------------------------
         $this->context->smarty->assign(array(
             'header' => self::l('Invoice'),
             )
         );
         return $this->context->smarty->fetch($this->getTemplate('sellerinvoice_header'));
    }

    we have getHeader(), function in our custom class. we have assign Invoice text in header variable which will be used in .tpl file. In last line, we are returning the tpl file using fetch method of smarty. getTemplate() is a custom function used to set path to tpl file. By this way, we can set the content on tpl file.

    Step : 4) We have set the content in our header, footer and content. Now we need to render our PDF document by using PDFGenerator class.

    In PDFGenerator, there are various methods which used to create the content, write the content then render the content.

    • writePage() for writing the pdf file.
    • createHeader() for creating the header content.
    • createFooter() for creating the footer content.
    • createContent() for creating the main body content.
    • setFontForLang() for setting the font language. If you don’t do this, then it will create some issue while rendering the content like missing special characters, not supported currency symbol n others.

    These are the steps which will help you to create your own PDF file in prestashop.

    Example code –

    $orientation = 'P';   // p for portrait view
    $file_attachement = array();
    $this->pdf_renderer = new PDFGenerator((bool) Configuration::get('PS_PDF_USE_CACHE'), $orientation);
    $obj_seller = new SellerInvoice();  // custom class
    $this->pdf_renderer->setFontForLang(Context::getContext()->language->iso_code); // setting lang
    $this->pdf_renderer->createHeader($obj_seller->getHeader()); // creating header content
    $this->pdf_renderer->createFooter($obj_seller->getFooter()); // creating footer content
    $this->pdf_renderer->createContent($obj_seller->getContent()); // creating body content
    $this->pdf_renderer->writePage(); // writing pdf page
    $file_attachement['content'] = $this->pdf_renderer->render($obj_seller->getFilename(), false);
    $file_attachement['name'] = $obj_seller->getFilename(); // getting pdf file name
    $file_attachement['invoice']['mime'] = 'application/pdf';
    $file_attachement['mime'] = 'application/pdf';
    
    

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • Emanuel Schiendorfer
    • Deepak Bharti
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home