Re: Combine multiple PDFs
Re: Combine multiple PDFs
- Subject: Re: Combine multiple PDFs
- From: Bill Janssen <email@hidden>
- Date: Fri, 29 May 2009 12:13:45 PDT
- Comments: In-reply-to Fabrizio Costantini <email@hidden> message dated "Fri, 29 May 2009 11:32:24 -0700."
Fabrizio Costantini <email@hidden> wrote:
> If you have Leopard, you can use Preview.app. Open the first PDF, then
> drag the others into the sidebar.
>
> Fabrizio
Oh, very cool.
>
> Il giorno 29/mag/2009, alle ore 07.25, Nate Battles
> <email@hidden
> > ha scritto:
>
> > I would like to combine multiple PDFs into one PDF doc. Is there a
> > way to do this without third party add-ons or automator actions?
> > Most of the script snippets I’ve seen use a command line to call
> > “joinPDF “. I would prefer a solution that does not require this.
> >
> > Any help is appreciated.
Here's my Python script to do it:
#!/usr/bin/python
# combine.py -- combine multiple PDF files into one output file, and write that to stdout
from CoreGraphics import *
import sys, os, math, getopt, tempfile
def usage ():
sys.stderr.write('''
usage: python combine.py [-o outputfilename] INPUT-PDF [INPUT-PDF...]
''')
def main ():
outputfile = None
temporary = True
try:
opts,args = getopt.getopt (sys.argv[1:], 'o:', ['output='])
except getopt.GetoptError:
usage ()
sys.exit (1)
if len (args) < 2:
usage ()
sys.exit (1)
for o,a in opts:
if o in ('-o', '--output'):
outputfile = a
temporary = False
# figure pagesize for output file
mediabox = CGRect()
for pdffile in args:
if not os.path.exists(pdffile):
sys.stderr.write("Specified input file '%s' does not exist!\n")
sys.exit(1)
pdf = CGPDFDocumentCreateWithProvider (CGDataProviderCreateWithFilename (pdffile))
for index in range (1, pdf.getNumberOfPages () + 1):
mediabox = mediabox.union(pdf.getPage(index).getBoxRect(kCGPDFMediaBox))
if temporary:
outputfile = tempfile.mktemp()
c = CGPDFContextCreateWithFilename (outputfile, mediabox)
for pdffile in args:
pdf = CGPDFDocumentCreateWithProvider (CGDataProviderCreateWithFilename (pdffile))
for index in range (1, pdf.getNumberOfPages () + 1):
r = pdf.getPage(index).getBoxRect(kCGPDFMediaBox)
c.beginPage (r)
c.saveGState ()
c.drawPDFDocument (r, pdf, index)
c.restoreGState ()
c.endPage ()
c.finish ()
# OK, now write it out
if temporary:
output = os.fdopen(sys.stdout.fileno(), 'wb')
fp = open(outputfile, 'rb')
output.write(fp.read())
fp.close()
os.unlink(outputfile)
sys.exit(0)
if __name__ == '__main__':
main ()
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden