Some test text!

Loading...
Guides
Web/HTML to PDF

Convert HTML to PDF on iOS

Internet connection is not required when converting local HTML files. However, it is required for converting HTTP/HTTPS links.

The platform-specific conversion API also supports HTML to PDF conversion, either with the +convertOfficeToPDF:paperSize:completion: method for .html files or the +convertHTMLStringToPDF:baseURL:paperSize:completion: for raw HTML text.

The following code shows how to convert an HTML string to PDF:

PTConvert.convertHTMLString(toPDF: htmlString, baseURL: baseURL, paperSize: .zero) { (pathToPDF) in
    guard let pathToPDF = pathToPDF else {
        // Failed to convert HTML to PDF.
        return
    }
    
    // Copy temporary PDF to persistent location.
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, false)[0]
    
    let urlToPDF = URL(fileURLWithPath: pathToPDF)
    let destinationURL = URL(fileURLWithPath: documentDirectory).appendingPathComponent(urlToPDF.lastPathComponent)
    
    do {
        try FileManager.default.copyItem(at: urlToPDF, to: destinationURL)
    } catch {
        // Failed to copy item to persistent location.
    }

    // Do something with PDF output.
}

where baseURL is the URL to use as the page's base URL for resolving relative links in the document.

Get the answers you need: Support