Generating Documents¶
Server scripts can produce PDF and Word files from process data, for example an order confirmation, a contract or a certificate. This page helps you choose an approach and shows the common patterns.
Choosing an Approach¶
| Need | Use |
|---|---|
| A PDF whose layout is defined in HTML, maintained with the process | A pool template rendered with $Templates.Format, then $Files.AddPDF |
| A Word template prepared by business users, filled with process data | Writer with Content.Replace |
| A document with tables, headers, footers, page numbers or barcodes built from data | Writer with sections, paragraphs and tables |
| Conversion of an uploaded Word, RTF or HTML file to PDF | Writer.LoadFromBase64 and SaveToBase64 |
All approaches return or create a file. Store its ID in the process data so that the file is shown on the form and kept with the instance.
Creating a PDF from a Pool Template¶
Define the HTML in a pool template, so that the layout can be changed without editing scripts, and convert the rendered HTML to PDF:
1 2 3 4 5 6 7 | |
The header and footer HTML can use the [page] and [topage] placeholders. See PDFOptions for margins and page size.
Filling a Word Template¶
Business users can prepare the template in a word processor with placeholders such as {CustomerName}. Store the template as a file, load it, replace the placeholders and save the result:
1 2 3 4 5 6 7 8 9 | |
Use placeholders that cannot occur in ordinary text. The template file itself is not changed. Save as .docx instead of .pdf when users must edit the result.
Building a Document from Data¶
Build the document element by element when its content depends on the data, such as a table with one row per line. A document consists of sections; a section contains blocks such as paragraphs and tables; a paragraph contains runs of text and fields:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
The Add... methods accept an options object whose properties are applied to the new element. Colors are an exception: set them on the element after it is added, for example run.CharacterFormat.FontColor = '#1F4E79'. Add a QR code or a barcode with AddField('DisplayBarcode', ...).
Converting Files¶
Writer.LoadFromBase64 detects the format of the input. Save it in another format with SaveToBase64:
1 2 3 4 5 | |
Fonts used by a document must be available on the server for PDF output; otherwise a substitute font is used.