Get Free Audit

PDF in Prestashop

Nov 13, 2013

11161 Alex Simonchik

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:

Andrey_Dubina
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey


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.

Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

36 Comments

  1. I have made my own controller which prepare everything ready to build pdf. and i use ajax to call my controller and my controller will build pdf.

    erverything looking good until new pdf() class was call after that i do the simple things with $pdf->render() as normal. but nothing happen.

    pdf was not build and download. please guide me

    sorry for my english
    thank in advance

  2. Hi, Jibe!
    We are happy to hear that our tutorial is helpful!

    There are three possible causes for your problem:
    1) code how you pass the values to custom_object variable;
    2) code how you call this value in your .tpl file;
    3) content what you pass to the custom_object variable (you can see content using |print_r for example).

    But this question is really tough to answer it in the comments. Please contact our support team for help: [email protected]

  3. Thank you for this tutorial, it is working great even for PS 1.7.

    However I encounter an issue with the variables contains in $custom_object. When I render the content of the variable in the tpl file (it is a string), it only displays the first letter. Any idea why ?

  4. Hi, Lukasz

    it’s really tough to answer such questions in the comments. Please contact our support for help.

  5. Hi, is it possible to remove header and footer from my custom pdf?

    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);

    This code from tcpdf docs doesn’t work.

  6. Hi David,

    It’s hard to write a full explanation here, please get in touch with @valeria from our Support Team to get a qualified help from a developer.

  7. Hi, another question from myself… I am trying to create PDF generator to match new GDPR policy and I am almost there, however I am struggling with the constructor. In HTMLTemplateCustomPdf.php it is:

    public function __construct($custom_object, $smarty)

    but I need to add another variable – $customer_id – to that constructor, however I don’t get where that constructor is called and how to change it. I’ve tried to find that in other pdf classes, however they all have 3 arguments and seem to call the same constructor of pdf.php class. Is there any way this:

    $pdf = new PDF($custom_object, ‘CustomPdf’, Context::getContext()->smarty);
    $pdf->render();

    can be changed so my class receives $customer_id variable?

    Kind regards

  8. I see now. It’s all in HTMLTemplate.php. getLogo() should actually return $logo path, and there’s no logo.tpl file required for this to work.

    Thank you for your help.

  9. Hi Alex,

    Thank you very much for your help. Forgot about those includes. Much better with:

    include_once(‘../../config/config.inc.php’);
    include_once(‘../../init.php’);
    require_once(_PS_ROOT_DIR_.’/classes/pdf/HTMLTemplate.php’);

    I’ve got another problem now, with logo to be particular. In my getLogo() method I have logo variable in smarty array assigned, then in _logo.tpl file:

    This is logo

    ,But it doesn’t show anything. Other parts of the .pdf document are fine, however cannot handle the logo. It looks to me like no matter if I edit logo.tpl file or not, there are no changes.

    How do I include logo in my .pdf file?

    Many thanks for your time.
    Kind regards

  10. Hi David,

    You need to be sure that your HTMLTemplateCustomPdf.php includes the ‘HTMLTemplate’ class, using include or autoloader statement.

  11. Hello,

    I did first few steps of the tutorial, so created HTMLTemplateCustomPdf.php and inserted it into /modules/mymodule folder, however when I am trying to call it from mydomain/modules/mymodule/HTMLTemplateCustomPdf

    The error is: Fatal error: Class ‘HTMLTemplate’ not found

    What should I do to load the core prestashop HTML template class?

    Thank you.
    Kind regards

  12. Dear Iryna,

    Thank you very much for your support.
    We took it out of modules section and it works like it should now. Maybe not perfect but it is working kindly.

    PS: a not on ‘Resource interpreted as Document but transferred with MIME type application/pdf’ would be probably nice too. I don’t know whether this was a topic 4 years ago.

    Best wishes and have a nice time,
    Priska

  13. Hello, Priska!

    You have a strong point here, the information is not actual for the latest Prestashop versions indeed. As you can see, the article was written 4 years ago.
    I added this topic to our content plan, we will try to cover it later.

    Apologize for the inconvenience!
    Iryna

  14. Dear Iryna,

    Thank you so much for your reply.
    This would meen to me that your above solution is not working for Prestashop 1.7.2.1 or at least not without any amendmend from programmers (although I learnt to programm C++, JAVA, PERL and other languages). That’s a shame. Wouldn’t it be usful to take this article of as it’s not working any longer on newer Prestashop versions?

    We’ll see what we’ll do as this is a point of money.

    Best wishes,
    Priska

  15. Dear Priska,

    The issue is pretty complicated. Unfortunately, it’s not possible to provide a qualitative answer without getting into details.
    Please contact our support department in case you need an individual consideration of the matter: [email protected]
    We’ll be happy to assist.

    Regards,
    Iryna

  16. Dear Iryna,

    I would need small assitance from you or your team. I have followed the instructions and created own pdf. What I do not understand is, why Prestashop is in BO trying to load my PDF file. I get an error ‘Container.php line 117: Cannot build a htmltemplatecustompdf.’

    I do not understand why Prestashop is trying to read it but it seems as it’s trying to render it to BO (at least under 1.7.2.1) because of modules.php. Do you have a clue or could you ask your colleagues? I would need assitance here as it’s preventing access due to the error from several BO navigation parts.

    Kind regards,
    Priska

  17. 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

  18. 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

  19. 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.

  20. 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…

  21. 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

  22. 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 ?

  23. 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.

  24. 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:

  25. 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

  26. 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.

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

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *