Difference between revisions of "Create a PDF document template"

From Dolibarr ERP CRM Wiki
Jump to navigation Jump to search
(Add link to Add Extrafields on PDF Models)
Tag: 2017 source edit
Line 15: Line 15:
 
An ODT template does not require PHP knowledge. To build an ODT template, see this page [[Create an ODT document template]].  
 
An ODT template does not require PHP knowledge. To build an ODT template, see this page [[Create an ODT document template]].  
  
= Prerequisite =
+
=Prerequisite=
* Dolibarr: 3.0+
 
* Knowledge in PHP development
 
  
 +
*Dolibarr: 3.0+
 +
*Knowledge in PHP development
  
= Find a document template that is the closest to your requirements =
+
 
You may review the existing templates by going to the module setup area and by clicking on the "preview" logo.
+
=Find a document template that is the closest to your requirements=
 +
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
 
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
 
pdf_propale_azur.modules.php
Line 32: Line 33:
 
For instance, a customized proposal template will be located in '''...core/modules/propale/doc/'''
 
For instance, a customized proposal template will be located in '''...core/modules/propale/doc/'''
  
= Create your new template =
+
=Create your new template=
 
Do not modify a template. Copy the file and rename it.  
 
Do not modify a template. Copy the file and rename it.  
 
For this example, we will call the new template 'mycompanyblue', based on the template 'azur':
 
For this example, we will call the new template 'mycompanyblue', based on the template 'azur':
* Copy and paste file pdf_propale_azur.modules.php
+
 
* Rename the copy as pdf_propale_mycompanyblue.modules.php
+
*Copy and paste file pdf_propale_azur.modules.php
* Edit and make the following changes in the code:  
+
*Rename the copy as pdf_propale_mycompanyblue.modules.php
# Rename 'Class pdf_propale_azur { ' to 'Class pdf_propale_mycompanyblue { '
+
*Edit and make the following changes in the code:
# Rename 'Function pdf_propale_azur ($db=0)' to 'Function pdf_propale_mycompanyblue ($db=0)' . This may not exist.
+
 
# Rename constant $this->name to match your new template name.
+
#Rename 'Class pdf_propale_azur { ' to 'Class pdf_propale_mycompanyblue { '
 +
#Rename 'Function pdf_propale_azur ($db=0)' to 'Function pdf_propale_mycompanyblue ($db=0)' . This may not exist.
 +
#Rename constant $this->name to match your new template name.
 +
 
 
For example for 'azur' template, change line '$this->name = "azur";' to '$this->name = "mycompanyblue";'.
 
For example for 'azur' template, change line '$this->name = "azur";' to '$this->name = "mycompanyblue";'.
# Save file. Now the template will be available in the list of models in Dolibarr.
 
# Test this model (see previous section) before going further ...
 
  
 +
#Save file. Now the template will be available in the list of models in Dolibarr.
 +
#Test this model (see previous section) before going further ...
  
= Customize content of your new template =
+
 
 +
=Customize content of your new template=
 
Customize the new template. In file pdf_propale_mycompanyblue.modules.php, search for function  
 
Customize the new template. In file pdf_propale_mycompanyblue.modules.php, search for function  
 
'Function _pagehead (&$pdf, $fac)'. This defines the header.
 
'Function _pagehead (&$pdf, $fac)'. This defines the header.
  
  
== Library for PDF manipulation ==
+
==Library for PDF manipulation==
 
The library used to create PDF documents in PHP is called FPDF and can be found in htdocs/includes/tcpdf/tcpdf.class.php. This class contains the methods used to generate different parts of documents.
 
The library used to create PDF documents in PHP is called FPDF and can be found in htdocs/includes/tcpdf/tcpdf.class.php. This class contains the methods used to generate different parts of documents.
  
Line 57: Line 62:
  
 
We can generally find the following calls into templates that generate PDF documents:
 
We can generally find the following calls into templates that generate PDF documents:
* $pdf->SetFont() - ''Define the font to use for the text''
+
 
* $pdf->SetXY() - ''Define position (X,Y) for next text that will be output onto page''
+
*$pdf->SetFont() - ''Define the font to use for the text''
* $pdf->MultiCell() - ''Draw a box containing text. Used to output any text''
+
*$pdf->SetXY() - ''Define position (X,Y) for next text that will be output onto page''
* $pdf->GetY() - ''Return current Y position''
+
*$pdf->MultiCell() - ''Draw a box containing text. Used to output any text''
* $pdf->SetDrawColor() - ''Set the color to use for new text to write - ie black (0,0,0) or white (255,255,255)''
+
*$pdf->GetY() - ''Return current Y position''
* $pdf->Rect() - ''Draw a rectangle whose top left corner coordinates are defined by two first parameters and bottom right corner is defined by two following parameters that are relative values''
+
*$pdf->SetDrawColor() - ''Set the color to use for new text to write - ie black (0,0,0) or white (255,255,255)''
 +
