Can't read PDF document using CGPDFDocumentCreateWithURL

133 Views Asked by At

I'm, unsuccessfully, trying to load a PDF on iOS, when debugging the code the document shows "0x0" as value.

NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
NSString *pdfPath = [appFolderPath stringByAppendingString:@"/Data/Raw/test.pdf"];
    
CFStringRef cfsPath = (__bridge CFStringRef)pdfPath;

CFURLRef url = CFURLCreateWithFileSystemPath(NULL, cfsPath, kCFURLPOSIXPathStyle, 0);
    
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(url);

What could be wrong with this snippet? I don't have much experience with iOS native development and Objective-C.

1

There are 1 best solutions below

0
On

First of all check if the file exists at the given location in the application bundle.

It's highly recommended to use the URL related API of NSBundle

NSURL *appFolderURL = [[NSBundle mainBundle] resourceURL];
NSURL *pdfURL = [appFolderURL URLByAppendingPathComponent:@"Data/Raw/test.pdf"];

And use the more convenient PDFDocument class of PDFKit rather than the CoreFoundation API

@import PDFKit;

PDFDocument *document = [[PDFDocument alloc] initWithURL:pdfURL];