Generate PDFs from images on iOS
Images are widely used to display content and graphics, but sometimes a PDF format is necessary. Use the Processor API to convert a UIImage into a PDF file with minimal code.
To convert an image to PDF, your license must include the Document Editor component, and to annotate images, your license must include the Image Document component.
Contact our Sales team for assistance.
Converting a single image to a PDF
Follow the steps below to convert an image to a PDF.
- Create a
PDFNewPageConfigurationobject to define the page settings as follows:- Specify the image to use.
- Set the compression quality.
- Define the page size.
- Create a
Processor.Configurationand add thePDFNewPageConfigurationinstance as the configuration for the first page. - Use
Processorto generate the PDF and write it to the specifiedoutputFilePath.
The following code sample demonstrates how to convert a single image to a PDF:
let image: UIImage = ...let outputFileURL: URL = ... // Writable file URL.let pageTemplate = PageTemplate(pageType: .emptyPage, identifier: nil)let newPageConfiguration = PDFNewPageConfiguration(pageTemplate: pageTemplate) { builder in builder.item = ProcessorItem(image: image, jpegCompressionQuality: 0.7, builderBlock: nil) builder.pageSize = image.size}
let configuration = Processor.Configuration()configuration.addNewPage(at: 0, configuration: newPageConfiguration)
do { try Processor(configuration: configuration, securityOptions: nil).write(toFileURL: outputFileURL)} catch { print("Could not create PDF file: \(error)")}Converting multiple images to a PDF
Follow the steps below to convert multiple images to a PDF.
- Create multiple
PDFNewPageConfigurationobjects, each containing a different image. - Add each
newPageConfigurationtoProcessor.Configuration, as demonstrated in the code sample below:
configuration.addNewPage(at: 0, configuration: newPageConfiguration0)configuration.addNewPage(at: 1, configuration: newPageConfiguration1)configuration.addNewPage(at: 2, configuration: newPageConfiguration2)...