*$pdf->Rect() - ''Draw a rectangle whose top left corner coordinates are defined by two first parameters and bottom right corner is defined by two following parameters that are relative values''
  
  
== Frame of scripts ==
+
==Frame of scripts==
 
During the development of version 2.2, the scripts used for generating PDF documents had the following methods (taking as example the model "crabe") inside the class with the model's name:
 
During the development of version 2.2, the scripts used for generating PDF documents had the following methods (taking as example the model "crabe") inside the class with the model's name:
* pdf_crabe() - ''Creator of the object pdf''
 
* write_pdf_file() - ''General method for generating the file. This method calls all the following ones after initializing some variables''
 
* _pagehead() - ''Method for drawing the heading of the document, including in general the logo, the title of the document (and the date) as well as the frames of the issuer and the addressee of the document''
 
* _tableau() - ''Method for drawing the details table (products, services,...)''
 
* _tableau_info() - ''Method for drawing the table containing information list present in the bill''
 
* _tableau_tot() - ''Method for drawing table of Totals''
 
* _tableau_versement() - ''Method for drawing table of payment rules''
 
* _pagefoot() - ''Method for drawing bottom of the page''
 
  
 +
*pdf_crabe() - ''Creator of the object pdf''
 +
*write_pdf_file() - ''General method for generating the file. This method calls all the following ones after initializing some variables''
 +
*_pagehead() - ''Method for drawing the heading of the document, including in general the logo, the title of the document (and the date) as well as the frames of the issuer and the addressee of the document''
 +
*_tableau() - ''Method for drawing the details table (products, services,...)''
 +
*_tableau_info() - ''Method for drawing the table containing information list present in the bill''
 +
*_tableau_tot() - ''Method for drawing table of Totals''
 +
*_tableau_versement() - ''Method for drawing table of payment rules''
 +
*_pagefoot() - ''Method for drawing bottom of the page''
 +
 +
 +
==Examples of customization==
 +
===Insert a logo===
 +
 +
*Add following instructions
  
== Examples of customization ==
 
=== Insert a logo ===
 
* Add following instructions
 
 
<source lang="php">
 
<source lang="php">
 
$pdf->Image('\www\htdocs\dolibarr\document\societe\logo.jpg', 10, 5, 60.00);
 
$pdf->Image('\www\htdocs\dolibarr\document\societe\logo.jpg', 10, 5, 60.00);
 
</source>
 
</source>
 
With this example: 10=abscissa, 5=ordinate, 60=with of logo
 
With this example: 10=abscissa, 5=ordinate, 60=with of logo
* If the logo is on or outside existing text, remove existing text by commenting the code that outputs the text or by changing its position.
 
  
=== Insert text ===
+
*If the logo is on or outside existing text, remove existing text by commenting the code that outputs the text or by changing its position.
 +
 
 +
===Insert text===
 
Most common function to use
 
Most common function to use
 
<source lang="php">
 
<source lang="php">
Line 98: Line 108:
 
Note: Origin for setXY functions are the top left corner of page.
 
Note: Origin for setXY functions are the top left corner of page.
  
=== Add a PDF Annotation (comment) ===
+
===Add a PDF Annotation (comment)===
 
''Left side of the page around the middle:''
 
''Left side of the page around the middle:''
 
<source lang="php">
 
<source lang="php">
Line 109: Line 119:
 
</source>
 
</source>
  
=== Add Complementary Attribute (extrafiled) ===
+
===Add Complementary Attribute (extrafiled)===
 
A dedicated page give you some advises how to integrate complementary attributes (extrafields) into PDF models of Dolibarr [[Add Extrafields on PDF Models]]
 
A dedicated page give you some advises how to integrate complementary attributes (extrafields) into PDF models of Dolibarr [[Add Extrafields on PDF Models]]
  
== More information ==
+
==More information==
* http://www.fpdf.org/?lang=en
 
* List of TCPDF's methods: tcpdf.org/doc/code/classTCPDF.html
 
  
 +
*http://www.fpdf.org/?lang=en
 +
*List of TCPDF's methods: tcpdf.org/doc/code/classTCPDF.html
  
= Activate your new model =
+
 
 +
=Activate your new model=
 
In page Home => Setup => Modules =>
 
In page Home => Setup => Modules =>
* activate your module
+
 
* eventually, set it as the default model.
+
*activate your module
 +
*eventually, set it as the default model.
  
  
= Troubleshooting =
+
=Troubleshooting=
  
