export to pdf failing?

1818
7
Jump to solution
03-10-2017 09:45 AM
forestknutsen1
MVP Regular Contributor

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!

0 Kudos
1 Solution

Accepted Solutions
forestknutsen1
MVP Regular Contributor

It is now working well. I changed the parameter for the ExportToPDF method for layers_attributes from "LAYERS_ONLY" to "NONE". I think this was eating up system resources. I also added del mxd to the loop.

Thanks so much for the help everyone! 

View solution in original post

0 Kudos
7 Replies
DanPatterson_Retired
MVP Esteemed Contributor

I am assuming that what you show is only a portion of the run since the elapsed time isn't jiving with the number of pages being printed.

Have you isolated that file and those pages for a test? to rule out content issues? or file issues?

forestknutsen1
MVP Regular Contributor

Yes, that is only the last lines of the tool text output. It had exported many mxd (maps and mapbooks) without problems before it failed. We did test the file and page at the fail point with a pdf export from arc. It worked fine.

0 Kudos
MichaelVolz
Esteemed Contributor

Your error shows the script ran for 3 hours + before failing on pg. 5 I guess.  Does it normally take this long to export pdfs from your mapbooks?

If it take 3+ hours to export 5 pages, how long does it take to print your 144 page mapbooks?

0 Kudos
forestknutsen1
MVP Regular Contributor

It had exported hundreds of pages from other mapbooks and maps at that point.  

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

ergo the time question from 2 of us... perhaps you are simply running out of memory or the folders (temporary file locations) are simply filling up.Try the whole process in inputs to limit possible memory issues and/or garbage volumes.  A good examination of that might be useful... and sometimes a good reboot/restart is necessary whether one wants to or not

0 Kudos
curtvprice
MVP Esteemed Contributor

Objects created in loops can often stack up and cause problems. One way you can see what's going on is to monitor resources with Windows Task Manager and see if the Python.exe module is grabbing more and more memory until it crashes. 

I have found that if you are doing a lot of iteration like this you want your code to explicitly clean up after each loop. For example, in your case "del mxd" at the end of each loop may help. The python "with" may be helpful here to help clean up your objects, but thats getting awfully fancy.

forestknutsen1
MVP Regular Contributor

It is now working well. I changed the parameter for the ExportToPDF method for layers_attributes from "LAYERS_ONLY" to "NONE". I think this was eating up system resources. I also added del mxd to the loop.

Thanks so much for the help everyone! 

0 Kudos