创建PDF模板

From Dolibarr ERP CRM Wiki
Jump to navigation Jump to search


本文档描述了如何创建自己的模块来生成与您的需求(建议、发票等)相匹配的不同文档。 教程是基于商业建议作为示例,但可以用于任何类型的文档。

了解如何建立一个ODT模板, 参考 Create an ODT document template. 构建一个PDF模板需要PHP开发知识,但不需要构建一个ODT模板。

先决条件

  • Dolibarr: 3.0+
  • PHP 开发知识

找到一个接近你需要的模型

在Dolibarr,测试现有的模型,通过模块设置区域,并点击“预览”标志。在现有的模型中,找到一个最接近你需要的模型。在这个例子中,我们假设它是“azur”模板(对应于文件pdf_propale_azur.modules.php)。

FYI,所有的模型都在htdocs/core/modules的相应子目录中,用于商业提案、发票制作、命令订单等。

创建新模板

为了安全,我们将保留原来的模型。例如,我们假设我们创建了一个新模板,我们称之为'mycompanyblue',并由模板“azur”启发:

  • 复制并粘贴文件pdf_propale_azur.modules.php
  • 将该副本重命名为pdf_propale_mycompanyblue.modules.php
  • 编辑并在代码中进行以下更改:
  1. 替换 'Class pdf_propale_azur { ' 为 'Class pdf_propale_mycompanyblue { '(更改类名为文件名,否则出错)
  2. 替换 'Function pdf_propale_azur ($db=0)' 为 'Function pdf_propale_mycompanyblue ($db=0)' (译者并未找到文件函数)
  3. 替换 $this->name 匹配你的模板名。 如:'azur' 模板, 改变 '$this->name = "azur";' 为 '$this->name = "mycompanyblue";'。(在构造函数中)
  4. 保存。现在模板可以用了。
  5. 测试模板 (see previous section) before going further ...

自定义新模板的内容

自定义创建的模板。仍然在文件pdf_propale_mycompanyblue.modules.php,搜索函数 'Function _pagehead (&$pdf, $fac)'。它管理标题的显示。

PDF操作库

用于在PHP语言中创建PDF文档的库被称为FPDF,可以被找到于htdocs/includes/tcpdf/tcpdf.class.php文件中。在这个类中,您可以找到用于生成文档不同部分的所有不同方法。

模板实例化了此FPDF类,并使用其方法,结合了发票、订单或其他类型的数据。

我们可以在生成PDF文档的模板时调用以下函数:

  • $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

脚本框架

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

定制实例

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

插入文本

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.

更多信息

  • List of TCPDF's methods: tcpdf.org/doc/code/classTCPDF.html

激活模块

In page Home => Setup => Modules =>

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

故障排除

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.

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 before to modify.

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

Thank's to Humphrey for the tip.