#!/usr/bin/python from CoreGraphics import * import sys import os # # This rather boneheaded little script will take any number of PDF # files, run thru 'em to find the maximum width and height, then # runs through 'em all over again, imaging each page into a new PDF # document. # # It is a totally naive little hack that was the minimum amount of # code I could spew forth to concatenate a series of PDFs that # happened to contain converted JPGs of different sizes into # a single resulting document where every page is the same size. # Thus, a single scaling factor could be used to make 'em full # page. # # Feel free to send me (bbum@mac.com) fixes and patches and stuff. # # MIT license, if anyone cares. if len (sys.argv) <= 2: print "concatpdf *.pdf out.pdf" sys.exit(1) inFiles = sys.argv[1:-1] outFile = sys.argv[-1] print "Processing %d input files" % len(inFiles) print "Output %s" % outFile # determine max width/height width = 0 height = 0 for aFile in inFiles: pdf = CGPDFDocumentCreateWithProvider(CGDataProviderCreateWithFilename(aFile)) w = pdf.getTrimBox(1).getWidth() h = pdf.getTrimBox(1).getHeight() if w > width: width = w if h > height: height = h print "Size: %d x %d" % (width, height) # create output document pageRect = CGRectMake(0, 0, width, height) outContext = CGPDFContextCreateWithFilename(outFile, pageRect) # image every page totalNumberOfPages = 0 for aFile in inFiles: print "Processing %s" % aFile pdf = CGPDFDocumentCreateWithProvider(CGDataProviderCreateWithFilename(aFile)) numberOfPages = pdf.getNumberOfPages() totalNumberOfPages = totalNumberOfPages + numberOfPages for p in range(1, numberOfPages + 1): outContext.beginPage(pageRect) outContext.saveGState() outContext.drawPDFDocument(pageRect, pdf, p) outContext.restoreGState() outContext.endPage() # done outContext.finish() print "Wrote %d pages" % totalNumberOfPages