Generate PDFs from Your App
Invoices, tickets, certificates and reports all end up as PDFs. There are three ways to make them, and picking the right one depends on who needs the file and whether it must never change.

Key takeaways
- For "let me print or save this page", a print stylesheet is enough and costs nothing.
- For documents that must be identical every time (invoices, certificates), generate the PDF on the server, store it once, and serve the stored file.
- Page breaks, embedded fonts and sequential numbering cause most PDF bugs; plan for them before the first customer download.
There are three ways to generate PDFs from a web app. The lightest is a print stylesheet: the user clicks Download, your page opens the browser's print dialog, and they choose "Save as PDF". The second builds the PDF in the browser with a library such as pdf-lib, jsPDF or react-pdf. The third generates it on the server, either with a PDF library in a server function or by sending HTML to a hosted HTML-to-PDF service, then stores the file. Use print CSS for convenience copies, the browser for user-made documents, and the server for anything official that must never change after it is issued.
What you will build
A PDF that matches its purpose. Examples that show up constantly: an invoice or receipt after a payment, an event ticket with a QR code, a course completion certificate with the student's name, a monthly report with a chart and a table, a signed booking confirmation. Each has a different answer to two questions: who needs the file, and can it change after it is made?
Choosing an approach
Print stylesheet (no library, no server)
Add an @media print block that hides navigation and buttons, sets margins with @page, and uses black text on white. A "Download PDF" button calls window.print(). It is free, uses your existing HTML, and is perfect for "print this itinerary" or "save my order details". The downside: the result depends on the user's browser, paper size and settings, and the file is not stored anywhere.
Build it in the browser
A library composes the PDF from data in JavaScript and triggers a download. react-pdf lets you write the document as components with its own layout primitives; pdf-lib can also fill and modify existing PDF templates, which is great for certificates designed in a design tool. Output is consistent across browsers. Good for documents users make for themselves: a CV builder, a packing list, a seating chart. Not good for official records, because anyone can edit the code or data running in their own browser.
Generate it on the server
An edge function reads the record from your database, builds the PDF with a library like pdf-lib, saves it, and returns a link. Edge functions are lightweight runtimes, not full browsers, so if you want to turn complex HTML and CSS into a PDF exactly as Chrome would draw it, use a hosted HTML-to-PDF API (DocRaptor, PDFShift and similar services). That needs an account with the service and an API key, which goes in Secrets and is only read by the function.
Data flow for an official document
Take an invoice issued after a payment:
- 01The payment succeeds and your webhook handler marks the order paid.
- 02It assigns the next invoice number inside a database transaction (a sequence or a counter row per business), so two simultaneous orders can never share a number and numbers never skip.
- 03It snapshots everything the invoice shows (customer name and address, line items, prices, tax rate, currency) into an invoices row. Later edits to the product or the customer do not alter an issued invoice.
- 04An edge function renders the PDF from that snapshot and uploads it to a private storage bucket at a path like invoices/2026/INV-2026-0042.pdf, saving the path on the invoice row.
- 05When the customer clicks Download, your app checks they own the invoice and creates a signed URL (valid for an hour) to the stored file. Re-downloads serve the same bytes.
Never regenerate an issued invoice on the fly: if you change your template next year, last year's invoices would quietly change too. Corrections are issued as a new credit note, not an edit. For payments themselves, see accept payments in your app.
A brief you can paste
When an order is paid, issue an invoice PDF. Assign the next number in the format INV-YYYY-NNNN inside a transaction so numbers are sequential with no gaps or duplicates. Save a snapshot of the customer details, line items, tax and totals in an invoices table. Generate the PDF in an edge function (A4 for EU customers, US Letter for US customers): our logo top left, business details top right, a line items table that repeats its header on each page and never splits a row across pages, totals bottom right, payment terms in the footer with "Page X of Y". Embed the font we use on the site. Store the file in a private invoices bucket and save its path on the row. In the customer's account, list invoices with a Download button that creates a one-hour signed link. Never regenerate an issued invoice.
Layout details that bite
- Paper size. The US uses Letter (8.5 by 11 inches), most of the world uses A4 (210 by 297 mm). Pick per customer or region, and leave margins of at least 15 mm so home printers do not clip content.
- Page breaks. Long tables must repeat their header on each page and never split a row. In HTML-based rendering, break-inside: avoid on rows and display: table-header-group on the header do this.
- Fonts. Embed the fonts you use. A font that is not embedded falls back to something else on the reader's machine, and non-Latin scripts (Arabic, Japanese, Hebrew) may not render at all without an embedded font that covers them.
- Images and QR codes. Use high-resolution or vector images; a 72 dpi logo looks blurry in print. Generate QR codes as vectors where possible and keep them at least 2 cm wide.
- File size. Big photos balloon PDFs. Compress images before embedding; a one-page invoice should be well under 200 KB.
- Accessibility. Set the document title and language, and use real text rather than a screenshot of text, so screen readers and search can read it.
Design the template once, then test it with ugly data: a 60-character company name, an address with five lines, a single order with 80 line items, a zero-amount line, a customer name in Arabic or Japanese. Those cases break layouts that look perfect with "Acme Inc" and three items. Keep a handful of these test records in your database and regenerate their PDFs whenever the template changes.
Pitfalls
- Public buckets for private documents. Invoices and certificates contain names and addresses. Keep them in a private bucket and hand out short-lived signed links, as covered in storing files and images.
- Generating on every click. It is slow and costs compute for no reason. Generate once at issue time and serve the stored file.
- Browser-made "official" documents. A certificate built in the user's browser can be edited before it is saved. If authenticity matters, generate on the server and add a verification link or code that checks your database.
- Time zones on dates. An order paid at 11:30 pm in Los Angeles is already the next day in UTC. Print dates in the business's time zone and be consistent.
Tickets are a common second PDF. The event ticketing guide covers the codes on them and how check-in validates each one exactly once.
Frequently asked questions
What is the easiest way to let users download a page as a PDF?
Add a print stylesheet and a button that calls window.print(). Users choose Save as PDF in the dialog. It needs no library or server, but the output depends on their browser settings.
Should I generate PDFs in the browser or on the server?
Browser generation suits documents users create for themselves. Official documents such as invoices and certificates should be generated on the server from stored data and saved, so they cannot be altered and every download is identical.
How do I stop tables splitting badly across PDF pages?
Repeat the table header on every page and prevent rows from breaking across pages. In HTML-based rendering, use display: table-header-group on the header and break-inside: avoid on rows.
Where should generated PDFs be stored?
In a private storage bucket, with the file path saved on the related database row. Serve downloads through short-lived signed links after checking the user is allowed to see the document.

