创建PDF模板
本文档描述了如何创建自己的PDF文档生成模板,以便生成自定义文档(报价单、发票等)。它以报价单为例,但适用于任何类型的文档。
要了解如何生成ODT文档模板,请参阅 Create an ODT document template 页面。创建PDF模板需要PHP编程知识,但创建ODT模板不需要PHP编程知识。
前提条件
- Dolibarr: 3.0+
- PHP 开发知识
查找最接近您需求的现有模板
在Dolibarr中,通过进入模块配置页面、并单击模板行末尾的“预览”图标查看预览来测试现有模板。查看每个可用的模板。
在现有模板中,留意最接近您需求的模板。在本例中是azur模板(对应文件 pdf_propale_azur.modules.php)。
有关信息,所有模板都在htdocs/core/modules的相应子目录中,如报价单的propale子目录、发票的facture子目录、订单的commandes子目录等,最后是doc子目录。例如,自定义报价单模板将位于 ...core/modules/propale/doc/、对于自定义发票模板,它将位于 ...core/modules/facture/doc/ 等等...
创建新模板
参考现有模板创建
为了安全起见,我们将保留原始模板。例如,假设我们创建了一个新的报价单模块,称为“mycompanyblue”,其灵感来自“azur”模板:
- 复制并粘贴文件 pdf_propale_azur.modules.php
- 将该副本重命名为 pdf_propale_masocietebleu.modules.php
- 编辑它并在代码中进行以下更改:
- 把“Class pdf_propale_azur { ”替换为“Class pdf_propale_mycompanyblue { ”(重要提示:文件名和类名称必须匹配)
- 修改变量 $this->name 的值以反映新名称。例如,对于模板“azur”,更改行 '$this->name = "azur";' 为 '$this->name = "mycompanyblue";'。(在构造函数中)
- 保存:“masocietebleu”模板可在Dolibarr的propales模板列表中找到
- 通过在与模板相关的模块配置页面(即我们示例中的报价单模块配置页面)中激活此模板,并显示预览来测试此模板...
将您创建的新文档模板添加到表 llx_document_model 中。
使用模块生成器创建
使用 v12.0 中的模块生成器创建新模块。输入要创建的新对象的名称时,可以选中复选框以表示要管理此类新对象的文档。在这种情况下,将自动生成两个模板(一个 ODT 模板和一个 PDF 模板)。
生成的模板将保存在如下路径中:
- custom/mymodule/core/modules/doc/pdf_standard_myobject.class.php
- custom/mymodule/core/modules/doc/doc_generic_myobject_odt.class.php
自定义您的新模板
通过修改代码来自定义您创建的模板:
在 pdf_propale_mycompanyblue.modules.php 中查找函数 _pagehead() 。它负责管理标题的显示。
查找函数 _pagefooter() 。它负责管理页脚的显示。
查找函数 write_file() 。它显示PDF的正文。
用于PDF操作的库
用于创建PDF文档的库被称为TCPDF,可以在htdocs/includes/tcpdf/tcpdf.class.php中找到。在这个类中,您还可以找到许多用于生成文档的不同元素的方法。
PDF生成模板通过调用 pdf_getInstance() 来实例化PDF对象,然后将其方法与发票、订单或其他对象中的数据结合使用。
在文档创建脚本中通常会发现以下调用:
- $pdf->SetFont() - 指定用于后续文本的字体
- $pdf->SetXY() - 指定以X、Y坐标绘制下一个文本的起点,以页面的毫米为单位
- $pdf->MultiCell() - 绘制一个可以包含文本的框,其中必须首先给出宽度和高度-非常常用
- $pdf->GetY() - 返回Y的当前位置
- $pdf->SetDrawColor() - 指定用于下一个要写入的文本的颜色-通常为黑色(0,0,0)或白色 (255,255,255)
- $pdf->Rect() - 绘制一个矩形,其左上角位于前两个参数指示的点,右下角在前两个参数的相对模式下使用最后两个参数
脚本结构
用于生成PDF文档的脚本在模板类中具有以下方法(以“crabe”模板为例):
- pdf_crabe() - PDF对象的构造函数
- write_pdf_file() - 生成文件的通用函数。此方法在初始化一些变量后调用以下所有变量
- _pagehead() - 绘制文档标题的函数,通常包括徽标、文档标题(和日期)以及文档发送者和接收者的框架
- _tableau() - 详细信息表(产品、服务等)的绘制方法
- _tableau_info() - 绘制包含发票上信息列表的表格的方法
- _tableau_tot() - 绘制总计表的方法
- _tableau_versement() - 绘制支付规则表的方法
- _pagefoot() - 绘制页脚的方法
个性化示例
插入 logo
- 添加说明
$pdf->Image('\www\htdocs\dolibarr\document\societe\logo.jpg', 10, 5, 60.00);
在示例中:10=横坐标,5=纵坐标,60=logo宽度
- 如果logo位于现有文本的下方或上方,请通过注释掉显示信息的代码或更改其位置来删除现有文本。
在发票页面底部插入签名或其他内容
- 打开要编辑和插入的.php文件
$pdf->Image(DOL_DOCUMENT_ROOT.'/mydir/signature.jpg',120,250,74);
图像(74mm宽)显示在页脚前的右栏中。
- 将图像复制到目录。具有透明度的PNG图像似乎是有问题的。
- 生成PDF时,将显示签名。
插入文本
使用的主要功能
$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
提醒:setXY 函数的原点位于页面的左上角
添加PDF批注(注释)
页面左侧围绕中间:
$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)));
页面右侧围绕中间:
$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)));
添加补充属性(额外字段)
一个专门的页面为您提供了一些建议,如何将补充属性(额外字段)集成到Dolibarr的PDF模板中:在PDF模板上添加额外字段
如需了解更多信息
- http://www.fpdf.org/?lang=en
- TCPDF函数清单:https://tcpdf.org/docs/srcdoc/
激活您的新模板
前往 主页 => 设置 => 模块/应用 =>
- 激活您的模块
- 最终,将其设置为默认模板
Proposer son modèle via un module
Si le modèle fabriqué est destiné à être livré via un module extension de Dolibarr (voir la doc pour faire un module Dolibarr), le fichier modèle ne sera pas dans le même répertoire que les autres, et ne sera donc pas détecté automatiquement. Pour le rendre détectable vous devrez ajouter cette ligne dans la fonction d'initialisation de votre module externe :
sql=array("INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('".$MODELNAME."','".$MODEL_TARGET_MODULE."',".$conf->entity." )");
avant
return $this->_init($sql,$option);
Puis
Dans Accueil => Configuration => Modules => Module sur lequel porte le modèle => Configurer
- activer votre module qui doit apparaître parmi ceux disponible
- éventuellement le mettre en modèle proposé par défaut.
Vidéos sur le sujet
故障排除
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.