The Shape of Everything
A website mostly about Mac stuff, written by August "Gus" Mueller
» Acorn
» Retrobatch
» Mastodon
» Instagram
» Github
» Maybe Pizza?
» Archives
» Feed
» Micro feed
February 18, 2004
(This post is from my old, old, super old site. My views have changed over the years, hopefully my writing has improved, and there is now more than a handful of folks reading my site. Enjoy.)

Wednesday, February 18th, 2004

A question came up on the mac-python mailing list, on how to merge some pdf's together. For future reference (for myself and others), here is one way to do it:

#!/usr/bin/python #usage: outfile a.pdf b.pdf ...

from CoreGraphics import * import sys, os, math, getopt, string

outfile = sys.argv[1] page_rect = CGRectMake (0, 0, 612, 792)

c = CGPDFContextCreateWithFilename (outfile, page_rect)

for file in sys.argv[2:]:      pdf = CGPDFDocumentCreateWithProvider(       CGDataProviderCreateWithFilename(file)      )      for p in range (1, pdf.getNumberOfPages () + 1):     r = pdf.getMediaBox (p)     c.beginPage(r)     c.drawPDFDocument(r, pdf, p)     c.endPage()    c.finish ()

And of course, I don't actually bother with error checking and such. That's um.. left as an exercise to the reader because I'm lazy.

-- posted 3:54 pm