PDF Reports using CloudCode

JourneyApps's PDF service and library can be used from CloudCode to generate PDFs, then email or upload the resulting file.

TypeScript Packages

In TypeScript apps, app packages can also be used to generate PDF reports. Packages are the recommended tool for PDF reports if, for example, the reports need to be generated while the user is offline. Please see the documentation for further information.

Getting Started

When creating a new task, select the "PDF Report" template.

This creates a basic task the combines all of the following:

  1. Use handlebars to generate HTML from a template.

  2. Convert the HTML to a PDF, using JourneyApps's PDF service.

  3. Generate an email body, once again using a handlebars template.

  4. Send the report via email using SendGrid or save the report to the database.

Sending Email

The task template contains sample code to send the PDF as an email attachment via SendGrid. To use this, a SendGrid account and API key are required. SendGrid offers 100 emails/day for free, and very reasonable pricing for larger volumes.

There are also many other alternative services available, for example, Postmark. However, all our examples will use SendGrid.

PDF Renderer

We use a hosted version of Chrome's renderer to generate the PDF. This generates results looking the same or very similar to Chrome's print preview functionality, with generally the same functionality being supported.

We do however apply some restrictions:

  • WebGL is not supported.

  • Cookies and other storage APIs are not supported.

  • All resources must be loaded over HTTPS.

  • Navigation and redirects are not supported.

  • All the rendering must be performed within 29 seconds.

Usage documentation for the library is available on the repository:

Splitting Templates into Files

The task template keeps all the templates (HTML and CSS) in a single file together with the JavaScript for simplicity. However, for any larger templates it is more maintainable to split these out into separate files.

In OXIDE, new files can be created via the Create CloudCode File action.

The JavaScript side then becomes:

const css = fs.readFileSync(__dirname + '/style.css', 'utf8');
const pdfTemplate = handlebars.compile(fs.readFileSync(__dirname + '/template.html', 'utf8'));

Uploading to S3

The pdf-reports library contains helper methods to upload the resulting document to S3. To use this, AWS credentials with access to a S3 bucket is required.

For details, see our docs on uploading to S3.

JOURNEY_PDF_KEY: Local Development vs CloudCloud

For local development, you can use the following token for JOURNEY_PDF_KEY: ohhu8Yahyu6ipee6aemeeg3quohghu2ijeph2xoZ. This token will generate watermarked PDFs.

In CloudCode, you can use this.backend.token to generate a token. Note that the token expires after 10 minutes.

DocRaptor

The Chrome renderer has very limited support for print-related styling, for example headers & footers, page-specific styles, page numbers and table of contents. If this functionality is required, it is recommended to use the DocRaptor service instead.

DocRaptor can be used together with the pdf-reports library, by using pdf.generatePdfDocRaptor() instead of pdf.generatePdf(). For details, see our docs on using DocRaptor.

Last updated