This page describes how to create your own document-generation module (for proposals, invoices, etc. ) The tutorial uses Commercial Proposals as an example but the same process is used for any type of document.
Building a PDF template requires some PHP knowledge. An ODT template does not require PHP knowledge. To build an ODT template, see this page Create an ODT document template.
You may review the existing templates by going to the module setup area, the Commercial Proposals setup in this case, and then clicking on the "preview" logo. Choose one that is the best match to your needs. In this example, we will use the template "azur" that corresponds to the file pdf_propale_azur.modules.php
All document models are in htdocs/core/modules, in subdirectories, for example propale for commercial proposals facture for invoices commandes for orders, etc. and finally doc or pdf. For instance, a customized proposal template will be located in . core/modules/propale/doc/
Do not modify an existing template. Copy the file of the template used as example into a new name in same directory than your example and edit it to change content. For this example, we will call our new template 'mycompanyblue', based on the existing template 'azur':
Test this model (see previous section) before going further.
Add into table llx_document_model the new document template that you have created.
Just use the Module Builder from v12.0 to create your module. When entering the name of a new object to create, you can also check a checkbox to say that you want to manage documents for such new object. In such a case, two templates will be automatically generated (one ODT template and one PDF template).
The templates will be generated with path:
Customize the new template:
In file pdf_propale_mycompanyblue.modules.php, look for the function '_pagehead()' function. It is the function that manages the display of the header.
Look for the '_pagefooter()' function. It is the function that manages the display of the footer.
Look for the 'write_file()' function. It is the function that manages the display of the body of PDF.
The library used to create PDF documents in PHP is called FPDF and can be found in htdocs/includes/tcpdf/tcpdf.class.php (or htdocs/includes/tecnickcom/tcpdf/tcpdf.class.php). This class contains the methods used to generate different parts of documents.
Templates instantiate the FPDF class and use its methods combined with the specific data of the invoice, order etc.
We can generally find the following calls into templates that generate PDF documents:
The PHP script used for generating PDF documents had the following methods (taking as example the model "crabe") inside the class with the model's name:
$pdf->Image('\www\htdocs\dolibarr\document\societe\logo.jpg', 10, 5, 60.00);
With this example: 10=abscissa, 5=ordinate, 60=width of logo
Most common function to use
$pdf->setX(float a); // set current x position $pdf->setY(float b); // set current y position $pdf->setXY(float a,float b); // fixe les positions x et y courantes $pdf->SetTextColor(0,0,200); // fixe la couleur du texte $pdf->SetFont('Arial','B',14); // fixe la police, le type ( 'B' pour gras, 'I' pour italique, '' pour normal. ) $pdf->MultiCell(60, 8, 'Mon texte", 0, 'L'); // imprime 'Mon texte' avec saut de ligne
Note: Origin for setXY functions are the top left corner of page.
Left side of the page around the middle:
$pdf->Annotation(102, 202, 10, 10, "this is the comment text", array('Subtype'=>'Text', 'Name' => 'Comment', 'T' => 'this is comment author', 'Subj' => 'this is comment title', 'C' => array(255, 255, 0)));
Right side of the page around the middle:
$pdf->Annotation(202, 102, 10, 10, "this is the comment text", array('Subtype'=>'Text', 'Name' => 'Comment', 'T' => 'this is comment author', 'Subj' => 'this is comment title', 'C' => array(255, 255, 0)));
A dedicated page give you some advises how to integrate complementary attributes (extrafields) into PDF models of Dolibarr Add Extrafields on PDF Models
In page Home => Setup => Modules =>
This is probably a font problem, the current font used for generating the PDF cannot handle the foreign characters you are trying to print. So just try to use another font.
You will find the fonts used by Dolibarr to print PDF inside your Dolibarr folder. /includes/tecnickcom/tcpdf/fonts/
To change the font used for generating PDF, you need to edit the 'main.lang' file of the language you are using (you'll find the file in your Dolibarr. /langs/en_US/main.lang for instance if you use English)
If you are unlucky, you can try with more fonts, just go to the TCPDF website sourceforge.net/projects/tcpdf/files/ and download the latest zip package. Then extract only the 'fonts' folder from the zip. Then copy the files in your Dolibarr installation . /includes/tecnickcom/tcpdf/fonts/ so you will get more fonts to play with.
Also, please make a backup of your files modification.
Alternative if this doesn't work: make sure the Translation class is correctly instantiated and loaded with the correct language inside your PDF templates (stored inside the $langs or $outputlangs variable).
Thanks to Humphrey for the tip.