Swiftpack.co - Package - aaronland/swift-webster

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.


Swift package for generating a PDF file from a URL (rendered by WebKit).


Swift package for generating a PDF file from a URL (rendered by WebKit).


import Webster

guard let source_url = URL(string: "https://example.com") else {
    fatalError("Invalid source URL.")

let w = Webster()

w.dpi = 72.0
w.width = 11.0
w.height = 8.5
w.margin = 0.5

on_complete = func(result: Result<Data, Error) -> Void {

    if case .failure(let error) = result {
        fatalError("Failed to generate PDF file, \(error.localizedDescription)")

    if case .success(let data) = result {
        // Do something with data here

w.render(source: source_url, completionHandler: on_complete)


Prior to iOS 14 and MacOS 10.16 this package is uses NSPrintOperation to render a WebView. Ideally I would like NSPrintOperation to print directly to a Data instance but it's not possible to do this. Instead the render method creates a temporary file, writes to it, reads the data and removes the temporary file on exit. This introduces extra overhead but, hopefully, keeps the interface a little more agnostic about how the resultant PDF document is used.

Really what we want is to be able to pass the NSPrintOperation method something like an abstract "writer" similar to the Go language io.Writer interface but this is not possible in Swift. Perhaps in future releases the WKWebView.createPDF method will adopt the publish/subscribe model used in Apple's Combine framework but today it does not.


This build's on @msmollin's original webster and currently exists as a separate project because it is full of Swift Package Manager -isms and I am not sure what the best way to create a PR is yet.

See also


Stars: 0
Last commit: Yesterday



0.0.2 - 2021-02-24T06:08:58

  • Add support for optional page bleed per issue #7