Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Podofo for iOS; "Catalog object not found" after writing a PDF file

I have built Podofo 0.9.3 for iOS along with all the other needed libraries to support amrv7, arm64 and simulator. My project runs fine, but my problem is loading a document for the second time. I always get the error "Catalog object not found" in Podofo. If I open the document using Preview app on mac, and save it, Podofo can open it again.

Here's the code I'm using to open the document and save it:

self.doc = new PoDoFo::PdfMemDocument([path UTF8String]);

NSString *tmpPath = [self createCopyForFile:self.pdfPath];

self.doc->Write([tmpPath UTF8String]);

NSData *myFile = [NSData dataWithContentsOfFile:tmpPath];
[myFile writeToFile:tmpPath atomically:YES];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;

if ([fileManager fileExistsAtPath:self.pdfPath] == YES) {
     [fileManager removeItemAtPath:self.pdfPath error:&error];
}
[fileManager copyItemAtPath:tmpPath toPath:self.pdfPath error:&error];

The error is here:

void PdfMemDocument::InitFromParser( PdfParser* pParser )
{
...
PdfObject* pCatalog = pTrailer->GetIndirectKey( "Root" );
if( !pCatalog )
{
      PODOFO_RAISE_ERROR_INFO( ePdfError_NoObject, "Catalog object not found!" );
...
}

Have you guys built Podofo for iOS lately? Any idea why is this happening?

like image 909
Basel Avatar asked Apr 13 '15 06:04

Basel


1 Answers

I faced with similar problem. The problem was with path to NSCachesDirectory. It changes with every run of application in simulator. To fix this problem I saved path to my file without path to caches directory:

- (NSString *)filePathWithoutCacheDirectoryComponent:(NSString *)fullPath {
    NSString *cacheDirectoryPath =
        NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    return [fullPath stringByReplacingOccurrencesOfString:cacheDirectoryPath withString:@""];
}

To open my file next time I added caches directory path to its saved path:

NSString *cacheDirectoryPath =
        NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
fullPath = [cacheDirectoryPath stringByAppendingPathComponent:savedPathToFile];

So if you save your file to NSCachesDirectory or NSDocumentDirectory try this.

like image 167
Vlad Avatar answered Nov 14 '22 19:11

Vlad