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 into PDF using the Document Processing feature. In this post, we will look at how to convert simple HTML (not complex webpages) to PDF using Swift. For our example, we’ll be using Sparksuite’s Simple HTML invoice template to convert the static HTML to PDF.

So let’s get started!

Get the HTML String

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

1
2
// 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 would look like this:

1
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:.

Use the Document 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:

1
2
3
// 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:

Copy
1
2
// 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:

1
2
// 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:

Copy
1
2
3
4
5
6
7
// 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 the Document Processor feature. There are many other ways to create PDF documents by using features like the Document Editor, Annotations, and Forms. Please refer to our blog post that details how to Generate On-Device PDF Reports on iOS for more information.

PSPDFKit for iOS

Download the free 60-day trial and add it to your app today.

iOS