Difference between revisions of "Create a PDF document template"

From Dolibarr ERP CRM Wiki
Jump to navigation Jump to search
Line 47: Line 47:
* _pagefoot() - ''Méthode de dessin du pied de page''
* _pagefoot() - ''Méthode de dessin du pied de page''
== Insert a logo ==
== Examples of customization ==
=== Insert a logo ===
* Add following instructions
* Add following instructions
<source lang="php">
<source lang="php">
Line 55: Line 56:
* 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 logo is on or outside existing text, remove exisiting text by commenting the code that output the text or by changing its position.
== Insert text ==
=== Insert text ===
Most comme function to use
Most comme function to use
<source lang="php">
<source lang="php">

Revision as of 11:39, 18 September 2010

This document describe how to create your own module to generate different documents that match your needs (for proposals, invoices, etc...) Tutorial is based on commercial proposals used as example but can be used for any type of document.

Find a model near your need

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

FYI, all models are in htdocs/includes/modules, into subdirectory propale for commercial proposals, facture for invoices, commandes for orders, etc...

Create your new template

For security, we will retain the original models. For the example, we assume we creates a new module we call 'mycompanyblue' and inspired by the module 'azur':

  • Copy and paste file pdf_propale_azur.modules.php
  • Rename the copy into pdf_propale_mycompanyblue.modules.php
  • Edit and make the following changes in the code:
  1. Rename 'Class pdf_propale_azur { ' into 'Class pdf_propale_mycompanyblue { '
  2. Rename 'Function pdf_propale_azur ($db=0)' into 'Function pdf_propale_mycompanyblue ($db=0)'
  3. Save file. Now template is available in the list of models into Dolibarr
  4. Test this model (see previous section) before going further ...

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.

Library for PDF manipulation

The library used to create PDF documents in PHP language is called FPDF and can be found into htdocs/includes/fpdf/fpdf/fpdf.class.php. It's also in this class you can find all different 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.

We can generraly find the following calls into templates that generated 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() - 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

Frame of scripts

En verysmall.png Page waiting for translation. To translate, create an account, go back and clic on "Modify".
Fr verysmall.png Page en attente de traduction. Pour traduire, créez un compte, revenez et cliquez sur "Modifier".
Es verysmall.png Página a traducir. Para traducirla, cree una cuenta, vuelva a la página y haga clic en "editar".
It verysmall.png Pagina da tradurre. Per tradurla, crea un utente, torna indietro e clicca su "modifica".
Pt verysmall.png Página a aguardar por tradução. Para traduzir, crie uma conta, volte atrás e clique em "Modificar".
De verysmall.png Seite wartet auf Übersetzung. Um Übersetzung zu erstellen, richte einen Account ein, geh zurück und klicke auf "bearbeiten".

Les scripts de génération des documents PDF, au moment du développement de la version 2.2, disposent des méthodes suivantes (prenant pour exemple le modèle "crabe") au sein de la classe du nom du modèle:

  • pdf_crabe() - Constructeur de l'objet pdf
  • write_pdf_file() - Méthode générale pour la génération du fichier. Cette méthode appelle toute les suivantes après avoir initialisé quelques variables
  • _pagehead() - Méthode de dessin de l'en-tête du document, incluant généralement le logo, le titre du document (et la date) ainsi que les cadres de l'émetteur et du destinataire du document
  • _tableau() - Méthode de dessin du tableau de détails (produits, services, etc)
  • _tableau_info() - Méthode de dessin du tableau reprenant une liste d'informations sur la facture
  • _tableau_tot() - Méthode de dessin du tableau des totaux
  • _tableau_versement() - Méthode de dessin du tableau des règles de versement
  • _pagefoot() - Méthode de dessin du pied de 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 logo is on or outside existing text, remove exisiting text by commenting the code that output the text or by changing its position.

Insert text

Most comme 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.

More information


Activate your new model

In page Home => Setup => Modules =>

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