Working with documents is my job as a lawyer, and I’m excited to share a new library I’ve been working on: swift-html-to-pdf. This library provides an easy-to-use interface to print HTML strings to PDF documents on iOS and MacOS.
Key Features
Convert HTML strings to PDF documents on both iOS and macOS.
Lightweight and fast: it can handle thousands of documents quickly.
Customize margins for PDF documents.
Swift 6 language mode enabled
Why did I build swift-html-to-pdf?
As a lawyer with a passion for legal tech, a significant part of my work involves generating legal documents. However, I have always found converting HTML to PDFs to be quite cumbersome.
This library is built to address common pain points I faced when dealing with PDF generation from HTML. It provides a modern and easy to use interface API that simplifies the process, allowing me (and you!) to focus on building great documents without worrying about the intricacies of PDF conversion.
Example Usage
Here’s a quick example of how you can get started with swift-html-to-pdf:
import HtmlToPdf
let htmlString = "<html><body><h1>Hello, World!</h1></body></html>"
try await htmlString.print(
to: URL(fileURLWithPath: "path/to/output.pdf"),
configuration: .a4
)
Get Involved
swift-html-to-pdf is open-source and contributions are welcome! Check out the swift-html-to-pdf for more details, and feel free to report issues, suggest features, or submit pull requests.
I hope this library helps you as much as it has helped me. I look forward to your feedback and contributions!
This looks like it could be just what I need. I'm having a lot of trouble trying to do it other ways.
When I try to install via SPM I get this error. I'm assuming it's down to the Swift 6 language mode enabled?
Do you know how I can resolve this without breaking my project?
Failed to resolve dependencies Dependencies could not be resolved because 'swift-html-to-pdf' contains incompatible tools version (6.0.0) and root depends on 'swift-html-to-pdf' 0.2.0..<1.0.0.
I can install it now. However, Xcode forced me to replace print() with Swift.print() in a few places. I assume that's something to do with you overloading the print statement.
The error was:
URLHelper.swift:172:13 Use of 'print' refers to instance method rather than global function 'print(_:separator:terminator:)' in module 'Swift'
Then everything was working nicely.
It worked perfectly with a 2000 page PDF.
This has saved me so much time and hair pulling.
Not at all.
I was using BNHtmlPdfKit, but it hadn't been updated in over a decade and didn't work with Catalyst.
I was looking for an easy drop in replacement since I had a hard time trying to do it myself with web views and PDF Kit. Just kept hitting dead ends.
The fact that I could do it in one line with your package totally made my day
Big news: HtmlToPdf now supports printing PDFs that include images!
Printing images to PDFs on iOS/macOS has always been tricky, with countless threads and articles tackling the issue. Thanks to the brilliant work of @jonathansds, we’ve crafted a sleek (and performant!) solution.