3

I have been trying to edit an existing pdf file and saving it to device. I have tried saving the edited file to the same location as the unedited one. But the file could not be saved. Instead I got errors like

  • Attempting to save dictionary with key: . Dictionary keys must be of type string.
  • Could not create dictionary value for key: /DR. Invalid value.
  • Cannot save value for annotation key: /DR. Invalid type.
  • Attempting to save dictionary with key: . Dictionary keys must be of type string.

And here is my code:

pdfView.document?.write(to: pdfView.document!.documentURL!)

How to save edited changes in pdf files with PDFKit?

Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
Koushik Mudi
  • 183
  • 1
  • 12
  • Hello Kaushik, did you find a solution, in my case I am able to save the data in the new file but still getting the error message in debugging area as yours – Codicil Feb 07 '20 at 23:30
  • No, I haven't. But I think I figured out something. Instead overwriting the file, try deleting the old file and saving the new one at the location. Meanwhile our requirement changed and now I'm just saving the pdf file (downloading from the server) using the "share sheet" (UIActivityViewController). So, basically I do not know how to resolve the problem yet. – Koushik Mudi Feb 20 '20 at 09:26

3 Answers3

2

Here is the code to save the PDF:

pdfView.document?.write(toFile: "Path To Save the File.")

If you still can't save your changes, please recheck the path to save the pdf.

And in the same folder, I think, you can't create two files with the same name.

In this case that's because the old file is being replaced with a new one.

m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
Miah G.
  • 129
  • 1
  • 7
0

I figured out this, I had similar issue with my PDF form. The form was originally created in word and converted to PDF form. When I created the PDF form using Adobe Acrobat DC from scratch, it correctly annotating PDF in PDF view saving without above issue

Codicil
  • 196
  • 3
  • 15
0

Try this, may be solve your problem

if let data = pdfDocument?.dataRepresentation(){

    
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory = paths[0] // Get documents folder
    let folderPathUrl = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("samplePdf.pdf")
    if FileManager.default.fileExists(atPath: folderPathUrl.path){
        try? FileManager.default.removeItem(at: folderPathUrl)

    }
    try? data.write(to: folderPathUrl)

}
Jagveer Singh
  • 2,258
  • 19
  • 34