What's the correct data type for PDF file in arcpy.Setparameter() ?

369
1
Jump to solution
02-12-2020 08:15 AM
Arne_Gelfert
Regular Contributor

Trying to consume a Geoprocessing Service (webtool) out of ArcGIS Pro with JSAPI 3.31 in WAB DE.

I have geoprocessing service that's supposed to prepare a web map for printing. This was adapted from the example given in the documentation here - ConvertWebMapToArcGISProject—ArcPy | Documentation. Service is consumed from JSAPI (WAB widget) and would like users to be able to save/open the resulting PDF from the app.

The service readily produces PDF output in a server directory but every attempt to return some reference to it (as file or filepath) so that I can interact with it in Javascript has failed so far. Numerous other Geonet threads have suggested various fixes, and I've tested them all.

The error I get consistently points to the setting of parameter:

RuntimeError: Object: Error in setting parameter as text
Failed to execute‍‍‍

I've tried both

arcpy.SetParameter() 

and 

arcpy.SetParameterAsText(). 

In my geoprocessing tool, I've tried type "String" with "SetParameterAsText" and passing the path to the file, and I've tried type "File" with "SetParameter" and passing a reference to the actual file.

I've tried creating the file in 

arcpy.env.scratchWorkspace

as suggested in one thread, then doing the following to get a proper i/o file reference to it (as suggested elsewhere):

uniqueID = str(uuid.uuid1())
WebMapPDF = os.path.join(arcpy.env.scratchWorkspace, 'WebMap_{0}.pdf'.format(uniqueID))

layout.exportToPDF(WebMapPDF)

PDFOutputFile = open(WebMapPDF_forDownload)
PDFOutputFile.close()‍‍‍‍‍‍‍

arcpy.SetParameter(1, PDFOutputFile.name)   # I only have one GetParameter, so this is index 1

 

In JS, I was hoping to do something like this with a reference to the file:

gp.getResultData(jobInfo.jobId, "PDFOutputFile", this.printComplete);
 },
printComplete : function(result) {
 window.open(result.url);
 },

but haven't even gotten as far as getting an error because the geoproc service call keeps failing. As stated above, replacing the scratchspace reference with an actual server path and removing the setparameter, creates a beautiful PDF. So thee issue has to be with my last line of Python that set the output parameter or with configuring that parameter in the tool. Or potentially in how these get passed into the widget? Here is how I set up my Geoprocessor parameters....

var gpParams = {"Web_Map_as_JSON" : webMapAsJSON};
// do I need to include my PDFOutfile in the params ?
// var gpParams = {"Web_Map_as_JSON" : webMapAsJSONasString, 
//                "PDFOutputfile" : call it what?? };

gp.submitJob(gpParams, this.completeCallback, this.statusCallback);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
Arne_Gelfert
Regular Contributor

As a post-mortem exercise, here is what made it work:

WebMapPDF_forDownload = os.path.join(arcpy.env.scratchWorkspace, 'WebMap_{0}.pdf'.format(uniqueID))

PDFOutputFile = open(WebMapPDF_forDownload)
PDFOutputFile.close()
arcpy.SetParameter(1, PDFOutputFile.name)

The help came from here: Download File from Geoprocessing Service by URL 

To see how to marry this to the web and a custom widget that calls this service, see my other thread:

Help me understand how to use ConvertWebMapToArcGISProject.

View solution in original post

0 Kudos
1 Reply
Arne_Gelfert
Regular Contributor

As a post-mortem exercise, here is what made it work:

WebMapPDF_forDownload = os.path.join(arcpy.env.scratchWorkspace, 'WebMap_{0}.pdf'.format(uniqueID))

PDFOutputFile = open(WebMapPDF_forDownload)
PDFOutputFile.close()
arcpy.SetParameter(1, PDFOutputFile.name)

The help came from here: Download File from Geoprocessing Service by URL 

To see how to marry this to the web and a custom widget that calls this service, see my other thread:

Help me understand how to use ConvertWebMapToArcGISProject.

0 Kudos