0

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.

See here: enter image description here

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. Section of the script that is having issue pulling the correct report

4
  • Welcome to GIS SE. As a new user, please take the Tour. Please Edit the post to contain correct indentation on the Python code.
    – Vince
    Commented Nov 21, 2022 at 20:16
  • Look at your 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?
    – KHibma
    Commented Nov 23, 2022 at 14:56
  • 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)
    – KHibma
    Commented Nov 23, 2022 at 15:01
  • @KHibma The adding the report name into the list reports worked!! I did have to add in a wildcard as well because the tool kept failing to find the list. The Solution was listReports("REPORT NAME*")[0] Commented Nov 23, 2022 at 15:54

1 Answer 1

0

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)

Solution:

    listReports("*Report Name*")[0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.