Hi. I have a python script that exports a pdf file. I have published this script as a geoprocessing service and successfully used this in a web application to publish and return that pdf to the user.
In the script properties defined in the toolbox within ArcMap prior to publishing, one of the parameters is the name of the file to return. This is currently set to '%ScratchWorkspace%/myfilename.pdf'. This works fine so long as the name of the file I create is called 'myfilename.pdf'. What I can't figure out is how I can create and return a file with a different name.
For example, the pdf might reference a particular job or client, and I might want to look up the client name and job id and return a file called 'bobjones_1234AB.pdf'. Under the current method, every pdf that the user publishes needs to be saved locally to disk then renamed to something more meaningful.
Does anyone know how I could tell the geoprocessing service to change the output file name parameter during the script?
Regards,
-Paul
Solved! Go to Solution.
I did a similar project for a large council to automatically generate planning reports. The result was an ArcGIS Server geoprocessing script that took a lot identifier as the input parameter and generated a PDF planning report for the lot.
You should be able to create the PDF with any filename you like - just make sure it is unique. You can also use something like shutil.copyfile to place a copy of the file in a server directory for example:
D:\WebReports\bobjones\1234AB.pdf
Map the server directory to a virtual directory and then return the URL from your script:
http:// myserver /web-reports/bobjones/1234AB.pdf
Edit: had to add the spaces to stop GeoNet thinking this is a real link.
To pass the URL back from the script set up the tool with an output parameter:
I use a separate python script that uses a string as an input parameter and tags the date on the end separated by underscores - myfile_15_04_22. The "myfile" is the input parameter and I compile the date using the datetime library. The script returns the full file name string. I then put this in my model and make the input string a model parameter.
I hope that helps a bit.
Thanks for the reply Jeff.
I don't think that will help in this situation. I can get the script to change the file name (e.g. add date etc) and save that file locally - which is fine when I'm using and running the script locally using ArcMap. But I want to publish this as a geoprocessing service in ArcGIS Server that remote users can use to publish and download. In this scenario, the service seems to only return the file that exactly matches the name entered as the input parameter in the first place. In my case I don't know what the file name will be until the script is running (e.g. if I tag the date/time onto the file name).
If you think of anything else let me know.
I did a similar project for a large council to automatically generate planning reports. The result was an ArcGIS Server geoprocessing script that took a lot identifier as the input parameter and generated a PDF planning report for the lot.
You should be able to create the PDF with any filename you like - just make sure it is unique. You can also use something like shutil.copyfile to place a copy of the file in a server directory for example:
D:\WebReports\bobjones\1234AB.pdf
Map the server directory to a virtual directory and then return the URL from your script:
http:// myserver /web-reports/bobjones/1234AB.pdf
Edit: had to add the spaces to stop GeoNet thinking this is a real link.
To pass the URL back from the script set up the tool with an output parameter:
Thanks Owen. Your reply got me on the right track, combined with some help from Kevin at Esri who sent me the code pasted below. Have marked your answer as correct.
The step I was missing was setting the parameter to 'derived' in the script tool properties to make the arcpy.SetParameter line to work.
import arcpy, os
newName = arcpy.GetParameterAsText(0)
startFile = "e:/temp/Paul/start.zip"
#create file, do something, etc
outputFile = os.path.join(arcpy.env.scratchFolder, newName + ".zip")
arcpy.Copy_management(in_data=startFile, out_data=outputFile, data_type="File")
#Can also do shutil to copy, or just give the PDF the name you want when you originally create, thus you don’t need to rename
#Finally, set that file path as your derived output parameter
arcpy.SetParameterAsText(1, outputFile)