0

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

MatzeLoCal
  • 392
  • 3
  • 14

1 Answers1

0

This is a result of the way you are initializing CGPDFContextCreateWithURL((CFURLRef)originalURL, NULL, NULL);

The second parameter is mediaBox, which according to Apple, is

A rectangle that specifies the bounds of the PDF. The origin of the rectangle should typically be (0,0). The CGPDFContextCreateWithURL function uses this rectangle as the default page media bounding box. If you pass NULL, CGPDFContextCreateWithURL uses a default page size of 8.5 by 11 inches (612 by 792 points).

If 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

Anton
  • 559
  • 2
  • 15
  • Thanks for the answer but unfortunately that's not the problem. The bounding box is equal on both documents. After posting this question, I found this http://stackoverflow.com/questions/3099312/why-does-combining-pdf-pages-with-cgcontextdrawpdfpage-create-very-large-output where @qwzybug points out that CGPDFContext would transform jpgs to raw bitmaps. I did not found a confirmation of this yet but it would be an explanation. But then not sure how I can solve this. – MatzeLoCal Jun 08 '16 at 22:00
  • Oh ok, so I am thinking it might be a resolution problem. Maybe try to find a way to reduce the resolution – Anton Jun 08 '16 at 22:06