Some test text!

menu
search
Convert to PDFkeyboard_arrow_down

Convert documents to PDF

Saving annotations back to non-PDF formats is not supported. You can instead save the annotated PDFDoc to a PDF file.

PDFTron has a powerful conversion system which allows you to convert many file formats to PDF. The PTConvert class handles the conversion process and supports the following file types:

  • Office files: .docx, .pptx, .xlsx, .doc (with the office conversion Add-On module)
  • Images: .bmp, .jpg, .tif, .png, .gif
  • Image collections (zip archives with any type of image listed above)
  • Markdown files: .md (with the office conversion Add-On module)

linkOpen a non-PDF file

The simplest way to convert and view any file format that PDFTron supports is to use the PTDocumentViewController (or PTTabbedDocumentViewController) method openDocumentWithURL:. This method supports local files, files opened from remote cloud services and "raw" http URLs.

linkConvert a non-PDF file

Another way is to convert a file with the PTConvert class's +ToPDF:in_filename: method. The following code shows how to convert a non-PDF file using PDFTron's internal conversion:

// Create empty PDFDoc.
let doc = PTPDFDoc()

// Convert non-PDF file at "my_file_path"
PTConvert.toPdf(doc, in_filename: "my_file_path")

The result of the conversion will be contained in the provided PTPDFDoc, which can then be displayed.

linkConvert other non-PDF files

In addition to the file types supported by PDFTron's internal conversion, it is also possible to convert many other file types with an iOS-specific API. The PTConvert class's +convertOfficeToPDF:paperSize:completion: method can convert any file type supported by WKWebView to PDF. Some of the supported file types include:

  • iWork files: .pages, .key, .numbers
  • Binary office files: .doc, .ppt, .xls
  • Rich Text Format files: .rtf

The following code shows how to convert a file with the iOS-specific API:

PTConvert.convertOffice(toPDF: "my_file_path", paperSize: .zero) { (pathToPDF) in
    guard let pathToPDF = pathToPDF else {
        // Failed to convert file 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.
}

linkConvert HTML to PDF

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

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn More
close