Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for a corrupted PDF in iOS?

I have a PDF file which I downloaded from a server, sometimes the users uploads a corrupted PDF and I need to check if my application can open such a file or not.

So is there a built-in way I could use to check for the PDF corruption ? If not is there a free lightweight PDF framework that I could use to view or checking the corruption ?

Note: Currently I am opening PDF files on a UIWebView.

like image 413
Ahmed I. Khalil Avatar asked Jul 28 '13 13:07

Ahmed I. Khalil


1 Answers

I've found the solution using the CoreGraphics PDF drawing APIs. Thanks for this answer.

NSString *path = [[NSBundle mainBundle] pathForResource:@"A_Corrupted_PDF" ofType:@"pdf"];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];

CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(provider);

if (document == nil) {
    NSLog(@"The PDF is corrupted");
}
CGDataProviderRelease(provider);
CGPDFDocumentRelease(document);
like image 86
Ahmed I. Khalil Avatar answered Nov 01 '22 04:11

Ahmed I. Khalil