Generate PDFs from Data
Generate PDFs from Data
Section titled “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.
What you’ll build
Section titled “What you’ll build”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
Prerequisites
Section titled “Prerequisites”- Access to a script, workflow step, or console where code can run
- Data that should be included in the generated document
- Access to the
workingfilesystem area
Step 1: Prepare the data
Section titled “Step 1: Prepare the data”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
Step 2: Create HTML content
Section titled “Step 2: Create HTML content”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.
Step 3: Convert the HTML to a PDF
Section titled “Step 3: Convert the HTML to a PDF”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>"});Step 4: Convert the PDF to a buffer
Section titled “Step 4: Convert the PDF to a buffer”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.
Step 5: Save the PDF to the filesystem
Section titled “Step 5: Save the PDF to the filesystem”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.
Step 6: Use the generated PDF
Section titled “Step 6: Use the generated PDF”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
Result
Section titled “Result”You have generated a PDF from dynamic data and saved it to the filesystem.
The pattern is:
Data → HTML → PDF → FileWhat’s happening
Section titled “What’s happening”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.
Next steps
Section titled “Next steps”- 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
Related guides
Section titled “Related guides”- Working with Files
- Sending Emails
- Workflow Fundamentals
- Document Generation