0.0.3 • Published 3 years ago
pdf-from-template v0.0.3
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