PDF in Prestashop

PDF in Prestashop

To generate PDF documents Prestashop uses two libraries: FPDF and TCPDF.

FPDF library possesses the following attributes:

  • Choice of measure unit, page format and margins
  • Page header and footer management
  • Automatic page break
  • Automatic line break and text justification
  • Image support (JPEG, PNG and GIF)
  • Colors
  • Links
  • TrueType,
  • Type1 and encoding support
  • Page compression

You can find more information about FPDF on the official php-library page.

TCPDF library has the following features:

  • no external libraries are required for the basic functions;
  • all standard page formats, custom page formats, custom margins and units of measure;
  • UTF-8 Unicode and Right-To-Left languages;
  • TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 and CID-0 fonts;
  • font subsetting;
  • methods to publish some XHTML + CSS code, Javascript and Forms;
  • images, graphic (geometric figures) and transformation methods;
  • supports JPEG, PNG and SVG images natively, all images supported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM) and all images supported via ImagMagick (http: www.imagemagick.org/www/formats.html)
  • 1D and 2D barcodes: CODE 39, ANSI MH10.8M-1983, USD-3, 3 of 9, CODE 93, USS-93, Standard 2 of 5, Interleaved 2 of 5, CODE 128 A/B/C, 2 and 5 Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state Customer Code), CBC (Customer Bar Code), KIX (Klant index – Customer index), Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11, PHARMACODE, PHARMACODE TWO-TRACKS, Datamatrix ECC200, QR-Code, PDF417;
  • ICC Color Profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;
  • automatic page header and footer management;
  • document encryption up to 256 bit and digital signature certifications;
  • transactions to UNDO commands;
  • PDF annotations, including links, text and file attachments;
  • text rendering modes (fill, stroke and clipping);
  • multiple columns mode;
  • no-write page regions;
  • bookmarks and table of content;
  • text hyphenation;
  • text stretching and spacing (tracking/kerning);
  • automatic page break, line break and text alignments including justification;
  • automatic page numbering and page groups;
  • move and delete pages;
  • page compression (requires php-zlib extension);
  • XOBject templates;
  • PDF/A-1b (ISO 19005-1:2005) support.

These libraries can be found in the folder with all other tools.

Usually, Prestashop generates PDF documents only for orders, invoices, refunds and other documents. For each type of document it uses its own class. All these classes are located in the directory classes/pdf.

1

Standard types of pdf-documents:

Each constant contains the template name of a particular class. All these classes are inherited from the abstract class HTMLTemplаte . This class is responsible for generating a document header, footer, store logo and address.

Templates of standard pdf-documents are located in the folder pdf :

2

Document content is generated with the help of HTML, which is generated via Smarty.

So, let us try to generate a PDF-document.

To do this we need to create a pdf-class. Let’s call it HTMLTemplateCustomPdf and put it into our module folder. As a result, we get a HTMLTemplateCustomPdf.php file with the following content:

In this class there are implemented the methods which are responsible for specific parts of a pdf document.

Next we need to create templates for each part of a pdf document:

сustоm_templаte_ * .tpl

These templates we also need to put into the folder with our module.  In these templates you can use any html and pull data from Smarty, which have been transferred in the following code:

So, to generate a document, use the following code:

Once the document is ready your browser will offer to download it.



15 comments

  1. Trying to follow your tutorial but can’t seem to get it to work. Could you please assist?

  2. Hello,

    I’m sorry for my english (I’m French), I hope you can understand me :)

    I’m a newbie in PrestaShop (3-4 weeks using it) and I hope somebody can help me :)

    I’m trying to generate a pdf file but I don’t understand where (in which file and where in this file) to put this code :

    require_once _PS_MODULE_DIR_ . ‘my_module/HTMLTemplateCustomPdf.php’;
    $pdf = new PDF($custom_object, ‘CustomPdf’, Context::getContext()->smarty);
    $pdf->render();

    Everything else seems to be ok.

    I tried in the hook that displays my tpl file and in the tpl file (using {php} … {php})

    Thanks for reading me.

  3. Hello,

    Thank you for this turorial, it’s very useful.

    Do you know how to create a PDF which return just a link and user can click to download it later ?

    I tried to use a lot of option available in tcpdf but it did not work ($pdf->render(‘F’) ….) :(

    Thank you

  4. Tom,

    You need to create a controller, which will generate pdf by link. Take the controller PdfInvoiceController as an example. Links for this controller are generated as follows:

  5. Hi Denis,

    first of all thanks for this article which helped me a lot with a functionnality i had to implement for a client.

    I have a really simple question regarding page numbers in footer. Where should i start with this, as per usual there’s no documentation in prestashop.

    Thank you for your time,

    Stefan.

  6. Nice tutorial it works for me thank you.
    Just one more thing, where do i put the css file for the html in сustоm_templаte_ * .tpl ?

  7. Hi,
    Do I need to make any configuration changes in order to generate PDFs in back office.?
    I am newbie to prestashop and unable to generate PDFs when clicked on generate pdf link under orders. The page just keeps on loading but nothing comes up.
    Appreciate if some one helps me out.
    Thanks

  8. Hi, I try to use this tuts in BackEnd, and I have some problem, when i create link in form.tpl (in my module catalog) and then i click this link, prestashop show me informations, that Controller not found…

  9. Good Morning,

    I need to generate the delivery ship document in horizontal mode (not vertical as now).
    How can I modify it? I fund delivery-slip.tpl but I can not find where to modifiy the file.
    Thanks.

  10. Hi Denis,

    nice article, I found interesting things for my project. I have one question :
    Do you tried to generate a barcode on invoice ? I didn’t found a good way to do this. I can generate a barcode everywhere on the site (with TCPDF class) but not in a pdf file. An idea maybe ?
    Thanks

    Regards

  11. Hello,

    I followed this, and I understood the custom class part
    But have one query, what is the custom object sent to PDF class?
    And where do we use the class extended from HTMLTemplate?

    Thanks,
    Gauri

Post a new comment

top