Generate PDFs via HTTP

Made by PDFPlum

Generates PDF files from HTML templates and HTTP calls using Puppeteer and Handlebars.js and stores them in Firebase Storage and/or returns them in response.

<10
installs
Works with
Cloud Storage
Version
0.12.19 | Source code
License
Apache-2.0
Publisher
PDFPlum
Report
Bug
Abuse

How this extension works

Use this extension to generate PDF files with Handlebars, Puppeteer, and HTML. To use PDFPlum, follow these steps:

  • Create a template using HTML and Handlebars.
  • Package the resources into a ZIP file.
  • Upload the ZIP file to a Firebase Storage bucket.

This extension exposes an HTTP endpoint, which, when called, triggers the extension.

Upon triggering, the extension downloads the template, runs Handlebars on it with the data from the endpoint’s GET parameters, and converts it to PDF. The generated PDF file is stored in a Firebase Storage bucket, and you can also configure the extension to return the PDF in the HTTP call response.

Demo

For inspiration, check out the pre-made templates and their outputs in the template-samples/ directory. Each includes HTML files and their resources. To use any of these templates in your extension, upload the .zip file to a Storage bucket and include the complete file path in the TEMPLATE_PATH extension parameter.

Preparation

Before installing this extension, you need to:

Templates in this extension are based on Handlebars and can be configured using the features provided by Handlebars.

The printing mechanism is powered by Puppeteer which uses Chromium’s PDF rendering engine.

Why Puppeteer?

After testing various free PDF generation tools, including Google Docs API, Pandoc, makepdf, and others, Puppeteer was found to be the best for running in Cloud Functions and supporting easy templating with Handlebars. All other tools had limitations that prevented a complete end-to-end PDF templating solution. Third-party PDF APIs could be alternatives, but this extension aims to provide a free and straightforward solution.

Usage

The template

The template bundle is a ZIP file containing an index.html file. The bundle can optionally include media, fonts, CSS files, and more. The index.html file can access all files in the bundle, assuming they are served at the root (/).

For example, images/flower.png in the ZIP file will be served at /images/flower.png. The index.html file can also access online resources, such as loading a font, script, or CSS file from CDNs.

The endpoint

The extension exposes an endpoint that generates a PDF file based on the template file and the GET parameters provided.

How it all works

The template bundle is uncompressed, served, and loaded by a Chromium instance. Handlebars runs on all .html, .txt, and .md files, replacing their template placeholders with data from the GET parameter data. After all network resources are fully loaded, a PDF file is generated from the rendered webpage.

If a bucket name is set in the OUTPUT_STORAGE_BUCKET extension parameter, the generated PDF file is saved in that bucket. If RETURN_PDF_IN_RESPONSE is enabled, the PDF file is returned in response to the HTTP call.

The PDF file is named according to the rules described here.

GET parameters

outputFileName

Sets the name of the output PDF file to be saved in the Firebase Storage bucket and the name of the PDF file returned in the response.

If not provided, a name is generated by concatenating a UUID and the current timestamp

data

Provides the values to replace the template placeholders.

It can include nested containers (array lists and associative arrays) as described here.

adjustHeightToFit

Overrides the ADJUST_HEIGHT_TO_FIT extension parameter.

chromiumPdfOptions

Overrides the CHROMIUM_PDF_OPTIONS extension parameter.

networkIdleTime

Overrides the NETWORK_IDLE_TIME extension parameter.

shouldWaitForIsReady

Overrides the SHOULD_WAIT_FOR_IS_READY extension parameter.

templatePath

Overrides the TEMPLATE_PATH extension parameter.

Billing

To install an extension, your project must be on the Blaze (pay as you go) plan

  • This extension uses other Firebase and Google Cloud Platform services, which have associated charges if you exceed the service’s no-cost tier:
    • Cloud Functions (Node.js 10+ runtime. See FAQs)
    • Cloud Storage