pdf-from-template
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

pdf-from-template

Minimal PDF generation from a template

Supported templates:

  • html
  • handlebar

usage

With a typescript project:

/**
 * 1. Import
 */
import { pdfFromTemplate } from "pdf-from-template";
// optional
import { PDFOptions } from "puppeteer";

/**
 * 2. Prepare data
 */

// sample template
const template = fs.readFileSync(
    path.resolve(__dirname, "template.hbs"),
    "utf8",
);

// sample data
const data = {
    logo: "https://www.sparksuite.com/images/logo.png",
    invoiceId: Math.floor(Date.now() / 100000),
    invoiceCreationDate: moment(new Date()).format("LL"),
    invoiceDueDate: moment(new Date()).add(10, "days").format("LL"),
};

// sample output directory
const outDir = "./out";
// sample file name
const fileName = `pdf_${data.invoiceId}.pdf`;
// sample options
const options: PDFOptions = {};

/**
 * 3. Call the function
 */
pdfFromTemplate(template, data, fileName, outDir, options)
    .then((buffer) => console.log(buffer))
    .catch((err) => console.log(err));

With a node.js project:

const { pdfFromTemplate } = require("pdf-from-template");
const path = require("path");
const fs = require("fs");
const moment = require("moment");

// sample template
const template = fs.readFileSync(
    path.resolve(__dirname, "template.hbs"),
    "utf8",
);

// sample data
const data = {
    logo: "https://www.sparksuite.com/images/logo.png",
    invoiceId: Math.floor(Date.now() / 100000),
    invoiceCreationDate: moment(new Date()).format("LL"),
    invoiceDueDate: moment(new Date()).add(10, "days").format("LL"),
};

// sample output directory
const outDir = "./out";
// sample file name
const fileName = `pdf_${data.invoiceId}.pdf`;
// sample options
const options = {};

/**
 * 3. Call the function
 */
pdfFromTemplate(template, data, fileName, outDir, options)
    .then((buffer) => console.log(buffer))
    .catch((err) => console.log(err));

doc

Documentation generated by TypeDoc is HERE

Readme

Keywords

Package Sidebar

Install

npm i pdf-from-template

Weekly Downloads

0

Version

0.0.3

License

MIT

Unpacked Size

35.8 kB

Total Files

10

Last publish

Collaborators

  • chankruze