Combining several PDFs into a single one

3324
4
Jump to solution
05-08-2014 09:36 AM
ThomasKendall
New Contributor
I have created some pdfs through a stand alone script, but would like to be able to create one single pdf file and not 100+ single page pdf files.

My naming convention has a different name for Left and Right pages.

Left: North_x_FAC
Right: North _x_SLD

Is there a way to get python to combine the files so that the single pdf file would have to correct ordering. North_1_FAC would come before North_1_SLD, and so on down to like 50+ pages of both left and right pages.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
At 10.1, you can actually combine pdf's using the arcpy mapping module.  I use this script all the time to combine pdfs:

import os, arcpy, glob  def combinePDFs(out_pdf, pdf_path_or_list):     '''     uses arcpy mapping  module to combine pdf documents      out_pdf -- output pdf document (.pdf)     pdf_path_or_list -- list of pdf documents or folder                         path containing pdf documents.     '''     # Create new PDF document     out_path, pdf_name = os.path.split(out_pdf)     pdfDoc = arcpy.mapping.PDFDocumentCreate(out_pdf)      # set workspace to get pdfs     if isinstance(pdf_path_or_list, list):         for pdf in pdf_path_or_list:             pdfDoc.appendPages(pdf)             print 'Added "%s" to "%s"' %(pdf, os.path.basename(out_pdf))     elif isinstance(pdf_path_or_list, str):         if os.path.exists(pdf_path_or_list):             # if path, use glob to find all pdf's in folder             for pdf in sorted(glob.glob(os.path.join(pdf_path_or_list, '*.pdf'))):                 pdfDoc.appendPages(os.path.join(pdf_path_or_list, pdf))                 print 'Added "%s" to "%s"' %(pdf, os.path.basename(out_pdf))        # Save and close pdf document        pdfDoc.saveAndClose()     del pdfDoc     print 'Created: %s' %out_pdf     return out_pdf  if __name__ == '__main__':      # test function with path     out_pdf = r'C:\Users\calebma\Desktop\test.pdf'     path = r'C:\Users\calebma\Desktop\pdfTest'     combinePDFs(out_pdf, path)      # test functino with list     pdf2 = r'C:\Users\calebma\Desktop\test2.pdf'     pdfs = [r'C:\Users\calebma\Desktop\pdfTest\Mailing_Labels5160.pdf',             r'C:\Users\calebma\Desktop\pdfTest\Mailing_Taxpayer.pdf',             r'C:\Users\calebma\Desktop\pdfTest\stfr.pdf']     combinePDFs(pdf2, pdfs)  

View solution in original post

0 Kudos
4 Replies
MattEiben
Occasional Contributor
I don't think arcpy has any sort of built in functionality for this.  My suggestion would be to post this question on Stack Overflow, since this is more of a broad Python question than specific to ArcMap.

A couple possible solutions I can think of off the top of my head is using a cloud pdf combiner.  You could use the Requests module to pass your pdfs to the cloud service via a post request.  Though I'm not sure how well this would work with 100+ pdfs. 

Another solution would be a custom python module that is built to do exactly this sort of thing.  I'm not sure it one exists, but a post to a more general forum may bring up some answers.

Good hunting!

UPDATE:

A quick Google search turned up a Python module called pyPDF, which has the functionality you're looking for.

Check here for an example of it's usage.
0 Kudos
GeorgeHaskett
Occasional Contributor III
I don't know for sure about after the fact, however if you created a new ranking field and your data was such that you could cycle through the field for each new pdf, then you can combine them all in the end.  This method, at least how I am using it, would require the ranking field prior to the creation of the pdfs.  The new pdfs would have a page count associated with them and can then be compiled via python.

Can you easily rebuild these pdfs or at least rename them in the order you want by adding a page count at the end?

You might also be able to build a custom dictionary and use that to compile the pdf based upon the order you designated.  I haven't tried this method as of yet.
0 Kudos
by Anonymous User
Not applicable
At 10.1, you can actually combine pdf's using the arcpy mapping module.  I use this script all the time to combine pdfs:

