Skip to content

Svelte to PDF

Svelte to PDF With a SvelteKit PDF Generation API

Turn a Svelte component or a SvelteKit route into a real, selectable PDF from one fetch in a server endpoint. Managed Chromium lays out your markup, so scoped styles, Tailwind and web fonts print the way they look on screen, and no browser has to fit inside your adapter. Try it on any URL or HTML below.

  • PDF and archive in one call
  • Plans from $49 a month
  • Your HTML stays yours

Live demo

Runs in your browser. Nothing is uploaded. One PDF free to try, then a plan.

Your PDF is downloading. That was the free one. The Starter plan converts as many as you need, for $49 a month or $288 a year, half price.

See the plan

Short answer

The reliable way to go from Svelte to PDF is to render the component to HTML on the server and let a real browser print it. In SvelteKit that is a +server.ts endpoint: call render() from svelte/server, inline your CSS, POST the HTML to SitePDF and return the PDF. Text stays selectable, page breaks follow your print CSS, and it runs on serverless adapters where Chromium will not start. Plans start at $49 a month for 10,000 renders.

Written and fact checked by the SitePDF team.

Four ways to get a PDF out of a Svelte app

Svelte ships no PDF export, so every team ends up on one of these routes. They split on two questions: does the PDF contain real text, and can it be made without a person clicking a button.

Approach Where it runs Output Works on serverless adapters Best for
window.print() with print CSS The user's browser True vector PDF, but the user picks paper, margins and headers in the dialog Not applicable, nothing runs on the server A one off printout a signed in user starts themselves
html2pdf.js or jsPDF in a component The user's browser html2pdf.js embeds a canvas image, jsPDF needs every element placed in code Not applicable A short receipt or label where image text is acceptable
Puppeteer or Playwright in a Node adapter Your own server True vector PDF No, Chromium is too large for most function limits and cold starts hurt Teams already running adapter-node on a long lived box
Rendering API from a +server.ts endpoint
SitePDF
Managed Chromium, called with fetch True searchable vector PDF with headers, footers and page numbers Yes, it is one HTTP request Invoices, statements and reports sent from a workflow, with a dated copy kept

The honest read: if your users only print what they see, window.print() plus a print stylesheet is free and produces real text, so start there. The API earns its fee when nobody is at the keyboard, for example an invoice emailed after a webhook, or when every customer must get the same paper size and footer.

Render the component to HTML inside a SvelteKit endpoint

Svelte 5 ships a server renderer in the box. render() from svelte/server takes a component and its props and returns body and head strings, which is everything a printer needs except your CSS. Put the call in a +server.ts file so it never runs in the browser and your API key stays on the server.

// src/routes/invoices/[id]/pdf/+server.ts
import { render } from "svelte/server";
import Invoice from "$lib/Invoice.svelte";
import invoiceCss from "$lib/invoice.css?inline";
import { SITEPDF_KEY } from "$env/static/private";

export async function GET({ params, locals }) {
  const invoice = await locals.db.invoice(params.id);
  const { body, head } = render(Invoice, { props: { invoice } });
  const html = `<!doctype html><html><head>${head}<style>${invoiceCss}</style></head><body>${body}</body></html>`;

  const res = await fetch("https://api.sitepdf.com/v1/render", {
    method: "POST",
    headers: { Authorization: `Bearer ${SITEPDF_KEY}` },
    body: new URLSearchParams({ html, format: "Letter", archive: "true" }),
  });
  const { pdf_url } = await res.json();
  return Response.redirect(pdf_url, 303);
}

The detail that trips most people: component styles are compiled into your bundle, not into the string render() returns. Keep the document styles in one plain stylesheet and import it with ?inline, as above, or link an absolute stylesheet URL in the head. Fonts follow the same rule, so reference them by absolute URL. If a font still falls back, the checklist in why fonts do not load in HTML to PDF covers the usual causes.

Or point the renderer at a live route

If the invoice already exists as a page in your app, skip the string building. Send the route URL instead of html, and the renderer loads it like a visitor, runs your hydration, and prints it. Add wait_for with a selector that only appears once data has loaded, such as #invoice-total, so a chart or a lazy table is never captured half drawn. The guide to waiting for content before you convert explains when a selector beats a fixed delay. Protect the route with a signed, expiring token in the query string rather than your session cookie.

