I merge several single pdf pages in to a single file. The merge itself works good. All pages are in the right place and they look right.
The merge code looks like this:
CGContextRef writeContext = CGPDFContextCreateWithURL((CFURLRef)originalURL, NULL, NULL);
for(NSURL * pdfCacheURL in pdfURLs) {
singlePDFDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)pdfCacheURL);
pdfPage = CGPDFDocumentGetPage(singlePDFDocumentRef, 1);
mediaBox = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
CGContextBeginPage(writeContext, &mediaBox);
CGContextDrawPDFPage(writeContext, pdfPage);
CGContextEndPage(writeContext);
CGPDFPageRelease(pdfPage);
}
CGPDFContextClose(writeContext);
CFRelease(writeContext);
The strange thing that happens after the merge is that the final Document is extremely larger in file size than the file sizes of all single pages combined.
Here a part of my debug output
Processing Page: 1
File Size before merge:: 0.000000 mb
Single page length: 0.758951 mb
File Size after merge: 6.172294 mb
Processing Page: 2
File Size before merge: 6.172294 mb
Single page length: 0.262792 mb
File Size after merge: 6.722573 mb
Processing Page: 3
File Size before merge:: 6.722573 mb
Single page length: 0.215380 mb
File Size after merge: 8.150043 mb
Processing Page: 4
File Size before merge:: 8.150043 mb
Single page length: 0.346910 mb
File Size after merge: 10.788255 mb
As you can see, after 4 Pages the file size is over 10 megabytes but the combined file size is 1.58 megs. You can imagine what happens when 100 pages are merged.
The pdfs contain lots of images but I'm not sure if that can be responsible for such an increase in file size
This is a result of the way you are initializing
CGPDFContextCreateWithURL((CFURLRef)originalURL, NULL, NULL);
The second parameter is
mediaBox
, which according to Apple, isIf your original file is small than this size, it will get increased because it get drawn on a bigger canvas. Pass in a smaller size instead.
Here is the link to the reference: CGPDFContext Reference