Invoice PDF API
Generate Invoice PDFs from HTML with an API
Build consistent invoice and receipt downloads from trusted HTML using print CSS, page controls, headers, footers, and a server-side API call.
Updated
Key outcomes
Send HTML for private invoice data
Invoices usually contain customer names, addresses, tax identifiers, and line items. Build the final HTML inside your application and send it as the source value instead of exposing a private invoice URL.
This approach also makes the conversion deterministic: the request contains the exact document snapshot, while your application remains responsible for authorization and data access.
- Escape untrusted customer text before adding it to HTML.
- Use absolute HTTPS URLs or embedded data URLs for logos and fonts.
- Keep the API key in a server-side environment variable.
Design the invoice for paper
Use print-specific CSS to define page breaks, avoid splitting totals, and remove interface controls. Choose A4 or Letter explicitly so the same invoice does not reflow across environments.
- Apply break-inside: avoid to totals and line-item groups.
- Reserve enough bottom margin for the footer.
- Test long product names, multiple tax rates, and multi-page invoices.
Generate the PDF on your server
The request below sends raw invoice HTML, enables print media styles, and adds a footer with Chromium page-number placeholders. The response body contains the PDF bytes.
const response = await fetch('https://api.pixeltopdf.com/convert/pdf', {
method: 'POST',
headers: {
'X-API-Key': process.env.PIXELTOPDF_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({
source: invoiceHtml,
format: 'A4',
use_print: true,
filename: `invoice-${invoice.number}.pdf`,
margin: { top: '18mm', right: '16mm', bottom: '22mm', left: '16mm' },
footer: {
height: '12mm',
source: '<div style="font-size:9px;width:100%;text-align:center"><span class="pageNumber"></span> / <span class="totalPages"></span></div>',
},
}),
});
if (!response.ok) throw new Error(`PDF failed: ${response.status}`);
const pdf = Buffer.from(await response.arrayBuffer());Make invoice generation reliable
Store the immutable invoice data used for each document and generate a stable filename. Log the invoice ID and conversion result in your application, but never log the full HTML or API key.
For retries, use the same invoice snapshot. If a font or logo is hosted remotely, make it highly available or embed it so third-party loading cannot change the result.
PixelToPdf returns the generated file to your request. Your application decides whether to stream it immediately, attach it to an email, or store it in your own document system.
Frequently asked questions
Should I send an invoice URL or raw HTML?
Use raw HTML for private or customer-specific invoices. A URL is suitable only when it is public and contains no credentials or sensitive query parameters.
How do I add page numbers?
Send footer HTML with Chromium placeholders such as pageNumber and totalPages, and reserve footer space with the bottom margin.
Can I use my own invoice CSS?
Yes. Include styles in the HTML or send additional CSS in the css request option. Enable use_print when you want print media rules.
Continue building
Build your first conversion
Start with 75 free credits each month. No credit card required.