HTML Invoice to PDF: Multi-Page Invoices with an API
Updated by Cyril Nicodème
Turning an HTML invoice into a PDF takes one POST to PDFShift. You don't run Chromium: we do. A long invoice needs one flag, use_print: true, and two lines of CSS to print right. The flag repeats the table's header row on every page, but on its own it still cut a row in half on our 40-line test invoice and pushed "Total due" onto page 3, away from its subtotal and tax. Two break-inside: avoid rules fix that. With both, a 150-line invoice came out as 8 clean pages.
How do I turn an HTML invoice into a PDF with an API?
Send the invoice as a full HTML document in source, with your API key in the X-API-Key header. Four more fields make a long invoice print well: format for the paper size, margin for white space around the content, use_print to render with print styles, and footer for the page numbers. Here it is in Node.js 18 or later:
import { readFile, writeFile } from "node:fs/promises";
const html = await readFile("invoice.html", "utf8");
const res = await fetch("https://api.pdfshift.io/v3/convert/pdf", {
method: "POST",
headers: {
"X-API-Key": process.env.PDFSHIFT_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
source: html,
format: "A4",
margin: "40px 40px 20px 40px",
use_print: true,
footer: {
source: '<div style="font-size:10px; width:100%; text-align:center; color:#555;">Page {{page}} of {{total}}</div>',
height: "40",
},
}),
});
if (!res.ok) throw new Error(`PDFShift ${res.status}: ${await res.text()}`);
await writeFile("invoice.pdf", Buffer.from(await res.arrayBuffer()));
And in Python with requests:
import os
import requests
with open("invoice.html", encoding="utf-8") as f:
html = f.read()
res = requests.post(
"https://api.pdfshift.io/v3/convert/pdf",
headers={"X-API-Key": os.environ["PDFSHIFT_API_KEY"]},
json={
"source": html,
"format": "A4",
"margin": "40px 40px 20px 40px",
"use_print": True,
"footer": {
"source": '<div style="font-size:10px; width:100%; text-align:center; color:#555;">Page {{page}} of {{total}}</div>',
"height": "40",
},
},
timeout=120,
)
res.raise_for_status()
with open("invoice.pdf", "wb") as f:
f.write(res.content)
The response body is the PDF itself. invoice.html is your template with the data already filled in (by Handlebars, Jinja2 or whatever your app uses), plus the two print rules from the CSS section below in its <style> block. Both snippets returned our 40-line invoice as a 3-page PDF. If your invoice is a React component, our React to PDF guide shows how to turn it into HTML first.
Why does a long invoice break across PDF pages?
A browser lays out a page for the screen, then cuts it into sheets wherever the paper ends. A table row, a totals block or your payment terms can land on that cut. We built one invoice template and sent it to PDFShift with 40 and 150 line items, set up four ways. The set-ups with print CSS also had the margin and footer from the code above; the other two had neither. Here's what each one did to the 40-line invoice:
| Set-up | Row cut in half | Header repeats | Totals whole | Pages, 40 / 150 lines |
|---|---|---|---|---|
| No fixes | Yes | No | Yes | 3 / 7 |
| Print CSS only | No | No | Yes | 3 / 8 |
use_print only | Yes | Yes | No | 3 / not run |
| Both | No | Yes | Yes | 3 / 8 |
Neither fix works alone. Print CSS never repeated the header row. use_print left subtotal and tax at the foot of page 2, so a customer who opens page 3 sees "Total due" with nothing above it.


Which CSS rules keep invoice rows and totals together?
Two rules, in the template's <style> block:
The first tells the browser never to put a page break inside a table row, so a line item moves to the next page whole. The second does the same for the totals block and the payment terms. Swap in whatever classes wrap those in your template. Older templates may use page-break-inside: avoid, which is the legacy name for the same property.
You can skip thead { display: table-header-group; } if your table has a real <thead>: that's already how a browser treats it. We took the rule out and sent the 40- and 150-line invoices again, and the PDFs came back identical apart from their timestamps. It only matters when your header row is built from <div>s.
Set the margin in the request, too. With none, our template printed its text right against the edge of the sheet. "40px 40px 20px 40px" gave us room on every side, in CSS order: top, right, bottom, left.
How do I add "Page 1 of 3" to every invoice page?
Use the footer field. Its source is a small piece of HTML, and two variables are filled in on each page: {{page}} for the current page and {{total}} for the page count. The footer doesn't get your page's CSS, so style it inline or with its own <style> tag.
height sets the space between the end of your content and the bottom of the sheet. With "height": "40" and our 20 px bottom margin, the numbers sat clear of the last table row on every page. To leave the footer off the first pages, set start_at to the page it should start on. A header works the same way, and either one can also show {{date}} and {{title}}.
On our 150-line invoice, the totals and payment terms stayed together on the last page, and the footer counted from "Page 1 of 8" to "Page 8 of 8".

How do I put the invoice number on every page?
Put it in the document's <title> and print {{title}} in a header. Page 2 of a long invoice is just rows. A customer who prints it, or forwards one page to their accounts team, can lose track of which invoice it belongs to. Our template's title was Invoice INV-2026-0142, and we added this next to the footer from the first code block:
"header": {
"source": "<div style=\"font-size:10px; width:100%; text-align:right; color:#555; padding-right:40px;\">{{title}}</div>",
"height": "40"
}
We raised the top margin to 60px ("60px 40px 20px 40px") so the header had room above the table. Every page of the 40-line invoice then carried "Invoice INV-2026-0142" in its top right corner, still 3 pages and 1 credit. The title can carry anything your template fills in, like the customer's name or the due date.
Does a longer invoice take longer or cost more?
Not in our test. PDFShift took about 2.1 to 2.3 seconds per invoice whether it had 5, 40 or 150 lines. Every PDF cost 1 credit: we count 1 credit per 5 MB of output, and our invoices came out between 31 KB and 53 KB. On the $9 plan, 500 credits a month is 500 invoices like these, 5 lines or 150.
A web font made the file bigger, but not the bill. We loaded Lora with a plain <link> tag and no wait setting: it was embedded in the PDF, and the 40-line invoice grew from 41 KB to 139 KB, still 1 credit. Prices for every plan are on the PDF generator API page.
Frequently asked questions
Can I get a link to the invoice instead of the PDF file?
Yes. Add a filename of at least 7 characters (letters, digits, - and _) and the response becomes JSON with a url to the PDF, kept for 2 days. To keep it longer, send it to your own S3 bucket with s3_destination.
How do I email the invoice to my customer?
The response body is the PDF file itself, so attach those bytes to your email as invoice.pdf with your usual mail library or email API. If you'd rather send a link, add a filename and email the url that comes back.
Can I convert my invoice page's URL instead of sending the HTML?
Yes. Put the address in source and PDFShift loads the page and converts it, with the same parameters.
Can I generate a whole month of invoices at once?
Yes. Send one request per invoice with a webhook URL. Each call is queued at once and PDFShift posts a link to each finished PDF to your server. Our batch PDF guide has the sender and receiver code.
Does Letter paper change the page count?
Not for our 40-line invoice: with "format": "Letter" it came out as 3 pages, the same as on A4.
Can I test the layout without using credits?
Yes. Add "sandbox": true. Sandbox PDFs don't count against your credits, carry a watermark, and are limited to 10 a minute.
To try it on your own template, create a free account: you get 50 credits a month, no card needed.
Did a snippet fail, or is something unclear?
Drop us a line and we'll look at it.