Blog Post

How to Convert HTML to PDF with Swift

Illustration: How to Convert HTML to PDF with Swift

A widespread use case for many businesses is to give their users the ability to download data from their website as a PDF. Examples of this include invoices, concert tickets, and flight tickets.

PSPDFKit for iOS allows you to convert HTML to PDF in iOS using the PDF processing feature. In this post, we’ll look at how to convert simple HTML (not complex webpages) to PDF using Swift. For our example, we’ll use Sparksuite’s Simple HTML Invoice Template to convert the static HTML to PDF.

So, let’s get started!

Getting the HTML String

First, we create the URL that points to the HTML file:

// Uses a local HTML file.
let htmlFileURL = Bundle.main.url(forResource: "invoice", withExtension: "html", subdirectory:"Samples")!

Then, we use the file URL to create a string variable that contains the entire HTML payload. In Swift, it looks like this:

let htmlString = try! String(contentsOf: htmlFileURL, encoding: .utf8)

ℹ️ Note: If you’re looking to use a remote HTML file, you need to download it first using URLSession. Otherwise, you can use generatePDFFromURL:completionBlock:.

Using the PDF Processor

Now that we have the HTML payload as a string variable, we need to create a URL variable that specifies where the PDF processor should write the newly generated PDF file to disk:

// The URL where the PDF should be created.
// Note that it needs to point to a writable location.
let outputURL: URL = ...

Then, we need to create the options dictionary that we pass to the processor. In this case, we pass 1 for PSPDFProcessorNumberOfPagesKey to specify that the generated PDF should have a single page, and PSPDFProcessorDocumentTitleKey for the title of the new PDF:

// Create the options for the PDF processor.
let options = [PSPDFProcessorNumberOfPagesKey: 1, PSPDFProcessorDocumentTitleKey: "Generated PDF"] as [String: Any]

Now, we create a new PSPDFProcessor instance using the options dictionary from above:

// Create the processor instance.
let processor = PSPDFProcessor(options: options)

Finally, we call -convertHTMLString:outputFileURL: on the document processor and pass the HTML string and the output URL to create the new document, like this:

// Process/convert the HTML string to PDF.
processor.convertHTMLString(htmlString, outputFileURL: outputURL) { _ in
    // Generate the PDF document.
    let document = PSPDFDocument(url: outputURL)
    let pdfController = PSPDFViewController(document: document)
    // Present the PDF view controller.
}

That’s all!

You can download this example as a runnable sample project. To do so, please take a look at ConvertHTMLToPDFExample.swift from our Catalog app.

Conclusion

In this article, we discussed how to generate a PDF using simple HTML, and we talked about the processor feature in our iOS PDF library. We also offer several additional guides on generating a PDF in iOS:

You can also learn how to use templates to create PDF reports on a mobile device without an internet connection. To get started creating PDFs, download our free trial.

Free 60-Day Trial Try PSPDFKit in your app today.
Free Trial

Related Articles

Explore more
PRODUCTS  |  iOS • Releases

PSPDFKit 13.4 for iOS Introduces Revamped API Documentation and Improves Multiple Annotation Selection

DEVELOPMENT  |  visionOS • iOS

Apple’s Vision of Our Digital Future

PRODUCTS  |  iOS • visionOS • Mac Catalyst • Releases

PSPDFKit 13.3 for iOS Adds Long-Term Validation for Digital Signatures