iOS get error by creating a pdf

匆匆过客 提交于 2020-01-01 21:09:22

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/20126818/ios-get-error-by-creating-a-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!