PDFKit deletes certificates on document load

Why does PDFKit delete signature widgets that have already been digitally signed?

This should not happen.

Is there an undocumented flag that needs to be set so that PDFKit doesn't remove them when loading or saving the PDF?

It's difficult to tell if it is happening at

PDFDocument(url: fileURL)

or

document.write(to: outputURL)

If a document is signed and still allows annotations, form filling, comments, etc. we should be able to load the PDF into a PDFDocument and save it without losing the certs.

Instead the certs are gone and only the signature annotation widgets are present.

Here is a simple example of loading and then saving the PDF with out any changes and it shows that the data is actually being changed...

...

import UIKit
import PDFKit

class ViewController: UIViewController {
    
    var pdfView: PDFView!
    
    @IBOutlet weak var myButton: UIButton!
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        pdfView = PDFView(frame: self.view.bounds)
        pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.view.addSubview(pdfView)
        self.view.bringSubviewToFront(myButton)
        
        // Load and compare the PDF data
        if let originalData = loadPDF() {
            if let loadedData = getRawDataFromLoadedPDF() {
                let isDataEqual = comparePDFData(originalData, loadedData)
                print("Is original data equal to loaded data? \(isDataEqual)")
            }
        }

    }
    
    @IBAction func onSave(_ sender: Any) {
        if let savedData = savePDF() {
            if let originalData = loadPDF() {
                let isDataEqual = comparePDFData(originalData, savedData)
                print("Is original data equal to saved data? \(isDataEqual)")
            }
        }
    }
    
    func loadPDF() -> Data? {
        
        guard let fileURL = Bundle.main.url(forResource: "document", withExtension: "pdf") else {
            print("Error: document.pdf not found in bundle.")
            return nil
        }
        
        do {
            let originalData = try Data(contentsOf: fileURL)
            if let document = PDFDocument(url: fileURL) {
                pdfView.document = document
                print("PDF loaded successfully.")
                return originalData
            } else {
                print("Error: Unable to load PDF document.")
                return nil
            }
        } catch {
            print("Error reading PDF data: \(error)")
            return nil
        }
    }

    func getRawDataFromLoadedPDF() -> Data? {
        guard let document = pdfView.document else {
            print("Error: No document is currently loaded in pdfView.")
            return nil
        }
        
        if let data = document.dataRepresentation() {
            return data
        } else {
            print("Error: Unable to get raw data from loaded PDF document.")
            return nil
        }
    }

    func comparePDFData(_ data1: Data, _ data2: Data) -> Bool {
        return data1 == data2
    }

    func savePDF() -> Data? {
        guard let document = pdfView.document else {
            print("Error: No document is currently loaded in pdfView.")
            return nil
        }
        
        let fileManager = FileManager.default
        let urls = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
        guard let documentsURL = urls.first else {
            print("Error: Could not find the documents directory.")
            return nil
        }
        
        let outputURL = documentsURL.appendingPathComponent("document_out.pdf")
        
        if document.write(to: outputURL) {
            print("PDF saved successfully to \(outputURL.path)")
            do {
                let savedData = try Data(contentsOf: outputURL)
                return savedData
            } catch {
                print("Error reading saved PDF data: \(error)")
                return nil
            }
        } else {
            print("Error: Unable to save PDF document.")
            return nil
        }
    }

}

Our engineering teams need to investigate this issue, as resolution may involve changes to Apple's software. I'd greatly appreciate it if you could open a bug report, include an Xcode project that demonstrates the problem and some directions for reproducing the problem, and post the FB number here once you do.

Bug Reporting: How and Why? has tips on creating your bug report.

PDFKit deletes certificates on document load
 
 
Q