Generate PDFs from complex HTML

Processor provides a powerful API for generating PDF files directly from HTML strings. The conversion is fast, but it only supports simple HTML tags, such as <strong>, <a>, or <font>.

If you want to generate a PDF file from more complex HTML, you can use another technique. First save your HTML to a temporary local file, and then generate a PDF file from that URL. Nutrient utilizes the full power of WebKit when generating PDF files from URLs, allowing you to use CSS, embed images, or take advantage of a variety of tags and modern HTML constructs. This isn’t possible with simple HTML-to-PDF conversion.

Generating PDF files from URLs is a more time- and memory-consuming operation than simple HTML-to-PDF conversion. Make sure to perform the conversion and opt to do simple HTML-to-PDF conversion if possible.

Using these APIs requires the HTML-to-PDF Conversion component in your license.

let htmlURL = // URL to save the temporary HTML file to.
let pdfURL = // URL to save the converted PDF file to.
let html = """
<html>
<head>
<style type="text/css">
h1 {
color: red;
}
</style>
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
"""
do {
try html.data(using: .utf8)!.write(to: htmlURL)
} catch {
// Handle the error.
}
Processor.generatePDF(from: htmlURL, outputFileURL: pdfURL, options: nil) { outputURL, error in
if let outputURL = outputURL {
let document = Document(url: outputURL)
// Handle the document.
} else if let error = error {
// Handle the error.
}
}

For more details about how to generate PDF files from HTML strings, URLs, and attributed strings, take a look at Processor documentation and ConvertHTMLToPDFExample from our Catalog app.