I am in the process of building a custom Widget for Printing multiple reports on different layers. This print widget is from a custom print service created in ArcGIS Pro from the Export Web Map and Get Layout Templates Info Geoprocessing Services.
I mainly followed the example here: https://www.esri.com/arcgis-blog/products/arcgis-enterprise/mapping/print-reports-from-arcgis-enterp...
I have tweaked the example script to pull multiple reports from the layout folder location and reference the data layer for that particular report
def exportReport_NationalWetlandsInventory2021(p, folder, outfile):
m = p.listMaps()[0]
lyrs = m.listLayers("*National*")[0] #Change wildcard here to find your layer
reportfn_Wet = os.path.join(folder,'{}.{}'.format('NationalWetlandsInventory','rptx'))
p.importDocument(reportfn_Wet)
r = p.listReports()[0]
r.setReferenceDataSource(lyrs)
outReportFileName = generateUniqueFileName('pdf')
r.exportToPDF(outReportFileName)
pdf = arcpy.mp.PDFDocumentOpen(outfile)
pdf.appendPages(outReportFileName)
pdf.saveAndClose()
os.remove(outReportFileName)
os.remove(reportfn_Wet)
def exportReport_ManagedConservationLandsReport(p, folder, outfile):
m = p.listMaps()[0]
lyrs = m.listLayers("*Managed Conservation*")[0] #Change wildcard here to find your layer
reportfn_manage = os.path.join(folder,'{}.{}'.format('ManagedConservationLands','rptx'))
p.importDocument(reportfn_manage)
r = p.listReports()[0]
r.setReferenceDataSource(lyrs)
outReportFileName = generateUniqueFileName('pdf')
r.exportToPDF(outReportFileName)
pdf = arcpy.mp.PDFDocumentOpen(outfile)
pdf.appendPages(outReportFileName)
pdf.saveAndClose()
os.remove(outReportFileName)
os.remove(reportfn_manage)
def generateUniqueFileName(outFormat):
guid = str(uuid.uuid4())
fileName = ""
fileExt = outFormat
#changing the file extension for few instances
if outFormat == "png8" or outFormat == "png32":
fileExt = "png"
elif outFormat == "tiff":
fileExt = "tif"
fileName = '{}.{}'.format(time.strftime("%H%M%S") + guid, fileExt)
fullFileName = os.path.join(arcpy.env.scratchFolder, fileName)
return fullFileName'
The widget prints a majority of the reports based on the corresponding layer. However, one layer, randomly chosen by the widget, does not get it's report pulled and is instead placed in a duplicate report template of the one printed right before it.
Curious if anyone has printed multiple reports using a single print service widget and can advise on how to resolve this duplicating report problem.
Update:
I have figured out that this issue occurs when the report(r) is exported to PDF. It seems to pull the file path from the Report directly above in the script. The data that is pulled in from the layer is correct but printing to the wrong report template.
generateUniqueFileName
function in the code, there is nothing in there to support PDF. Only png and tiff -- or maybe it just passes through and allows pdf if that's what's passed in?r = p.listReports()[0]
is how your code decides WHAT report to use.[0]
means, use the first one it finds when it LISTS. It's possible the list ORDER changes when it's published, so you probably shouldn't rely on grabbing the first one. If you're a little more explicit, and pass in the NAME of what you want, you can be sure it'll use the one you want:listReports("REPORT NAME")[0]
(You still need the [0] to get a single report because it will always return a list, but it should be a list with a single item now)