Convert Invoices to PDF Automatically in Node.js
Generating PDF invoices in a Node.js application is a common requirement for SaaS platforms, e-commerce systems, and internal billing tools. But maintaining your own headless browser, handling CSS inconsistencies, and scaling batch exports can quickly become messy and expensive.
This guide shows how to automate invoice PDF generation in Node.js using the PageSnap.co API — a simple, reliable HTML-to-PDF and URL-to-PDF service. You’ll learn how to generate PDFs from URLs, raw HTML, or templates; process single or batch invoices; and even store PDFs directly into your own AWS S3 bucket.
Why automate invoice PDFs?
Automated invoice PDFs improve accuracy, save engineering time, and create a better user experience:
- Invoices are generated the moment a purchase or billing cycle completes
- No manual downloads or html-to-pdf browser hacks
- PDFs stay consistent across users and platforms
- Perfect for scheduled and batch billing workflows
- You can store invoices securely in S3 for audit or compliance purposes
PageSnap.co handles all rendering logic using a full headless browser, so your Node.js server stays fast and lightweight.
How PageSnap.co Works (Quick Overview)
Using PageSnap is simple:
- Send a POST request to the
/snapendpointhttps://api.pagesnap.co/snap - Authenticate using Basic Auth (email + API key)
- Provide one or more invoice sources inside
contentshtmlsfor custom HTMLurlsfor hosted invoice pagestemplates+ data for reusable invoice templates
- Optionally set:
webhook_urlfor batch processings3_path_urlto save output directly into your AWS S3 bucketoptionsto control layout, margins, headers, JS execution, etc.
- PageSnap responds with PDF URLs (or sends them to your webhook for batch jobs)
Everything here is based on your official documentation.
Example: Convert a Single Invoice to PDF in Node.js
Below is a simple Node.js function that converts a hosted invoice URL to PDF:
import axios from "axios";
const API_ENDPOINT = "https://api.pagesnap.co/snap";
const PAGESNAP_USER = process.env.PAGESNAP_USER; // your PageSnap login email
const PAGESNAP_KEY = process.env.PAGESNAP_KEY; // your API key
export async function generateInvoicePdf(invoiceUrl) {
const payload = {
sandbox: false,
contents: {
urls: [invoiceUrl]
},
options: {
format: "A4",
margin_top: "2cm",
margin_bottom: "2cm",
display_header_footer: true,
footer_template:
"<div style='font-size:10px; text-align:right;'>Page <span class='pageNumber'></span> / <span class='totalPages'></span></div>"
}
};
const response = await axios.post(API_ENDPOINT, payload, {
auth: { username: PAGESNAP_USER, password: PAGESNAP_KEY },
headers: { "Content-Type": "application/json" }
});
return response.data.pdf; // returns signed PDF URL
}
Usage example:
const pdfUrl = await generateInvoicePdf("https://yourapp.com/invoice/12345");
console.log("Download invoice PDF:", pdfUrl);
Example: Generate PDF Invoices from Raw HTML
If your invoice is dynamically generated server-side, you can send raw HTML:
const payload = {
sandbox: false,
contents: {
htmls: [
{
html: `
<html>
<body>
<h1>Invoice #INV-2025-001</h1>
<p>Customer: John Doe</p>
<p>Total: $120.00</p>
</body>
</html>
`
}
]
}
};
Useful for apps generating invoices on the fly without hosting them.
Using PageSnap Templates for Reusable Invoice Designs
PageSnap allows you to store reusable HTML templates in your dashboard. Each has a uuid.
Node.js example using templates:
const payload = {
sandbox: false,
contents: {
templates: [
{
uuid: "e8c5b1a4-xxxx-yyyy-zzzz-1234567890ab",
data: {
invoiceId: "INV-1001",
customerName: "Jane Doe",
items: [
{ name: "Subscription", price: "$29" }
],
total: "$29"
}
}
]
}
};
This approach is best for large apps generating thousands of invoices with the same layout.
Automating Recurring or Batch Invoice Generation
To generate many invoices at once (up to 200 per request), use a webhook_url:
const payload = {
contents: { urls: invoiceUrls }, // up to 200 invoices
webhook_url: "https://yourapp.com/webhooks/invoice-pdf",
sandbox: false
};
PageSnap will call your webhook with the final PDF links when processing is complete.
This is perfect for monthly billing cycles.
Saving Invoice PDFs Directly to Your AWS S3
PageSnap can upload PDFs straight to your S3 bucket using s3_path_url:
const payload = {
contents: {
urls: ["https://yourapp.com/invoice/12345"]
},
s3_path_url: "s3://my-invoices/2025/INV-12345.pdf",
sandbox: false
};
Benefits:
- No need to handle file downloads
- Invoice archive stays organized
- Compliance-friendly storage flow
- Works with batch requests too
Best Practices for Reliable Invoice PDFs
✔ Use wait_for_selector if your invoice page loads data asynchronously
✔ Use print_background if your invoice has styled elements
✔ Use header_template and footer_template for page numbers and branding
✔ Avoid exposing your API key — always call PageSnap from your backend
✔ For large batches, use webhook_url to avoid long-running jobs
✔ Test with sandbox: true until layout is perfect (watermarked but free)
All options verified against your official documentation.
Final Thoughts
Building your own invoice PDF system is time-consuming and brittle. With PageSnap, Node.js developers can generate clean, consistent PDFs using:
- URLs
- Raw HTML
- Saved templates
- Batch processing
- S3 delivery
- Webhook automation
Whether you’re running SaaS billing, marketplaces, or internal tools, PageSnap.co lets you automate invoices end-to-end with just a few lines of code.
