Skip to content

Generate PDFs from Data

In this tutorial, you’ll generate a PDF document from dynamic application data.

You’ll create HTML content, convert it into a PDF, and save the generated file to the filesystem. This pattern is useful when you need to produce reports, confirmations, quotations, summaries, or generated documents from workflow data.


By the end of this tutorial, you will:

  • Create HTML from application data
  • Convert HTML into a PDF
  • Save the PDF to the filesystem
  • Use the generated PDF in downstream workflows

  • Access to a script, workflow step, or console where code can run
  • Data that should be included in the generated document
  • Access to the working filesystem area

Start with the data you want to include in the PDF.

For example:

const request = {
title: "Purchase Request",
requester: "Leigh",
item: "Laptop",
quantity: 1,
budgetAmount: 1000,
currency: "USD"
};

In a real application, this data may come from:

  • A workflow variable
  • A database entity
  • An AI extraction result
  • A form submission

Build an HTML string using the data:

const html = `
<h1>${request.title}</h1>
<p><strong>Requester:</strong> ${request.requester}</p>
<p><strong>Item:</strong> ${request.item}</p>
<p><strong>Quantity:</strong> ${request.quantity}</p>
<p><strong>Budget:</strong> ${request.budgetAmount} ${request.currency}</p>
`;

Note

For simple generated documents, JavaScript template strings are often enough. For user-editable templates, consider using a template engine such as Handlebars.


Use the PDF utility to create a PDF document from the HTML:

const pdf = await SimplePDF.createPdfFromHtml(html);

You can also pass PDF options, such as headers or page settings, depending on the supported configuration.

Example:

const pdf = await SimplePDF.createPdfFromHtml(html, {
headerHtml: "<strong>Generated Document</strong>"
});

Convert the PDF into a buffer so it can be written as a file:

const base64 = await pdf.saveAsBase64();
const buffer = this.data.toBuffer(base64, "base64");

This ensures the PDF is written as binary data rather than plain text.

Why

If you write the Base64 string directly to a file, the file may be saved as text instead of a valid PDF. Convert it to a buffer before writing it.


Write the generated PDF to the working filesystem area:

await this.fs.writeFile(
"working",
"generated/purchase-request.pdf",
buffer,
"binary"
);

The generated PDF can now be accessed from the filesystem.


After the PDF is created, you can use it in other parts of your application.

For example, you can:

  • Attach it to an email
  • Store it as part of a workflow record
  • Make it available for download
  • Archive it for audit or reporting purposes

You have generated a PDF from dynamic data and saved it to the filesystem.

The pattern is:

Data → HTML → PDF → File

Lowgile uses HTML as the source format for generated PDFs.

Your application creates HTML from data, converts the HTML into a PDF document, and writes the resulting binary file to the filesystem. This makes PDF generation flexible because you can control the document layout using HTML.


  • Attach the generated PDF to an email
  • Generate a quotation or report from workflow data
  • Use Handlebars for editable document templates
  • Add tables or styling to the generated HTML

  • Working with Files
  • Sending Emails
  • Workflow Fundamentals
  • Document Generation