pagebaker
v1.0.0
Published
A powerful Node.js library for server-side HTML template rendering and PDF generation. Built with Puppeteer for PDF conversion and Mustache for templating.
Maintainers
Readme
PageBaker
A powerful Node.js library for server-side HTML template rendering and PDF generation. Built with Puppeteer for PDF conversion and Mustache for templating.
Features
- Populate HTML templates with data using Mustache templating
- Convert HTML templates to high-quality PDF documents using Puppeteer
- Simple API for both in-memory PDF generation and file saving
- Flexible customization options for PDF output
- Uses
.mustacheextension for template files
Installation
npm install pagebakerMethods
getHTML(templateName, data)
Renders an HTML template with the provided data.
templateName: Name of the template file (without .mustache extension)data: Object containing data to populate the template- Returns: HTML string
getPDFBuffer(templateName, data)
Generates a PDF from a template and returns it as a buffer.
templateName: Name of the template file (without .mustache extension)data: Object containing data to populate the template- Returns: Promise resolving to a PDF buffer
savePDF(templateName, data, outputPath)
Generates a PDF from a template and saves it to a file.
templateName: Name of the template file (without .mustache extension)data: Object containing data to populate the templateoutputPath: File path where the PDF should be saved- Returns: Promise that resolves when the file is saved
Usage
Basic Example
const PageBaker = require("pagebaker");
// Initialize with template directory
const baker = new PageBaker("./templates");
// Generate PDF from template and save to file
async function generateInvoice() {
const data = {
invoiceNumber: "INV-001",
date: "2025-04-29",
customer: {
name: "Acme Corp",
address: "123 Business Ave, Suite 100",
},
items: [
{ description: "Web Development", amount: 1200 },
{ description: "Hosting (Annual)", amount: 300 },
],
total: 1500,
};
// Save to file
await baker.savePDF("invoice", data, "invoice-001.pdf");
console.log("PDF saved successfully!");
}
generateInvoice();Getting HTML Output
const html = baker.getHTML("invoice", data);
console.log(html);Getting PDF Buffer
async function getPDFBuffer() {
const buffer = await baker.getPDFBuffer("invoice", data);
// Use the buffer (e.g., send as API response)
}