1

I am getting error while generating pdf on iOS 11.3. It's working fine with iOS 11.2.

CGDataConsumerCreateWithFilename: failed to open `/note.pdf' for writing: Operation not permitted.
deflateEnd: error -3: (null).

Pdf is generated well but when I try to access it to upload, it generates error. Code for the same is :

let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

let fileURL:URL = documentsURL.appendingPathComponent("note.pdf")

do {
   try FileManager.default.createDirectory(atPath: fileURL.path, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
   NSLog("Unable to create directory \(error.debugDescription)")
}

UIGraphicsBeginPDFContextToFile((fileURL.path as NSString).lastPathComponent as String?, CGRectZero, nil);

The last line generates error.

Any help would be highly appreciated.

rmaddy
  • 307,833
  • 40
  • 508
  • 550
Preetika
  • 456
  • 4
  • 15
  • Possible duplicate of [iOS get error by creating a pdf](https://stackoverflow.com/questions/20126818/ios-get-error-by-creating-a-pdf) – Pulkit Kumar Singh Apr 17 '18 at 06:25
  • https://stackoverflow.com/questions/8502181/where-to-store-a-created-pdf-file or https://stackoverflow.com/questions/20126818/ios-get-error-by-creating-a-pdf please search extensively before posting a question – Pulkit Kumar Singh Apr 17 '18 at 06:27
  • before posting the comment please read the question. i am facing issue on 11.3 . its working fine at 11.2 – Preetika Apr 17 '18 at 06:42
  • uninstall the application and reinstall the application ..try renaming your file – Pulkit Kumar Singh Apr 17 '18 at 07:42

1 Answers1

1

You are saving the file in root directory , You can not do this, try this

 let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

            let fileURL:URL = documentsURL.appendingPathComponent("note.pdf")

            do {
                try FileManager.default.createDirectory(atPath: fileURL.path, withIntermediateDirectories: true, attributes: nil)
            } catch let error as NSError {
                NSLog("Unable to create directory \(error.debugDescription)")
            }

            UIGraphicsBeginPDFContextToFile(fileURL.appendingPathComponent("note.pdf").path, CGRect.zero, nil);

Which will get and according to what you are doing you will create a folder note.pdf and then save the file note.pdf inside it

a.masri
  • 2,309
  • 1
  • 13
  • 30