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.
Standard types of pdf-documents:
1 2 3 4 5 |
const TEMPLATE_INVOICE = 'Invoice'; const TEMPLATE_ORDER_RETURN = 'OrderReturn'; const TEMPLATE_ORDER_SLIP = 'OrderSlip'; const TEMPLATE_DELIVERY_SLIP = 'DeliverySlip'; const TEMPLATE_SUPPLY_ORDER_FORM = 'SupplyOrderForm'; |
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 :
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
<?php class HTMLTemplateCustomPdf extends HTMLTemplate { public $custom_model; public function __construct($custom_object, $smarty) { $this->custom_model = $custom_object; $this->smarty = $smarty; // header informations $id_lang = Context::getContext()->language->id; $this->title = HTMLTemplateCustomPdf::l('Custom Title'); // footer informations $this->shop = new Shop(Context::getContext()->shop->id); } /** * Returns the template's HTML content * @return string HTML content */ public function getContent() { $this->smarty->assign(array( 'custom_model' => $this->custom_model, )); return $this->smarty->fetch(_PS_MODULE_DIR_ . 'my_module/custom_template_content.tpl'); } public function getLogo() { $this->smarty->assign(array( 'custom_model' => $this->custom_model, )); return $this->smarty->fetch(_PS_MODULE_DIR_ . 'my_module/custom_template_logo.tpl'); } public function getHeader() { $this->smarty->assign(array( 'custom_model' => $this->custom_model, )); return $this->smarty->fetch(_PS_MODULE_DIR_ . 'my_module/custom_template_header.tpl'); } /** * Returns the template filename * @return string filename */ public function getFooter() { return $this->smarty->fetch(_PS_MODULE_DIR_ . 'my_module/custom_template_footer.tpl'); } /** * Returns the template filename * @return string filename */ public function getFilename() { return 'custom_pdf.pdf'; } /** * Returns the template filename when using bulk rendering * @return string filename */ public function getBulkFilename() { return 'custom_pdf.pdf'; } } |
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:
1 2 3 |
$this->smarty->assign(array( 'custom_model' => $this->custom_model, )); |
So, to generate a document, use the following code:
1 2 3 |
require_once _PS_MODULE_DIR_ . 'my_module/HTMLTemplateCustomPdf.php'; $pdf = new PDF($custom_object, 'CustomPdf', Context::getContext()->smarty); $pdf->render(); |
Once the document is ready your browser will offer to download it.
Hi Mamun
thanks for your question. For now PrestaShop works with TCPDF https://github.com/PrestaShop/PrestaShop/blob/develop/classes/pdf/PDFGenerator.php#L30
There are ways to migrate from TCPDF to mPDF but it is to difficult to share them in the comment section. Please, contact [email protected] to find out more.
Hi Tom
we need more details to help you with this issue. Could you please contact [email protected]?
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
is it possible to migrate from TCPDF to mPDF?
If so then how I can achieve it?
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]
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 ?
Hi, Lukasz
it’s really tough to answer such questions in the comments. Please contact our support for help.
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.
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.
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
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.
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
Hi David,
You need to be sure that your HTMLTemplateCustomPdf.php includes the ‘HTMLTemplate’ class, using include or autoloader statement.
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
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
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
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
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
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
Hi, Priska!
Thank you for the comment!
We have a lot of tasks to do, unfortunately, sometimes we don’t manage to answer all the questions on time.
Regards,
Iryna
Hi Gauri,
That’s the class https://github.com/PrestaShop/PrestaShop/blob/1.5.0.3/classes/pdf/HTMLTemplate.php#L80 but you do not use it directly. You just call it by override (HTMLTemplateCustomPdf extends HTMLTemplate).
Cheers,
Priska
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
Thank you so much for this tutorial , really helped me :)
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
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.
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…
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
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 ?
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.
Thank you very much !
thanks it’s very helpful
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:
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
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.
Carlos,
Please describe your problem in more details.
Trying to follow your tutorial but can’t seem to get it to work. Could you please assist?