Skip to main content

Creating PDF Documents

The SBSDKPDFRenderer class takes an image storage and renders the contained images into a PDF. For each image a page is generated. The generated pages have sizes that correspond to e.g. DIN A4, US Letter or Custom. As the images are embedded unscaled the resolution for each page depends on its image.

PDFs can be encrypted using SBSDKAESEncrypter or your custom written encryption classes. The PDF's data is encrypted in memory before it is written to disk. To decrypt the PDF you need to run proper decryption in your backend or clients.

NOTE: The Scanbot SDK does not lock the PDF with the password, but rather encrypts the actual file. This provides the best level of protection. To decrypt the PDF file you can use the key property of SBSDKAESEncrypter or generate the key yourself using salt, password and iterations.

See SBSDKPDFRendererPageSize for further information.

The operations' completion handlers are called in main thread.

Example code for creating a standard PDF from an image storage:

// Create an image storage to save the captured document images to
let imagesURL = SBSDKStorageLocation.applicationDocumentsFolderURL.appendingPathComponent("Images")
let imagesLocation = SBSDKStorageLocation.init(baseURL: imagesURL)
guard let imageStorage = SBSDKIndexedImageStorage(storageLocation: imagesLocation) else { return }

// Define the indices of the images in the image storage you want to render into a PDF, e.g. the first 3.
// To include all images you can simply pass nil for the indexSet. The indexSet is validated internally.
// You don't need to concern yourself with the validity of all the indices.
let indexSet = IndexSet(integersIn: 0...2)

// Specify the file URL where the PDF will be saved to. Nil makes no sense here.
guard let outputPDFURL = URL(string: "outputPDF") else { return }

// In case you want to encrypt your PDF file, create encrypter using a password and an encryption mode.
let encrypter = SBSDKAESEncrypter(password: "password_example#42", mode: .AES256)

// Create the OCR configuration for a searchable PDF (HOCR).
let ocrConfiguration = SBSDKOpticalCharacterRecognizerConfiguration.scanbotOCR()

// Create the default PDF rendering options.
let options = SBSDKPDFRendererOptions()

// Set the OCR Configuration.
options.ocrConfiguration = ocrConfiguration // Comment this line to not generate HOCR

// Create the PDF renderer and pass the PDF options to it.
let renderer = SBSDKPDFRenderer(options: options)

// Start the rendering operation and store the SBSDKProgress to watch the progress or cancel the operation.
let progress = renderer.renderImageStorage(imageStorage,
indexSet: indexSet,
encrypter: encrypter,
output: outputPDFURL) { finished, error in

if finished && error == nil {
// Now you can access the pdf file at outputPDFURL.
}
}

Want to scan longer than one minute?

Generate your free "no-strings-attached" Trial License and properly test the Scanbot SDK.

Get your free Trial License

What do you think of this documentation?