Generate PDFs with Firestore

Made by PDFPlum

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

300+
installs
Works with
Cloud Storage and Cloud Firestore
Version
0.18.0 | Source code
Tags
pdf, template, firestore, pdf-generator, template-bundle, html, css, javascript, markdown, handlebars, utilities
License
Apache-2.0
Publisher
PDFPlum
Report
Bug
Abuse

How this extension works

PDFPlum

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 Google Cloud Storage bucket.

This extension listens for Firestore document-creation events, which will trigger the extension.

Upon triggering, the extension downloads the template, runs Handlebars on it using data from the Firestore document, and converts it into a PDF. The generated PDF file is then stored in a Firebase Storage bucket.

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.

Firestore

When a document is created in FIRESTORE_COLLECTION, the extension generates a PDF based on the template file and the content of the created document.

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 coming from the document except for its _pdfplum_config key. After all network resources are fully loaded, a PDF file is generated from the rendered webpage.

The generated PDF file is saved in the bucket specified in the OUTPUT_STORAGE_BUCKET extension parameter.

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

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 Firestore
    • Cloud Functions (Node.js 10+ runtime. See FAQs)
    • Cloud Storage