Your app needs to make a PDF. An invoice, a quote, a certificate, a monthly report. The AI tool builds a button in minutes. Then it goes wrong. It works on your laptop and times out live. The PDF is blank, or the fonts are wrong. Chrome will not start in the function. A table is cut in half at the page break. Worst of all, a user gets someone else's invoice. Vendor facts below come from the official docs, checked in November 2026. Hosting limits change often, so check the linked pages again before you rely on a number. This is not legal or tax advice.
Short answer: Pick the simplest way that fits. If the user only wants to print or save what they see, let the browser do it. For anything official, like an invoice, generate the PDF on the server, once, and keep the file. Check your host's time and size limits first. Store the file privately and give the user a short-lived link, after checking that the file is theirs. If it takes long, make it in the background and email the link.
Option 1: let the browser print it
The simplest way is a "Download this page" button that calls window.print(). MDN says print() "Opens the print dialog to print the current document." The user then picks "Save as PDF". You control the look with print CSS.
@media printholds the styles that only apply on paper. Hide menus and buttons there.@pagesets the paper. MDN says the at-rule "targets and modifies the page's dimensions, orientation, and margins." Use it for A4 and margins.break-inside: avoidstops a table row from splitting. MDN says the value "Avoids any break (page, column, or region) from being inserted within the principal box."
@page { size: A4; margin: 15mm; }
@media print {
nav, button { display: none; }
tr { break-inside: avoid; }
}
This is fine for "print this report". It is not fine for invoices you must keep or email. The user's browser makes the file, so you do not have a copy, and it can look different on every device.
Option 2: headless Chrome on a server
Here your server opens your own page in a browser nobody sees, and saves it as a PDF. Puppeteer's page.pdf() "Generates a PDF of the page with the print CSS media type." Playwright's page.pdf() "generates a pdf of the page with print css media." So your print CSS from option 1 applies here too. Both docs also warn: "By default, page.pdf() generates a pdf with modified colors for printing." If a colored header comes out white, that is why. Their docs name a fix: the -webkit-print-color-adjust property.
This gives the best match with your web design. It is also where most live failures happen.
Limits that bite
A real browser is heavy. It is a large download next to a normal function, and it needs time to start. Check your host's limits, and see whether it offers a way to run a browser at all. Today these pages say:
- Netlify: in its functions settings, "Synchronous execution limit" is "60 seconds" and "Background execution limit" is "15 minutes". The same page names "image or PDF manipulation" as work that can hit memory errors or timeouts. Its background functions page says they "run for up to 15 minutes".
- Vercel: the limits page lists "Size (uncompressed)" as "250 MB" and, on the Hobby plan, "300s default and maximum" duration. Other plans differ, so read the page for yours.
- Supabase: the Edge Functions limits list the wall clock limit as "Free plan: 150s" and "Paid plans: 400s", and a maximum function size of "20MB" when bundled locally with the CLI.
Put plainly: a full Chrome may not fit or may be too slow on your plan. Then you slim it down, move the job to a host that allows a browser, or use a PDF service. Ask your AI tool which of these your host supports, and test it live, not only on your laptop.
Fonts and blank pages
A blank or wrong-font PDF usually means the page was saved before it finished loading. Wait for the fonts. MDN says document.fonts.ready resolves "once the document has completed loading fonts, layout operations are completed, and no further font loads are needed." Await it before you call pdf(). Also load fonts from your own site, not from a link that may be blocked on the server.
await page.setContent(html);
await page.evaluate(
() => document.fonts.ready
);
const pdf = await page.pdf({
format: 'A4'
});
Option 3: a PDF library
A library builds the PDF directly. It does not use a browser, so it is small and fast.
- pdf-lib says: "Create PDF documents from scratch, or modify existing PDF documents." It can draw text, images and graphics, and "Embed your own fonts."
- PDFKit says it "makes creating complex, multi-page, printable documents easy."
This fits a simple, fixed layout, like a certificate or a one-page invoice. It takes more work when the content grows. A long table with wrapping text means you place and split every line yourself. If your invoice has a variable number of lines, test it with 1, 30 and 200 lines.
Invoices need extra care
- Generate the invoice once, when it is issued. Store the file. Never rebuild an old invoice from current data. Prices, names and addresses change, and the old invoice must not.
- Number invoices in sequence. The EU's VAT invoicing page lists "Date of issue" and "Unique sequential number identifying the invoice" among the details a full invoice must contain. Your Europe says "Your VAT identification number must be shown on all invoices you give to customers", on its VAT page.
- Rules differ by country and by case. Ask an accountant what your invoices need before you launch.
- Let two requests for the same order give one invoice. See duplicate records from double clicks and retries.
If you take payments with Stripe, Stripe Invoicing can make the PDFs for you. Stripe's email docs say: "When Stripe emails an invoice, we automatically include a PDF attachment of the same invoice to assist your customer with record keeping." That can save you from building any of the above. For the payment side, see Stripe payments in your AI-built app.
Store and send it safely
- Keep the files in a private bucket, not a public one. Basics are in file uploads: what to check.
- Give the user a short-lived link. In Supabase, createSignedUrl takes
expiresIn, "The number of seconds until the signed URL expires." - Before you make the link, check on the server that this user may see this file. This is the "someone else's invoice" bug. See admin pages, roles and IDOR.
- Prefer sending a link over an attachment. Resend's attachment docs say: "Emails can be no larger than 40MB (including attachments after Base64 encoding)." Check your own email provider for its limit.
Long jobs: do them in the background
A monthly report with thousands of rows will not fit in a web request. Do not make the user stare at a spinner. Save a "report requested" row, make the PDF in a background job, and email a link when it is ready. See scheduled jobs on Netlify, Vercel and Supabase. Jobs get retried, so make each one safe to run twice: check whether the file already exists before you make it again.
Prompts for Lovable, Bolt or Cursor
- "Add print CSS so the report page prints cleanly on A4 with 15mm margins. Hide the menu and buttons when printing. Never split a table row across pages."
- "Generate invoice PDFs on the server when an invoice is issued, not in the browser. Store each PDF in a private bucket. Return a signed link valid for 10 minutes, and only if the logged-in user owns that invoice. Never regenerate an issued invoice."
- "Move PDF generation to a background job. When the user asks for a report, save a pending record and return at once. When the file is ready, email the user a link. If the job runs twice, it must not create a second file."
Checklist
- Chosen the simplest option: browser print, server browser, or library.
- Invoices are made on the server, once, and the file is stored.
- Invoice numbers run in sequence (ask your accountant whether gaps are allowed).
- I checked my host's time and size limits and tested the PDF on the live site.
- Fonts are loaded before the PDF is saved. Colors and page breaks look right.
- Tested with 1, 30 and 200 lines.
- Files are in a private bucket. Links expire.
- The server checks that the user owns the file.
- Long reports run in the background and email a link.
- A retry cannot make a second invoice.
- An accountant has checked what my invoices must contain.
Want this fixed for you?
I finish and fix apps built with Lovable, Bolt, Cursor or Replit, including PDF invoices and reports that work on the live site. Or send me the details and I'll reply within 24 hours.