Skip to content

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
var html = $Templates.Format('Order Confirmation', $Xml.SelectSingle('Order'));

var pdf = $Files.AddPDF(html, 'OrderConfirmation.pdf', {
    HeaderHtml : '<div style="text-align:right">Page [page] of [topage]</div>'
});

$Xml.SetValue('Order/ConfirmationFile', pdf.Id);

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
var writer = new Writer();
writer.LoadFromBase64($Files.GetBase64($Xml.Evaluate('Contract/TemplateFile')));

writer.Content.Replace('{CustomerName}', $Xml.Evaluate('Customer/Name'));
writer.Content.Replace('{ContractDate}', $Xml.Evaluate('Contract/Date'));
writer.Content.Replace('{Amount}', $Xml.Evaluate('Contract/Amount'), { Bold : true });

var contract = $Files.AddBase64(writer.SaveToBase64('.pdf'), 'Contract.pdf', 'application/pdf');
$Xml.SetValue('Contract/File', contract.Id);

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
var writer = new Writer();
var section = writer.Sections.Add();

section.Blocks.AddParagraph({ ParagraphFormat : { Alignment : 'Center', SpaceAfter : 12 } })
    .Inlines.AddRun({ Text : 'Order ' + $Xml.Evaluate('OrderNumber'), CharacterFormat : { Bold : true, Size : 16 } });

var table = section.Blocks.AddTable();

function addRow(values, bold) {
    var row = table.Rows.Add();

    for (var i = 0; i < values.length; i++) {
        row.Cells.Add().Blocks.AddParagraph()
            .Inlines.AddRun({ Text : values[i], CharacterFormat : { Bold : bold } });
    }
}

addRow(['Product', 'Quantity', 'Price'], true);

$Xml.SelectAll('Lines/Line', function (line) {
    addRow([line.Evaluate('Product'), line.Evaluate('Quantity'), line.Evaluate('Price')], false);
});

var footer = section.HeadersFooters.Add('FooterDefault').Blocks.AddParagraph({ ParagraphFormat : { Alignment : 'Right' } });
footer.Inlines.AddRun({ Text : 'Page ' });
footer.Inlines.AddField('Page');

var file = $Files.AddBase64(writer.SaveToBase64('.pdf'), 'Order.pdf', 'application/pdf');
$Xml.SetValue('OrderFile', file.Id);

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
var writer = new Writer();
writer.LoadFromBase64($Files.GetBase64($Xml.Evaluate('UploadedFile')));

var pdf = $Files.AddBase64(writer.SaveToBase64('.pdf'), 'Document.pdf', 'application/pdf');
$Xml.SetValue('PdfFile', pdf.Id);

Fonts used by a document must be available on the server for PDF output; otherwise a substitute font is used.