AnsweredAssumed Answered

Save to PDF script not working

Question asked by rcsellman on Aug 20, 2014
Latest reply on Aug 21, 2014 by rcsellman

I downloaded the Save to PDF script from the this resources on  I am trying to get it to run in ArcMap but am consistently getting the error shown in this image:


What I want to do is publish the results as a Geoprocessing Service to ArcGIS Server, but can't get it to run correctly in ArcGIS Desktop.  Any thoughts as to whats going on here?


Here is the script, which again was downloaded from


import arcpy
import os

arcpy.env.overwriteOutput = True

def main():

    arcpy.AddMessage("  Collecting input parameters")

    # Get Initial Coordinates
    xMin = arcpy.GetParameterAsText(0)
    if xMin == '#' or not xMin:
        arcpy.AddError("Cannot proceed if xMin is not passed")

    yMin = arcpy.GetParameterAsText(1)
    if yMin == '#' or not yMin:
        arcpy.AddError("Cannot proceed if yMin is not passed")

    xMax = arcpy.GetParameterAsText(2)
    if xMax == '#' or not xMax:
        arcpy.AddError("Cannot proceed if xMax is not passed")

    yMax = arcpy.GetParameterAsText(3)
    if yMax == '#' or not yMax:
        arcpy.AddError("Cannot proceed if yMax is not passed")

    xMin = float(xMin)
    yMin = float(yMin)
    xMax = float(xMax)
    yMax = float(yMax)

    # Get the input spatial reference
    srValIn = arcpy.GetParameterAsText(4)
    if srValIn == '#' or not srValIn:
        arcpy.AddError("Cannot proceed if the Spatial Reference of your extent is not passed")
    srIn = arcpy.CreateObject('SpatialReference')

    # Get Layout Folder
    LayoutsFolderPath = arcpy.GetParameterAsText(5)
    if LayoutsFolderPath == '#' or not LayoutsFolderPath:
        arcpy.AddError("Cannot proceed if the Layout Folder is not passed")

    # Get template document
    Layout = arcpy.GetParameterAsText(6)
    if Layout == '#' or not Layout:
        arcpy.AddError(Layout + " does not exist. File not found:" + mxdName)

    mxdName = os.path.join(LayoutsFolderPath, Layout) 
    if not (os.path.exists(mxdName)):
        arcpy.AddError(Layout + " does not exist. File not found:" + mxdName)

    mapDoc = arcpy.mapping.MapDocument(mxdName)

    # Get the requested scale
    mapScale = arcpy.GetParameterAsText(7)
    if mapScale == '#' or not mapScale:
        mapScale = 0 # provide a default value if unspecified
    mapScale = float(mapScale)
    # layers only or layers and attributes
    attributesFlag = arcpy.GetParameterAsText(8)
    if attributesFlag.lower() == 'true':
        layers_string = "LAYERS_AND_ATTRIBUTES"
        layers_string = "LAYERS_ONLY"

    # Get the requested Map Title
    title = arcpy.GetParameterAsText(9)
    if title == '#' or not title:
        title = " " # provide a default value if unspecified

    # Get pdf file path to create
    outputPDFPath = arcpy.GetParameterAsText(10)
    if outputPDFPath == '#' or not outputPDFPath:
        arcpy.AddError("Cannot proceed without specifying an output path for the pdf file")

    # Build the extent in the Layout document
    dataFrame = arcpy.mapping.ListDataFrames(mapDoc)[0]

    arcpy.AddMessage("  Processing Map Extent")
    dataFrame.extent = calculateExtent_Original(xMin,xMax,yMin,yMax,dataFrame,srIn)
    # Set the scale if necessary
    if mapScale > 0:
        arcpy.AddMessage("  Setting scale to: " + str(mapScale))
        dataFrame.scale = mapScale

    # Set the Map title
    if title != " ":
        for elm in arcpy.mapping.ListLayoutElements(mapDoc, "TEXT_ELEMENT"):
            if elm.text == "Map Title":
               elm.text = title

    # Export
    arcpy.AddMessage("  Saving as PDF")
    arcpy.mapping.ExportToPDF(mapDoc,outputPDFPath, layers_attributes=layers_string)

def calculateExtent_Original(xMin,xMax,yMin,yMax,dataFrame,srIn):
    # Make input extent coordinates in the same sr as the data frame
    pntInLL = arcpy.Point()
    pntInLL.X = xMin
    pntInLL.Y = yMin

    pntInUR = arcpy.Point()
    pntInUR.X = xMax
    pntInUR.Y = yMax

    pntGeomInLL = arcpy.PointGeometry(pntInLL,srIn)
    pntGeomInUR = arcpy.PointGeometry(pntInUR,srIn)

    pntGeomList = []

    arcpy.CopyFeatures_management(pntGeomList, "in_memory/extentPnts")
    rows = arcpy.SearchCursor("in_memory/extentPnts", "", dataFrame.spatialReference)

    row =
    newPntLL = row.shape.getPart(0)

    row =
    newPntUR = row.shape.getPart(0)

  myExtent = arcpy.CreateObject('Extent', xMin, yMin, xMax, yMax)
  #arcpy.AddMessage("New extent: %s %s %s %s" % (myExtent.XMin, myExtent.YMin, myExtent.XMax, myExtent.YMax))

    return myExtent

if __name__ == "__main__":