AnsweredAssumed Answered

export to pdf failing?

Question asked by fallingdog on Mar 10, 2017
Latest reply on Mar 14, 2017 by fallingdog

We have a script tool that exports mxd maps and mapbooks to pdf. Some of the mapbooks are large 144 pages. It is sometimes failing and sometimes working fine. I am not sure what the issue is...

 

import os
import arcpy
import tempfile
import shutil

def remove_pdf(pdf_path):
    if os.path.exists(pdf_path):
        os.remove(pdf_path)

parameter = arcpy.GetParameterAsText(0)
dir_list = parameter.split(";")

for in_path in dir_list:
    in_path =  in_path.replace("""'""", "")
    input_files = os.listdir(in_path)
    #for dirpath, subdirs, files in os.walk(in_path):
    for f in input_files:
        temp_dir = tempfile.mkdtemp()
        if f.endswith("mxd"):
            mxd_path = os.path.join(in_path, f)
            mxd = arcpy.mapping.MapDocument(mxd_path)
            if mxd.isDDPEnabled:
                map_book_path = mxd_path[:-3] + "pdf"
                remove_pdf(map_book_path)
                map_book_pdf = arcpy.mapping.PDFDocumentCreate(map_book_path)
                arcpy.AddMessage("\nExporting mapbook: " + os.path.join(in_path, f))
                for page in range(1, mxd.dataDrivenPages.pageCount + 1):
                    arcpy.AddMessage("\tExporting mapbook page: " + str(page))
                    pdf = os.path.join(temp_dir, "map" + str(page) + ".pdf")
                    mxd.dataDrivenPages.currentPageID = page
                    arcpy.mapping.ExportToPDF(mxd, pdf)
                    map_book_pdf.appendPages(pdf)
                map_book_pdf.saveAndClose()
                arcpy.AddMessage("Export Complete")
            else:
                arcpy.AddMessage("\nExporting map: " + os.path.join(in_path, f))
                map_pdf_path = mxd_path[:-3] + "pdf"
                remove_pdf(map_pdf_path)
                arcpy.mapping.ExportToPDF(mxd, map_pdf_path)
                arcpy.AddMessage("Export Complete")
        shutil.rmtree(temp_dir)

arcpy.AddMessage("\nAll Exports Complete")

Error:

Exporting mapbook: X:\xxxx\mapbook.mxd
     Exporting mapbook page: 1
     Exporting mapbook page: 2
     Exporting mapbook page: 3
     Exporting mapbook page: 4
     Exporting mapbook page: 5
Failed script ExportToPDF...
 
Traceback (most recent call last):
  File "X:\Tools\Export_PDF\Dev\pdfExport.py", line 31, in <module>
    arcpy.mapping.ExportToPDF(mxd, pdf)
  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\utils.py", line 182, in fn_
    return fn(*args, **kw)
  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 1156, in ExportToPDF
    layout.exportToPDF(*args)
AttributeError: PageLayoutObject: Error in executing ExportToPDF
 
Failed to execute (ExportToPDF).
Failed at Thu Mar 09 19:20:29 2017 (Elapsed Time: 3 hours 12 minutes 54 seconds)

 

Thanks for the help!

Outcomes