1.0.0 • Published 7 months ago

pdfile v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

PDFile

PDFile is a Node.js library for generating high-quality, dynamic PDFs using Handlebars templates and Puppeteer. It supports multi-page PDFs and offers full customization. Perfect for web developers, it enables easy creation of multi-page PDFs by simply designing HTML templates for output.

Installation

npm i pdfile

Usage

Initialization

Import the function

import { generatePdf } from 'pdfile';

Call the function to generate the PDF

const generatedPdfFilePath = await generatePdf({
  [templateFilePath],
  [dataPerPage],
  [pdfFilePath],
  [useStream],
  [helpersFilePath],
  [puppeteerOptions],
  [pdfOptions],
});
ParameterTypeDescriptionRequiredDefault
templateFilePathstringPath to the Handlebars template file.YesN/A
dataPerPageArrayArray of objects containing data for each page of the PDFYesN/A
pdfFilePathstringThe path where the generated PDF file will be saved. Required, if useStream is not passed or set to false.NoN/A
useStreambooleanReturns a readable stream of the PDF instead of saving it to disk if set to trueNofalse
helpersFilePathstringPath to a file containing custom Handlebars helpers.NoN/A
puppeteerOptionsobjectPuppeteer launch options (e.g., headless mode, executable path). <PuppeteerLaunchOptions>No { "headless": true, "args": "--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage" }
pdfOptionsobjectOptions for PDF generation (e.g., format, margins). <PDFOptions>No { "format": "A4", "margin": { "left": "10mm", "top": "10mm", "right": "10mm", "bottom": "10mm" }, "printBackground": true }

Working Example

To quickly get started, you can use the 'example' folder and check out the working code given in the file 'file.service.ts'. Ensure all dependencies are installed beforehand.

PDF generated in this example:

plot

More details


JavaScriptHandlebars Helper Syntax (Standard)Handlebars Helper Syntax (Custom)
if (a === b){{#if (eq a b)}}{{#ifCond var1 '===' var2}}
if (a !== b){{#if (not-eq a b)}}{{#ifCond var1 '!==' var2}}
if (a && b){{#if (and a b)}}{{#ifCond var1 '&&' var2}}
if (a > b){{#if (gt a b)}}{{#ifCond var1 '>' var2}}
if (a >= b){{#if (gte a b)}}{{#ifCond var1 '>=' var2}}
if (a < b){{#if (lt a b)}}{{#ifCond var1 '<' var2}}
if (a <= b){{#if (lte a b)}}{{#ifCond var1 '<=' var2}}
if (a && b){{#if (and a b)}}{{#ifCond var1 '&&' var2}}
if (a \|\| b){{#if (or a b)}}{{#ifCond var1 \|\| var2}}

License

MIT