[GIS] Creating bookmarked PDF from Mapbook (Python) export script

arcpybookmarksdata-driven-pagesexportpdf

I regularly use an export script to create multi-page PDFs from Data-Driven-Pages based Map-books. I am able to export and append each individual page to the PDF, and save the PDF in the single-page layout with the "Page Thumbnails" pane opened. I need to be able to (Pythonically) set the Bookmark text for each page within the PDF. I can do this manually from Adobe, but it would save a lot of time and headache if it could be automated.

Here is the script I use, at its most basic level without any bells or whistles. At the bottom of the script is where I set the page-layout (pdf_open_view="USE_THUMBS"). I can use "USE_BOOKMARKS" but this doesn't actually apply any bookmark text to the individual pages.

import arcpy, os
mxdPath = r"C:\temp\MyMapBook.mxd" 
tempMap = arcpy.mapping.MapDocument(mxdPath)
tempDDP = tempMap.dataDrivenPages

outDir      = r"C:\temp"   #PDF Folder where the individual pages and Combo are created
comboPDF    = r"\MultiPage_PDF.pdf"        #Name of Combo PDF
finalpdf_filename = os.path.join(outDir + comboPDF)
finalPdf = arcpy.mapping.PDFDocumentCreate(finalpdf_filename)

for pgIndex in range(1, tempDDP.pageCount + 1):
    tempDDP.currentPageID = pgIndex
    figName = str(tempDDP.pageRow.sdsFeatureName)
    figNumber = str(tempDDP.pageRow.Fig_Num_2015FSP)
    pageName = "Page-" + figNumber + "_" + figName + ".pdf"
    individualPagePDF = os.path.join(outDir + "\\" + pageName)

    #do something to the page before exporting. e.g. add inset map, add table etc.

    ###################################################################
    tempDDP.exportToPDF(individualPagePDF, "CURRENT") # export to signle page PDF
    finalPdf.appendPages(individualPagePDF) # append the page to the "combo" PDF
    print pageName + " added to final mapbook pdf"                    
    ###################################################################

    #undo whatever page modifications you just did before the export

##### Delete Objects
del tempMap, tempDDP

finalPdf.updateDocProperties(pdf_open_view="USE_THUMBS",pdf_layout="SINGLE_PAGE")
finalPdf.saveAndClose()

Best Answer

The PyPDF2 module works as needed in this situation. The script no longer uses arcpy.mapping.PDFDocumentCreate. The combo-PDF is now created using the PyPDF2 module. In the below script, it begins with the output = PyPDF2.PdfFileMerger().

Because multiple users need to access the module across the office network, installing the module on my machine would not have worked. Instead, the PyPDF2 module was placed in a network folder, and sys.path.insert was added to the script with the location to the network path.

Here is the working script that bookmarks each page within the combo-PDF.

import sys
sys.path.insert(0, r"H:\NetworkShared_PythonModules") # insert a path to the folder containing the PyPDF2 module
import PyPDF2, os, arcpy

mxdPath = r"C:\temp\MyMapBook.mxd" 
tempMap = arcpy.mapping.MapDocument(mxdPath)
tempDDP = tempMap.dataDrivenPages


outDir      = r"C:\temp"   #PDF Folder where the individual pages and Combo will be created
comboPDF    = r"MultiPage_PDF.pdf"        #Name of Combo PDF
finalpdf_filename = os.path.join(outDir, comboPDF)
output = PyPDF2.PdfFileMerger()

for pgIndex in range(1, tempDDP.pageCount + 1):
      tempDDP.currentPageID = pgIndex
      figName = str(tempDDP.pageRow.sdsFeatureName)
      figNumber = str(tempDDP.pageRow.Fig_Num_2015FSP)
      pageName = "Appendix Page-" + figNumber + "_" + figName + ".pdf"
      individualPagePDF = os.path.join(outDir, pageName)
      bookMarkText = "Figure A-" + figNumber + " --- " + figName

      #do something to the page before exporting.  e.g. add inset map, add table etc.

      ###################################################################
      tempDDP.exportToPDF(individualPagePDF, "CURRENT") # export to signle page PDF
      IndividualPageInput = PyPDF2.PdfFileReader(open(individualPagePDF, 'rb'))
      output.append(IndividualPageInput, bookMarkText)
      print pageName + " added to final mapbook pdf"                    
      ###################################################################

      #undo whatever page modifications you just did before the export

outputStream = file(finalpdf_filename, "wb")
output.setPageLayout("/SinglePage")#open the pdf to single page layout
output.setPageMode("/UseOutlines")#open the pdf with the bookMarks pane open
################################
output.write(outputStream)
outputStream.close()
del outputStream, output

##### Delete Objects
del tempMap, tempDDP
Related Question