714
edits
PolyglotBot (talk | contribs) m (Import interlang links (links to translated versions of this page in other languages) from origin English page "Create_a_PDF_document_template".) |
No edit summary |
||
| Line 12: | Line 12: | ||
{{TemplateMainPageZh}} | {{TemplateMainPageZh}} | ||
本文档描述了如何创建自己的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”启发: | 为了安全,我们将保留原来的模型。例如,我们假设我们创建了一个新模板,我们称之为'mycompanyblue',并由模板“azur”启发: | ||
= 自定义新模板的内容 = | *复制并粘贴文件pdf_propale_azur.modules.php | ||
*将该副本重命名为pdf_propale_mycompanyblue.modules.php | |||
*编辑并在代码中进行以下更改: | |||
#替换 'Class pdf_propale_azur { ' 为 'Class pdf_propale_mycompanyblue { '(更改类名为文件名,否则出错) | |||
#替换 'Function pdf_propale_azur ($db=0)' 为 'Function pdf_propale_mycompanyblue ($db=0)' (译者并未找到文件函数) | |||
#替换 $this->name 匹配你的模板名。 如:'azur' 模板, 改变 '$this->name = "azur";' 为 '$this->name = "mycompanyblue";'。(在构造函数中) | |||
#保存。现在模板可以用了。 | |||
#测试模板 (see previous section) before going further ... | |||
=自定义新模板的内容= | |||
自定义创建的模板。仍然在文件pdf_propale_mycompanyblue.modules.php,搜索函数 'Function _pagehead (&$pdf, $fac)'。它管理标题的显示。 | 自定义创建的模板。仍然在文件pdf_propale_mycompanyblue.modules.php,搜索函数 'Function _pagehead (&$pdf, $fac)'。它管理标题的显示。 | ||
== PDF操作库 == | ==PDF操作库== | ||
用于在PHP语言中创建PDF文档的库被称为FPDF,可以被找到于'''htdocs/includes/tcpdf/tcpdf.class.php'''文件中。在这个类中,您可以找到用于生成文档不同部分的所有不同方法。 | 用于在PHP语言中创建PDF文档的库被称为FPDF,可以被找到于'''htdocs/includes/tcpdf/tcpdf.class.php'''文件中。在这个类中,您可以找到用于生成文档不同部分的所有不同方法。 | ||
| Line 46: | Line 50: | ||
我们可以在生成PDF文档的模板时调用以下函数: | 我们可以在生成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: | 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'' | ||
=== 插入 logo === | *write_pdf_file() - ''General method for generating the file. This method calls all the following ones after initializing some variables'' | ||
* Add following instructions | *_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'' | |||
==定制实例== | |||
===插入 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 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 | Most comme function to use | ||
<source lang="php"> | <source lang="php"> | ||
| Line 85: | Line 94: | ||
Note: Origin for setXY functions are the top left corner of page. | 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 => | 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 ??? == | ==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 set of font. | ||
| Line 103: | Line 114: | ||
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. | ||
edits