NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

MacOS - Swift - Create PDF File from HTML String

Forums > Swift

During this lockdown I am learning Swift on MacOS and have hit a wall in trying to create a PDF file from an HTML string

I have the HTML string being displayed in a WKWebView, but any attempt to print it or save it to a PDF produces a blank page.

for exampe: -



    let pdf = wkWebView.dataWithPDF(inside: wkWebView.frame)
    let pdfDocument = PDFDocument(data: pdf)
    pdfDocument?.write(toFile: "test.pdf")

Can anyone give me some example code that can produce a PDF from the HTML ?

Many thanks


Hacking with Swift is sponsored by Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

Not logged in

Log in

Link copied to your pasteboard.