Giter VIP home page Giter VIP logo

swift-html-to-pdf's Introduction

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:

try await "<html><body><h1>Hello, World 1!</h1></body></html>".print(to: URL(...))

Print to a directory with a file title.

let directory = URL(...)
let html = "<html><body><h1>Hello, World 1!</h1></body></html>"
try await html.print(title: "file title", to: directory)

Print a collection to a directory.

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).

@Test func collection() async throws {
    [...]
    let count = 1_000
    try await [String].init(
        repeating: "<html><body><h1>Hello, World 1!</h1></body></html>",
        count: count
    )
    .print(to: URL(...))
    [...]
}

AsyncStream<URL>

Optionally, you can invoke an overload that returns an AsyncStream<URL> 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.

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:

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:

targets: [
    .target(
        name: "TheNameOfYourTarget",
        dependencies: [
            .product(name: "HtmlToPdf", package: "swift-html-to-pdf")
        ]
    )
]

swift-html-to-pdf's People

Contributors

coenttb avatar finestructure avatar

Stargazers

Tyo avatar James Pamplona avatar adamz avatar  avatar  avatar Jeff L. avatar Dante Broggi avatar Morten Bek Ditlevsen avatar  avatar brian flagg avatar Tim Kersey avatar

Watchers

brian flagg avatar  avatar  avatar

Forkers

finestructure

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.