Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Combine PDF's Python script
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Combine PDF's Python script

I was rummaging through some packages in /System/Library/Automator/ when I happened upon a tantalizing Python script with the following embedded usage info:

#! /usr/bin/python
# join
# Joing pages from a a collection of PDF files into a single PDF file.
# join [--output <file>] [--append] [--shuffle] [--preview] [-- verbose]"
# Parameter:
# --shuffle
# Take a page from each PDF input file in turn before taking another from each file.
# If this option is not specified then all of the pages from a PDF file are appended
# to the output PDF file before the next input PDF file is processed.
# --verbose
# Write information about the doings of this tool to stderr.

This is from /System/Library/Automator/Combine PDF Pages.action/ Contents/Resources/

So my question is: Is this some neat utility that I can use to mimic Automator's Combine PDF magic from anywhere I want? I tried to use it with terminal but couldn't get past the error:

dingbat:~ michaelhenley$ '/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/' '/Users/michaelhenley/ Desktop/Test C/PDF A.pdf' -o '/Users/michaelhenley/Desktop/Test C/PDF C.pdf' -a
Traceback (most recent call last):
File "/System/Library/Automator/Combine PDF Pages.action/Contents/ Resources/", line 117, in ?
File "/System/Library/Automator/Combine PDF Pages.action/Contents/ Resources/", line 111, in main
append(writeContext, args, verbose)
File "/System/Library/Automator/Combine PDF Pages.action/Contents/ Resources/", line 63, in append
AttributeError: 'NoneType' object has no attribute 'beginPage'

My feeling is that I'm mangling the syntax, or not understanding how the script wants its data. I suppose it's also possible that the script is so integral to the Automator action that it would require a significant re-write to work independently the way I want it too. Still intriguing though, suggestions?

I'm aware of other ways to combine PDF on the command line and elsewhere, but everything I've seen requires extra software be installed first. I've yet to find something that will work on the command line of a fresh system out of the box.

Here's the rest of the script, TIA:

import sys
import os
import getopt
import tempfile
import shutil
from CoreGraphics import *

def createPDFDocumentWithPath(path):
print path
return CGPDFDocumentCreateWithProvider (CGDataProviderCreateWithFilename(path))

def shufflePages(writeContext, inputFiles, verbose):

maxPages = 0

	print inputFiles

	# create PDFDocuments for all of the files.
	docs = map(createPDFDocumentWithPath, inputFiles)

print docs

# find the maximum number of pages.
for doc in docs:
if doc.getNumberOfPages() > maxPages:
maxPages = doc.getNumberOfPages()
print maxPages

# Shuffle the pages
for pageNum in xrange(1, maxPages + 1):
print "pageNum %d" % (pageNum)
for doc in docs:
page = doc.getPage(pageNum)
if page != None:
mediaBox = doc.getMediaBox(pageNum)
writeContext.drawPDFDocument(mediaBox, doc, pageNum)

def append(writeContext, inputFiles, verbose):
"""Write the pages from supplied PDF files into the provided context"""
for file in inputFiles:
inputFile = CGPDFDocumentCreateWithProvider (CGDataProviderCreateWithFilename(file))
for pageNum in xrange(1, inputFile.getNumberOfPages() + 1) :
mediaBox = inputFile.getMediaBox(pageNum)
writeContext.drawPDFDocument(mediaBox, inputFile, pageNum)
if verbose:
print "Copied page %d from %s" % (pageNum, file)

def main(argv):

	# The PDF context we will draw into to create a new PDF
	writeContext = None

# If True then generate more verbose information
verbose = False
source = None
shuffle = False

# Parse the command line options
options, args = getopt.getopt(argv, "o:sv", ["output=", "shuffle", "verbose"])

	except getopt.GetoptError:

	for option, arg in options:

		if option in ("-o", "--output") :
			if verbose:
				print "Setting %s as the destination." % (arg)
			writeFilename = arg
			pageRect = CGRectMake (0, 0, 612, 792)
			writeContext = CGPDFContextCreateWithFilename(arg, pageRect)

		elif option in ("-s", "--shuffle") :
			if verbose :
				print "Shuffle pages to the output file."
			shuffle = True

		elif option in ("-v", "--verbose") :
			print "Verbose mode enabled."
			verbose = True

		else :
			print "Unknown argument: %s" % (option)

	if shuffle:
		shufflePages(writeContext, args, verbose)
		append(writeContext, args, verbose)

def usage():
print "Usage: join [--output <file>] [--append] [--shuffle] [-- preview] [--verbose]"

if __name__ == "__main__":

Do not post admin requests to the list. They will be ignored.
Automator-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

Visit the Apple Store online or at retail locations.

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.