Save as PDFs on iOS

It’s not uncommon for customers to want a setup where a document is read-only but users can still edit it and then use the Save As functionality to save the modified document to a new location. PSPDFKit supports saving documents with the Save As functionality.

For Save As to work, the most important thing to do is disable autosaving. This is a one-line change to the configuration to disable the isAutosaveEnabled property when creating the PDF view controller:

let pdfViewController = PDFViewController(document: document) {
    $0.isAutosaveEnabled = false
}

The user can then save a copy of the document using the standard share button that’s included in PSPDFKit’s UI by default.

You can also choose to customize the sharing settings so that users aren’t asked about things like whether to embed or flatten annotations after tapping the share button:

// Allow only a single option for each setting so the first screen of the share UI is skipped.
let sharingConfiguration = DocumentSharingConfiguration {
    $0.fileFormatOptions = [.PDF]
    $0.pageSelectionOptions = [.all]
    $0.annotationOptions = [.embed]
}

let pdfViewController = PDFViewController(document: document) {
    $0.isAutosaveEnabled = false
    $0.sharingConfigurations = [sharingConfiguration]
}

If you want to change the icon on the button from the standard share icon to a different icon or use the text “Save As,” this can be done by creating a new bar button item:

let pdfViewController = PDFViewController(document: document) {
    $0.isAutosaveEnabled = false
}

pdfViewController.navigationItem.rightBarButtonItems = [
    UIBarButtonItem(title: "Save As", style: .plain, target: self, action: #selector(saveAs)),
	pdfViewController.annotationButtonItem
]

When the button is tapped, manually create a PDFDocumentSharingViewController:

@objc func saveAs(_ sender: UIBarButtonItem) {
    guard let document = pdfViewController.document else {
        return
    }

    let sharingController = PDFDocumentSharingViewController(documents: [document], sharingConfigurations: [
        DocumentSharingConfiguration {
            $0.fileFormatOptions = [.PDF]
            $0.pageSelectionOptions = [.all]
            $0.annotationOptions = [.embed]
        }
    ])

    // Setting the sharing flow's delegate to `PDFViewController`
    // allows it to preserve its default behavior and prevents it
    // from saving the original document in place, which would
    // otherwise be done as an optimization in common cases.
    sharingController.delegate = pdfViewController

    sharingController.present(from: pdfViewController, sender: sender)
}