The journal · 27 November 2026 · 6 min read
HTML to PDF in Swift With WKWebView or a Rendering API and What a Year Costs
WKWebView makes a PDF on the device, but output varies by OS version and Vapor on Linux has no WebKit. At 15,000 documents a month the API route costs about $888 in year one against about $3,880 for a self-run browser.
Live demo
Runs in your browser. Nothing is uploaded. One PDF free to try, then a plan.
Your HTML
Live preview
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 planSitePDF is an HTML to PDF API, and this article answers the question a Swift team asks before shipping invoices or reports as PDF: should the PDF be made on the device with WKWebView, or on a server, and what does the server route cost. The short answer is that on-device is fine for a receipt the user prints from their own phone, and a server render is the better route once the PDF must look the same for every customer, be emailed, or be kept as a record.
The short answer
Use WKWebView on the device when the user generates and shares the document themselves and a layout difference between iOS versions does not matter. Use a rendering API when the PDF is sent by your backend, must match your web invoice, or must be archived with a capture time. Vapor and other server-side Swift cannot use WebKit on Linux, so there the choice is a rendering API or your own Chromium.
What are the options for HTML to PDF in Swift?
| Route | Where it runs | Multi page output | What you operate |
|---|---|---|---|
WKWebView createPDF | iOS, iPadOS, macOS | One page of the rect you ask for, so you slice pages yourself | Nothing, but output varies with OS version |
| UIPrintPageRenderer with a print formatter | iOS, iPadOS | Yes, paginated for a paper size | Nothing, but you write the pagination code |
| Own Chromium next to a Vapor app | Linux container | Yes, CSS page breaks work | Browser, fonts, pool, timeouts |
| Rendering API over HTTP | Managed by the vendor | Yes, CSS page breaks work | An API key and one request |
Apple documents createPDF(configuration:completionHandler:) in the WebKit reference. It captures a rectangle of the loaded page. If your invoice is longer than one page you either size the rectangle to the whole content and accept one tall page, or you cut it into page-sized pieces and stitch them in PDFKit.
Why does a device PDF differ from the web invoice?
Three reasons come up again and again. The web view loads fonts and images asynchronously, so a call made on didFinish can capture before they arrive. Print CSS such as @page margins and break-inside: avoid is applied to printing but not always to a plain capture. And the engine is the one that ships with the user's OS, so a customer on an older iOS version can produce a different layout than the one you tested.
For a document you email to a customer, a support agent needs to be able to reproduce exactly what was sent. That is hard when the file was drawn on thousands of different phones, and easy when it was rendered by one engine on a server.
What does a server render cost in year one at 15,000 documents a month?
These are planning assumptions, not measurements, so replace them with your own. A Swift team sends 15,000 invoices and statements a month from a Vapor backend. Running its own Chromium means 16 hours to build the worker, fonts and monitoring, then 1.5 hours a month on upgrades and incidents, at $100 an hour, plus a $40 a month container.
| Item | Basis | Year one cost |
|---|---|---|
| Build the Chromium worker | 16 hours at $100 | $1,600 |
| Upgrades and incidents | 1.5 hours a month at $100 | $1,800 |
| Worker container | $40 a month | $480 |
| Self-hosted total | $3,880 | |
| Integrate the API from Swift | about 3 hours at $100 | $300 |
| SitePDF Growth, 50,000 renders a month | $99 list, $588 billed yearly | $588 |
| API total | $888 |
At 15,000 documents a month you are above the 10,000 renders of Starter API ($49 a month, $288 billed yearly), so Growth is the plan that fits. Only the $588 repeats in year two. If your team already runs a browser fleet, the self-hosted line is lower and the gap shrinks.
How do you call a rendering API from Swift?
With URLSession it is one POST. Send the HTML your template produces, for example from Leaf in Vapor, and read back a link to the PDF.
var request = URLRequest(url: URL(string: "https://api.sitepdf.com/v1/render")!)
request.httpMethod = "POST"
request.setValue("Bearer \(ProcessInfo.processInfo.environment["SITEPDF_KEY"]!)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONSerialization.data(withJSONObject: [
"html": invoiceHTML, "format": "Letter", "archive": true
])
let (data, _) = try await URLSession.shared.data(for: request)
The render runs in managed Chromium, so flexbox, web fonts and CSS page breaks behave as they do in a desktop browser, on every request. With archive set to true the render is stored with its capture time and a SHA-256 hash, described on the PDF integrity check page. Archive retention is 90 days on Starter API, 365 on Growth and three years on Scale. The PDF generator API page shows the same call in other stacks.
Keep the API key on your server. Do not put it in the app binary, because anyone can extract it. The app asks your backend, and the backend asks SitePDF.
Who this is for and who it is not for
It is for Swift teams whose backend, or whose app through a backend, sends invoices, statements or reports built from HTML and who want one consistent layout. It is not for an app that only lets a user print a screen to their own device, where WKWebView is free and sufficient, nor for teams that need PDF signing or must keep data inside their own network.
What to do this week
Render your most complicated invoice with createPDF on two iOS versions and compare them. If they match and the document never leaves the device, stay on-device. If they differ, post the same HTML to SitePDF from a short test and compare. The pricing page shows the plan for your volume, and HTML to PDF API for SaaS covers invoices and billing-day bursts.
Frequently asked questions
How do I convert HTML to PDF in Swift?
On Apple devices load the HTML in a WKWebView and call createPDF, or use UIPrintPageRenderer for paginated output. On a Linux server there is no WebKit, so you either run Chromium yourself or POST the HTML to a rendering API and receive the PDF.
Why is my WKWebView PDF one long page?
createPDF captures a single rectangle. If that rectangle covers the whole content you get one tall page. For paper-sized pages use UIPrintPageRenderer, or slice the capture into page-sized rectangles and assemble them in PDFKit.
Can Vapor generate PDFs from HTML?
Yes, but not with WebKit. A Vapor app on Linux can drive a headless Chromium it hosts, or send the HTML to a rendering API. Both give browser quality output; the API removes the browser operations work.
How much does an HTML to PDF API cost for a Swift backend?
SitePDF Starter API is $49 a month for 10,000 renders, or $288 billed yearly. Growth is $99 a month for 50,000 renders and Scale is $299 a month for 250,000. Count one render per document.
Written by the team behind SitePDF, an HTML to PDF API that archives every page it renders. The in-browser converter is free to try, and API plans start at $49 a month, half price billed yearly.