Changes

Jump to navigation Jump to search
m
no edit summary
Line 1: Line 1:  
{{TemplateDocDevEn}}
 
{{TemplateDocDevEn}}
   −
This document describe how to create your own module to generate different documents that match your needs (for proposals, invoices, etc...)
+
This page describes how to create your own document-generation module (for proposals, invoices, etc...)
Tutorial is based on commercial proposals used as example but can be used for any type of document.
+
The tutorial uses Commercial Proposals as an example but the same process is used for any type of document.
   −
To know how to build an ODT template, see page [[Create an ODT document template]]. Building a PDF template require PHP development knowledge, but not building an ODT template.
+
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 =
 
= Prerequisite =
Line 11: Line 12:       −
= Find a model near your need =
+
= Find a document template that is the closest to your requirements =
In Dolibarr, test existing models by going to the module setup area and by clicking on the "preview" logo.. Among existing models, find the one that is the nearest of your need. In this example, we suppose it's the template "azur" (corresponding to file pdf_propale_azur.modules.php).
+
You may review the existing templates by going to the module setup area and by 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
   −
FYI, all models are in '''htdocs/core/modules''', into subdirectory propale for commercial proposals, facture for invoices, commandes for orders, etc... and finally '''doc'''.
+
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/'''
 
For instance, a customized proposal template will be located in '''...core/modules/propale/doc/'''
      
= Create your new template =
 
= Create your new template =
For security, we will retain the original models. For the example, we assume we creates a new template we call 'mycompanyblue' and inspired by the template 'azur':
+
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
 
* Copy and paste file pdf_propale_azur.modules.php
* Rename the copy into pdf_propale_mycompanyblue.modules.php
+
* Rename the copy as pdf_propale_mycompanyblue.modules.php
 
* Edit and make the following changes in the code:  
 
* Edit and make the following changes in the code:  
# Rename 'Class pdf_propale_azur { ' into 'Class pdf_propale_mycompanyblue { '
+
# Rename 'Class pdf_propale_azur { ' to 'Class pdf_propale_mycompanyblue { '
# Rename 'Function pdf_propale_azur ($db=0)' into 'Function pdf_propale_mycompanyblue ($db=0)'
+
# 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";' into '$this->name = "mycompanyblue";'.
+
# Rename constant $this->name to match your new template name.
# Save file. Now template is available in the list of models into Dolibarr
+
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 ...
 
# Test this model (see previous section) before going further ...
       
= Customize content of your new template =
 
= Customize content of your new template =
Customize the template created. Still in file pdf_propale_mycompanyblue.modules.php, search function 'Function _pagehead (&$pdf, $fac)'. It manages the display of the header.
+
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 ==
 
== Library for PDF manipulation ==
The library used to create PDF documents in PHP language is called FPDF and can be found into htdocs/includes/tcpdf/tcpdf.class.php. It's also in this class you can find all different 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.
   −
Templates instantiate this class FPDF and used its method, combined with data of invoice, order, or other kind of data.
+
Templates instantiate the FPDF class and use its methods combined with the specific data of the invoice, order etc.
   −
We can generraly find the following calls into templates that generated 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->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->SetXY() - ''Define position (X,Y) for next text that will be output onto page''
Line 45: Line 54:  
* $pdf->GetY() - ''Return current Y position''
 
* $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->SetDrawColor() - ''Set the color to use for new text to write - ie black (0,0,0) or white (255,255,255)''
* $pdf->Rect() - ''Drwa a rectanle whose top left corner coordinates ared defineds by two first parameters and bottom right corner is defined by two following parameters that are relative values''
+
* $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''
      Line 67: Line 76:  
</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 logo is on or outside existing text, remove exisiting text by commenting the code that output the text or by changing its position.
+
* 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 ===
 
=== Insert text ===
Most comme function to use
+
Most common function to use
 
<source lang="php">
 
<source lang="php">
 
$pdf->setX(float a); // set current x position
 
$pdf->setX(float a); // set current x position
Line 107: Line 116:  
== 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 set of 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.
    
You will find the fonts used by Dolibarr to print PDF inside your Dolibarr folder.../includes/tcpdf/fonts/
 
You will find the fonts used by Dolibarr to print PDF inside your Dolibarr folder.../includes/tcpdf/fonts/
Line 119: Line 128:  
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.
   −
Also, please make a backup of your files before to modify.
+
Also, please make a backup of your files modification.
   −
Alternative if this doesn't work: make sure the Translation class is correctly instanciated and loaded with the correct language inside your PDF templates (stored inside the $langs or $outputlangs variable).
+
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).
   −
Thank's to Humphrey for the tip.
+
Thanks to Humphrey for the tip.
98

edits

Navigation menu