Convert documents to PDF on iOS

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

Apryse 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)

Open a non-PDF file

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

Convert 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 Apryse's internal conversion:

1// Create empty PDFDoc.
2let doc = PTPDFDoc()
3
4// Convert non-PDF file at "my_file_path"
5PTConvert.toPdf(doc, in_filename: "my_file_path")

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

Convert other non-PDF files

In addition to the file types supported by Apryse'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:

1PTConvert.convertOffice(toPDF: "my_file_path", paperSize: .zero) { (pathToPDF) in
2 guard let pathToPDF = pathToPDF else {
3 // Failed to convert file to PDF.
4 return
5 }
6
7 // Copy temporary PDF to persistent location.
8 let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, false)[0]
9
10 let urlToPDF = URL(fileURLWithPath: pathToPDF)
11 let destinationURL = URL(fileURLWithPath: documentDirectory).appendingPathComponent(urlToPDF.lastPathComponent)
12
13 do {
14 try FileManager.default.copyItem(at: urlToPDF, to: destinationURL)
15 } catch {
16 // Failed to copy item to persistent location.
17 }
18
19 // Do something with PDF output.
20}

Convert HTML to PDF

Please see this guide about Web/HTML to PDF.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales