综合编程

How to Convert HTML to PDF with Swift

微信扫一扫,分享到朋友圈

How to Convert HTML to PDF with Swift
0

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:

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

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

:information_source: 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:

// 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

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

Copy

// 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 ourCatalog app.

Conclusion

In this article, we discussed how to generate a PDF using simple HTML and theDocument Processor feature. There are many other ways to create PDF documents by using features like theDocument Editor,Annotations, andForms. Please refer to our blog post that details how to Generate On-Device PDF Reports on iOS for more information.

阅读原文...

微信扫一扫,分享到朋友圈

How to Convert HTML to PDF with Swift
0
PSPDFKit Blog

Symmetric key decoding returns values ​​with space between ch...

上一篇

Samsung Galaxy S10 colors illustrated before launch

下一篇

评论已经被关闭。

插入图片

热门分类

往期推荐

How to Convert HTML to PDF with Swift

长按储存图像,分享给朋友