Both inputs hit the same endpoint, described on the URL to PDF API page. Teams moving between frameworks reuse the call unchanged, and the Vue to PDF and Next.js PDF generator pages show the same pattern for those stacks.

Why the browser-side libraries disappoint on invoices

The Svelte ecosystem has several small PDF packages, and most of them are viewers rather than generators. The generators that run in the browser mostly wrap html2pdf.js, which draws your component onto a canvas with html2canvas and drops that bitmap into a PDF. That is fine for a quick export of one screen. On an invoice it means the amounts cannot be selected or searched by the customer's accounting software, a long table of line items is cut wherever the image happens to end, and Tailwind v4 colors written with oklch() throw during capture.

The other common route is to install Puppeteer next to adapter-node. That works on a server you keep running, and it is free. It stops working the moment you switch to a serverless or edge adapter, because the Chromium binary is larger than typical function limits and edge runtimes cannot start a browser process at all. The Puppeteer alternative page compares what it takes to keep a browser pool healthy against handing that job to an API.

A worked example for a SvelteKit SaaS

Take a SvelteKit billing app on a serverless adapter that issues 3,000 invoices a month, each two pages built from about 20 KB of HTML, plus around 400 ad hoc statement exports by users. That is 3,400 renders a month, well inside the Starter API plan at $49 a month (or $288 billed yearly) for 10,000 renders with a 90 day archive. Each invoice call returns a link in about two seconds, so a month end batch sent eight at a time finishes in roughly 15 minutes.

The alternative is a separate always on container just to host Chromium for those 3,400 documents, plus the time to keep its fonts, sandbox flags and memory limits right. Our Playwright production cost breakdown puts numbers on that upkeep for a larger volume. Under 10,000 documents a month the API is usually the cheaper line on the budget, and you get a dated copy of every invoice you sent, which helps when a customer disputes what was on it.

Keep a dated copy of each document

Add archive=true and SitePDF stores a timestamped, unchanged copy of exactly what it rendered, retrievable later by document id. For a billing app that answers the question every support team eventually gets: what did the invoice say on the day we sent it. Plan limits and retention periods are on the pricing page, and the invoice PDF API guide goes deeper on numbering, totals and multi page line items.

Post HTML rendered by svelte/server, or pass the URL of a SvelteKit route.
curl https://api.sitepdf.com/v1/render \
  -H "Authorization: Bearer $SITEPDF_KEY" \
  -F url=https://app.example.com/invoices/8842?token=signed \
  -F format=Letter \
  -F wait_for="#invoice-total" \
  -F archive=true

{
  "pdf_url": "https://api.sitepdf.com/v1/documents/doc_9s2kd.pdf",
  "pages": 2,
  "rendered_in_ms": 1740
}

This is the documented API call shape. Full request and response walkthrough.

Questions about this job

How do I convert a Svelte component to PDF?
Render the component to HTML on the server with render() from svelte/server, add your CSS inline, and send that HTML to a browser-based renderer that returns a PDF. In SvelteKit, do it in a +server.ts endpoint so the API key stays private. The result has real, selectable text instead of a canvas image.
Can SvelteKit generate a PDF on Vercel or Netlify?
Yes, if the browser does not run inside your function. Full Chromium is too large for typical serverless limits and edge runtimes cannot start it. Call a rendering API with fetch from a +server.ts endpoint instead; the function only sends HTML and returns a link, so it stays small and fast.
Why are my component styles missing from the PDF?
render() from svelte/server returns markup, not the CSS your build extracts into separate files. Import one document stylesheet with the ?inline suffix and put it in a style tag, or link an absolute stylesheet URL in the head. Fonts need absolute URLs for the same reason.
Is window.print() good enough for PDFs in Svelte?
For a page a user prints themselves, often yes. It produces real text and costs nothing. It falls short when PDFs must be created without a person, for example invoices emailed from a webhook, or when every customer must get the same paper size, margins, headers and page numbers.
Does it work with Svelte 4?
Yes. In Svelte 4 the server renderer is Component.render(props), which returns html, head and a css object. Inline css.code into a style tag and post the full document. The rendering call to SitePDF is identical, since the API only sees HTML, not the framework version that produced it.

Index

More PDF and archiving tools

Get started

Render and archive with one API call

A pixel perfect PDF and a dated copy of the page from the same request. Plans from $49 a month, half price billed yearly.

HTML to PDF, in your browser

Buy the plan