Save PDFs to Local Storage in iOS

PSPDFKit automatically saves a modified file-based document to your device’s local storage. You can also customize the autosaving behavior or manually save the changes. Please note that files are saved locally on your device, and no server is required.

The example below shows how to save a document on the main thread programmatically:

let document = ...

// Manually save the document.
try? document.save()

Saving Data-Backed Documents to Local Storage

If you’re working with a data-backed document and wish to save it as a PDF file on your device’s local storage, you can use the Processor API, like so:

let document = ...

// Manually save the document.
try? document.save()

// Generate a new document with embedded annotations.
guard let processorConfiguration = Processor.Configuration(document: document) else {
    print("Could not create a processor configuration. The document might be locked or invalid.")
    return
}

processorConfiguration.modifyAnnotations(ofTypes: .all, change: .embed)

let docsFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let outputURL = docsFolder.appendingPathComponent("local.pdf")

let processor = Processor(configuration: processorConfiguration, securityOptions: nil)
do {
    // Write the modified document. This can be used to initialize
    // and present a new PSPDFKit document.
    try processor.write(toFileURL: outputURL)
} catch {
    print(error)
}

For more details about using the Processor API to write a document on local storage, please refer to XFDFAnnotationProviderEmbeddedExample.swift in the PSPDFKit Catalog app.