Is there a way to ExportToPDF to a single PDF

2737
4
Jump to solution
05-01-2016 11:13 PM
BenVan_Kesteren1
Occasional Contributor III

Hi All,

I am currently using this code to export my data driven pages to PDF, but the export seems to only export individual pages, I would like a singular output PDF with several pages instead. Does anyone know if this is possible with this tool, or do I need to use an external module to achieve this one??

mxd = arcpy.mapping.MapDocument("CURRENT")
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum
  arcpy.mapping.ExportToPDF(mxd, r"C:\temp\MAP_" + str(pageNum) + ".pdf")
del mxd

Here is the help file for the ExportToPDF tool

Cheers

0 Kudos
1 Solution

Accepted Solutions
FreddieGibson
Occasional Contributor III

If you leverage the ExportToPDF method of the mapping class you'll have to export each page one at a time and then append them into a single document. If you're using data driven pages and you want to export all of the pages to a single pdf in one go use the exportToPDF method of the data driven pages class instead of the ExportToPDF method of the mapping class.

Data Driven Pages

http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/datadrivenpages-class.htm

I've highlighted the property you're looking for below.

exportTopdf.png

View solution in original post

4 Replies
NeilAyres
MVP Alum

Export to pdf seems to default to making a multipage pdf.

So, don't change the filename in the loop.

BenVan_Kesteren1
Occasional Contributor III

Thanks Neil Ayres​, it seems I was looking for a harder way of doing things than necessary...

0 Kudos
BenVan_Kesteren1
Occasional Contributor III

Neil seems to have a much 'lighter' answer, but this is the code I used to produce the result I was looking for.

Thanks for your reply Neil, much better answer than mine.

import os, arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
#Create final output PDF file 
finalFile = r"C:\temp\BensMap.pdf"
if os.path.isfile(finalFile) 
  os.remove(finalFile)
  print 'Removed existing output file'
finalPDF = arcpy.mapping.PDFDocumentCreate(finalFile)
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum
  currentPage = str(mxd.dataDrivenPages.currentPageID)
  pageCount = str(mxd.dataDrivenPages.pageCount)
  print 'Exporting page %s of %s' % (currentPage, pageCount)
  tmpPDF = r"C:\temp\MAP_" + str(pageNum) + ".pdf"
  arcpy.mapping.ExportToPDF(mxd, tmpPDF)
  print tmpPDF
  finalPDF.appendPages(tmpPDF)
  del tmpPDF
del mxd
0 Kudos
FreddieGibson
Occasional Contributor III

If you leverage the ExportToPDF method of the mapping class you'll have to export each page one at a time and then append them into a single document. If you're using data driven pages and you want to export all of the pages to a single pdf in one go use the exportToPDF method of the data driven pages class instead of the ExportToPDF method of the mapping class.

Data Driven Pages

http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/datadrivenpages-class.htm

I've highlighted the property you're looking for below.

exportTopdf.png