Updated for Xcode 14.2
New in iOS 16
SwiftUI’s ImageRenderer
class can render views any SwiftUI views to PDFs, and yes: all the text and shapes remain vectors, so they scale up beautifully.
Creating a PDF with ImageRenderer
takes eight steps:
render()
on the image renderer to start your rendering code.CGContext
object to handle the PDF pages.Once that finishes the PDF URL is yours to do with as you please.
I know that sounds like a lot of work, but the total amount of code isn’t so bad. Here’s a complete example that renders a view to a PDF to be exported using ShareLink
, with comments matching the explanation above:
@MainActor
struct ContentView: View {
var body: some View {
ShareLink("Export PDF", item: render())
}
func render() -> URL {
// 1: Render Hello World with some modifiers
let renderer = ImageRenderer(content:
Text("Hello, world!")
.font(.largeTitle)
.foregroundColor(.white)
.padding()
.background(.blue)
.clipShape(Capsule())
)
// 2: Save it to our documents directory
let url = URL.documentsDirectory.appending(path: "output.pdf")
// 3: Start the rendering process
renderer.render { size, context in
// 4: Tell SwiftUI our PDF should be the same size as the views we're rendering
var box = CGRect(x: 0, y: 0, width: size.width, height: size.height)
// 5: Create the CGContext for our PDF pages
guard let pdf = CGContext(url as CFURL, mediaBox: &box, nil) else {
return
}
// 6: Start a new PDF page
pdf.beginPDFPage(nil)
// 7: Render the SwiftUI view data onto the page
context(pdf)
// 8: End the page and close the file
pdf.endPDFPage()
pdf.closePDF()
}
return url
}
}
Download this as an Xcode project
SPONSORED Build a functional Twitter clone using APIs and SwiftUI with Stream's 7-part tutorial series. In just four days, learn how to create your own Twitter using Stream Chat, Algolia, 100ms, Mux, and RevenueCat.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.