Denis Denis - 1 month ago 24
iOS Question

iOS get error by creating a pdf

i want to create a pdf file, but i get 2 errors:

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


and this one

<Error>: CGPDFContextCreate: failed to create PDF context delegate.


I compute the path in the controller class

- (void)viewDidLoad
{
documentName = @"test.pdf";
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:documentName];
url = [NSURL fileURLWithPath:pdfFileName];

PDFRenderer *pdf = [[PDFRenderer alloc]initWithFileName:documentName];
[pdf renderPDF];


[super viewDidLoad];
}


and in the render class I call the

UIGraphicsBeginPDFContextToFile(documentName, CGRectZero, nil);


function and I get the errors.

How I fix that error?

Answer

You are writing to a relative path, try to change it into

PDFRenderer *pdf = [[PDFRenderer alloc]initWithFileName:pdfFileName];

i.e. the absolute path in the documents directory.

If you look at the error message, you will see that the file is being attempted to be written in the root directory.

Comments