import os, arcpy, glob  def combinePDFs(out_pdf, pdf_path_or_list):     '''     uses arcpy mapping  module to combine pdf documents      out_pdf -- output pdf document (.pdf)     pdf_path_or_list -- list of pdf documents or folder                         path containing pdf documents.     '''     # Create new PDF document     out_path, pdf_name = os.path.split(out_pdf)     pdfDoc = arcpy.mapping.PDFDocumentCreate(out_pdf)      # set workspace to get pdfs     if isinstance(pdf_path_or_list, list):         for pdf in pdf_path_or_list:             pdfDoc.appendPages(pdf)             print 'Added "%s" to "%s"' %(pdf, os.path.basename(out_pdf))     elif isinstance(pdf_path_or_list, str):         if os.path.exists(pdf_path_or_list):             # if path, use glob to find all pdf's in folder             for pdf in sorted(glob.glob(os.path.join(pdf_path_or_list, '*.pdf'))):                 pdfDoc.appendPages(os.path.join(pdf_path_or_list, pdf))                 print 'Added "%s" to "%s"' %(pdf, os.path.basename(out_pdf))        # Save and close pdf document        pdfDoc.saveAndClose()     del pdfDoc     print 'Created: %s' %out_pdf     return out_pdf  if __name__ == '__main__':      # test function with path     out_pdf = r'C:\Users\calebma\Desktop\test.pdf'     path = r'C:\Users\calebma\Desktop\pdfTest'     combinePDFs(out_pdf, path)      # test functino with list     pdf2 = r'C:\Users\calebma\Desktop\test2.pdf'     pdfs = [r'C:\Users\calebma\Desktop\pdfTest\Mailing_Labels5160.pdf',             r'C:\Users\calebma\Desktop\pdfTest\Mailing_Taxpayer.pdf',             r'C:\Users\calebma\Desktop\pdfTest\stfr.pdf']     combinePDFs(pdf2, pdfs)  
0 Kudos
BrunoDeus
New Contributor III

Hi,

I would like use it, but I can`t just copy e value. I tried replace 5 spaces for a broke line; but its doesn`t worked yet. I appreciate a help here. I'm using ArcGIS Desktop 10.4

Best Regards

 

import os, arcpy, glob  
def combinePDFs(out_pdf, pdf_path_or_list):     
'''     
uses arcpy mapping  module to combine pdf documents     
out_pdf -- output pdf document (.pdf)
pdf_path_or_list -- list of pdf documents or folder     
                    path containing pdf documents.     
'''     

# Create new PDF document     
out_path, pdf_name = os.path.split(out_pdf)     
pdfDoc = arcpy.mapping.PDFDocumentCreate(out_pdf)      

# set workspace to get pdfs     
if isinstance(pdf_path_or_list, list):
    for pdf in pdf_path_or_list:
        pdfDoc.appendPages(pdf)
        print 'Added "%s" to "%s"' %(pdf, os.path.basename(out_pdf))
elif isinstance(pdf_path_or_list, str):
    if os.path.exists(pdf_path_or_list):
        # if path, use glob to find all pdf's in folder
        for pdf in sorted(glob.glob(os.path.join(pdf_path_or_list, '*.pdf'))):
            pdfDoc.appendPages(os.path.join(pdf_path_or_list, pdf))
            print 'Added "%s" to "%s"' %(pdf, os.path.basename(out_pdf))   

# Save and close pdf document
    pdfDoc.saveAndClose()
del pdfDoc
print 'Created: %s' %out_pdf
return out_pdf  if __name__ == '__main__':

# test function with path
out_pdf = r'C:\Users\calebma\Desktop\test.pdf'
path = r'C:\Users\calebma\Desktop\pdfTest'
combinePDFs(out_pdf, path) 

# test functino with list
pdf2 = r'C:\Users\calebma\Desktop\test2.pdf'
pdfs = [r'C:\Users\calebma\Desktop\pdfTest\Mailing_Labels5160.pdf',
        r'C:\Users\calebma\Desktop\pdfTest\Mailing_Taxpayer.pdf',
        r'C:\Users\calebma\Desktop\pdfTest\stfr.pdf']
combinePDFs(pdf2, pdfs)
0 Kudos