Swiftpack.co - aaronland/swift-webster as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by aaronland.
aaronland/swift-webster v0.0.3
Swift package for generating a PDF file from a URL (rendered by WebKit).
⭐️ 1
🕓 1 year ago
.package(url: "https://github.com/aaronland/swift-webster.git", from: "v0.0.3")

swift-webster

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

Example

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)

Notes

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.

Credits

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

GitHub

link
Stars: 1
Last commit: 2 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Dependencies

Release Notes

0.0.3
1 year ago
  • Disable checks and code for 10.16 since the code is not working as expected

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics