Save to PDF script not working

961
2
08-20-2014 12:39 PM
RyanSellman
Occasional Contributor II

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

pyissues.PNG

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 arcgis.com:

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')

    srIn.loadFromString(srValIn)

    # Get Layout Folder

    LayoutsFolderPath = arcpy.GetParameterAsText(5)

    if LayoutsFolderPath == '#' or not LayoutsFolderPath:

        arcpy.AddError("Cannot proceed if the Layout Folder is not passed")

        return

    # Get template document

    Layout = arcpy.GetParameterAsText(6)

    if Layout == '#' or not Layout:

        arcpy.AddError(Layout + " does not exist. File not found:" + mxdName)

        return

    mxdName = os.path.join(LayoutsFolderPath, Layout)

    if not (os.path.exists(mxdName)):

        arcpy.AddError(Layout + " does not exist. File not found:" + mxdName)

        return

    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"

    else:

        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")

        return

    # 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 = []

    pntGeomList.append(pntGeomInLL)

    pntGeomList.append(pntGeomInUR)

    arcpy.CopyFeatures_management(pntGeomList, "in_memory/extentPnts")

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

    rows.reset()

    row = rows.next()

    newPntLL = row.shape.getPart(0)

    row = rows.next()

    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__":

    main()   

Tags (2)
0 Kudos
2 Replies
OwenEarley
Occasional Contributor III

A note on the CreateObject help page suggests using the Extent class directly. Try changing line 160 from:

# myExtent = arcpy.CreateObject('Extent', xMin, yMin, xMax, yMax)

myExtent = arcpy.Extent(xMin, yMin, xMax, yMax)

RyanSellman
Occasional Contributor II

That was it!  Thank you so much for your help!

0 Kudos