== Q: My PDF template doesn't understand foreign characters, it outputs them as ??? ==
+
==Q: My PDF template doesn't understand foreign characters, it outputs them as ???==
  
 
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.
 
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.
Line 133: Line 145:
 
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)
 
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)
  
* Edit the main.lang file
+
*Edit the main.lang file
* Locate the constant FONTFORPDF at the beginning of the file and change the value to FONTFORPDF=dejavusans for instance if you want to use dejavusans. Please note that sometimes FONTFORPDF may be missing, in this case you should add it at the beginning of the file.
+
*Locate the constant FONTFORPDF at the beginning of the file and change the value to FONTFORPDF=dejavusans for instance if you want to use dejavusans. Please note that sometimes FONTFORPDF may be missing, in this case you should add it at the beginning of the file.
* Save the file and try again to generate your PDF.
+
*Save the file and try again to generate your PDF.
  
 
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/tcpdf/fonts/ so you will get more fonts to play with.
 
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/tcpdf/fonts/ so you will get more fonts to play with.

Revision as of 17:12, 12 November 2019


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.

Prerequisite

  • Dolibarr: 3.0+
  • Knowledge in PHP development


Find a document template that is the closest to your requirements

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/

Create your new template

Do not modify a template. Copy the file and rename it. For this example, we will call the new template 'mycompanyblue', based on the template 'azur':

  • Copy and paste file pdf_propale_azur.modules.php
  • Rename the copy as pdf_propale_mycompanyblue.modules.php
  • Edit and make the following changes in the code:
  1. Rename 'Class pdf_propale_azur { ' to 'Class pdf_propale_mycompanyblue { '
  2. Rename 'Function pdf_propale_azur ($db=0)' to 'Function pdf_propale_mycompanyblue ($db=0)' . This may not exist.
  3. Rename constant $this->name to match your new template name.

For example for 'azur' template, change line '$this->name = "azur";' to '$this->name = "mycompanyblue";'.

  1. Save file. Now the template will be available in the list of models in Dolibarr.
  2. Test this model (see previous section) before going further ...


Customize content of your new template

Customize the new template. In file pdf_propale_mycompanyblue.modules.php, search for function 'Function _pagehead (&$pdf, $fac)'. This defines the header.


Library for PDF manipulation

The library used to create PDF documents in PHP is called FPDF and can be found in htdocs/includes/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:

  • $pdf->SetFont() - Define the font to use for the text
  • $pdf->SetXY() - Define position (X,Y) for next text that will be output onto page
  • $pdf->MultiCell() - Draw a box containing text. Used to output any text
  • $pdf->GetY() - Return current Y position
  • $pdf->SetDrawColor() - Set the color to use for new text to write - ie black (0,0,0) or white (255,255,255)
  • $pdf->Rect() - Draw a rectangle whose top left corner coordinates are defined by two first parameters and bottom right corner is defined by two following parameters that are relative values


Frame of scripts

During the development of version 2.2, the scripts used for generating PDF documents had the following methods (taking as example the model "crabe") inside the class with the model's name:

  • pdf_crabe() - Creator of the object pdf
  • write_pdf_file() - General method for generating the file. This method calls all the following ones after initializing some variables
  • _pagehead() - Method for drawing the heading of the document, including in general the logo, the title of the document (and the date) as well as the frames of the issuer and the addressee of the document
  • _tableau() - Method for drawing the details table (products, services,...)
  • _tableau_info() - Method for drawing the table containing information list present in the bill
  • _tableau_tot() - Method for drawing table of Totals
  • _tableau_versement() - Method for drawing table of payment rules
  • _pagefoot() - Method for drawing bottom of the page


Examples of customization

  • Add following instructions
$pdf->Image('\www\htdocs\dolibarr\document\societe\logo.jpg', 10, 5, 60.00);

With this example: 10=abscissa, 5=ordinate, 60=with of logo

  • If the logo is on or outside existing text, remove existing text by commenting the code that outputs the text or by changing its position.

Insert text

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.

Add a PDF Annotation (comment)

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)));

Add Complementary Attribute (extrafiled)

A dedicated page give you some advises how to integrate complementary attributes (extrafields) into PDF models of Dolibarr Add Extrafields on PDF Models

More information


Activate your new model

In page Home => Setup => Modules =>

  • activate your module
  • eventually, set it as the default model.


Troubleshooting

Q: My PDF template doesn't understand foreign characters, it outputs them as ???

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/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)

  • Edit the main.lang file
  • Locate the constant FONTFORPDF at the beginning of the file and change the value to FONTFORPDF=dejavusans for instance if you want to use dejavusans. Please note that sometimes FONTFORPDF may be missing, in this case you should add it at the beginning of the file.
  • Save the file and try again to generate your PDF.

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