Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/coenttb/swift-html-to-pdf
A Swift package providing an easy-to-use interface for concurrently printing HTML to PDF on iOS and macOS.
https://github.com/coenttb/swift-html-to-pdf
html pdf pdf-converter swift
Last synced: about 2 months ago
JSON representation
A Swift package providing an easy-to-use interface for concurrently printing HTML to PDF on iOS and macOS.
- Host: GitHub
- URL: https://github.com/coenttb/swift-html-to-pdf
- Owner: coenttb
- License: apache-2.0
- Created: 2024-07-15T13:54:56.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2024-07-30T15:43:32.000Z (about 2 months ago)
- Last Synced: 2024-07-30T19:49:48.906Z (about 2 months ago)
- Topics: html, pdf, pdf-converter, swift
- Language: Swift
- Homepage: https://tenthijeboonkkamp.nl
- Size: 107 KB
- Stars: 10
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# HtmlToPdf
HtmlToPdf provides an easy-to-use interface for concurrently printing HTML to PDF on iOS and macOS.
## 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## Examples
Print to a file url:
```swift
try await "Hello, World 1!
".print(to: URL(...))
```
Print to a directory with a file title.
```swift
let directory = URL(...)
let html = "Hello, World 1!
"
try await html.print(title: "file title", to: directory)
```Print a collection to a directory.
```swift
let directory = URL(...)
try await [
html,
html,
html,
....
]
.print(to: directory)
```## Performance
The package includes a test that prints 1000 HTML strings to PDFs in ~2.6 seconds (using ``UIPrintPageRenderer`` on iOS or Mac Catalyst) or ~12 seconds (using ``NSPrintOperation`` on MacOS).
```swift
@Test func collection() async throws {
[...]
let count = 1_000
try await [String].init(
repeating: "Hello, World 1!
",
count: count
)
.print(to: URL(...))
[...]
}
```### ``AsyncStream``
Optionally, you can invoke an overload that returns an ``AsyncStream`` that yields the URL of each printed PDF.
> [!NOTE]
> You need to include the ``AsyncStream`` type signature in the variable declaration, otherwise the return value will be Void.```swift
let directory = URL(...)
let urls: AsyncStream = try await [
html,
html,
html,
....
]
.print(to: directory)for await url in urls {
Swift.print(url)
}
```## Installation
To install the package, add the following line to your `Package.swift` file:
```swift
dependencies: [
.package(url: "https://github.com/coenttb/swift-html-to-pdf.git", from: "0.1.0")
]
```You can then make HtmlToPdf available to your Package's target by including HtmlToPdf in your target's dependencies as follows:
```swift
targets: [
.target(
name: "TheNameOfYourTarget",
dependencies: [
.product(name: "HtmlToPdf", package: "swift-html-to-pdf")
]
)
]
```