ExportToPDF failed with OSError

1494
4
06-22-2021 12:58 AM
MaximilianGlas
Esri Contributor

Hi guys,

I have a strange behaviour with my script which I wrote for a customer.

Just the relevant lines ...

import arcpy
import os
import sys
import logging
import logging.config
from datetime import datetime

# CONFIG Paths
Pdf_Path = r"C:\PrintServer"
Log_Path = r"C:\PrintServer\log"
Aprx_File = r"D:\data\PrintServer\COVID19_Print.aprx"

def main():
    # Input Layout template
    Layout_Template = arcpy.GetParameterAsText(0)

    #open the ProProject    
    try:
        aprx = arcpy.mp.ArcGISProject(Aprx_File)
        layouts = aprx.listLayouts(Layout_Template)
    except:
        raise

    #print each layout found
    out_files = []
    for lyt in layouts:
        try:                
            now = datetime.now()
            tmp_file_name = lyt.name + "_" + now.strftime("%Y%m%d_%H%M%S") + ".pdf"
            Temp_File = os.path.join(arcpy.env.scratchFolder, tmp_file_name)

            #This line always results in an OSError
            lyt.exportToPDF(Temp_File, resolution = 300)

            Output_File = os.path.join(Pdf_Path, tmp_file_name)
            import shutil
            shutil.move(Temp_File, Output_File)

            out_files.append(Output_File)
        except:
            raise

    arcpy.SetParameter(1, out_files)

if __name__ == '__main__':
    try:
        main()
    except:
        import traceback
        logging.error(traceback.format_exc())

The idea is to provide a GPService which takes a public WebMap, uses a predefined ArcGIS Pro Project (.aprx) also published with the GPService, reading a layout and printing this layout as pdf document.

In my tests on my machine and three different ArcGIS servers, this has been no problem. Also running on the customers ArcGIS Pro client worked as expected, but when rolling out on the customers server, we always get an OSError when executing the ExportToPDF method.

2021-06-08_15h38_08.png

 

What we already verified/tested:

  • different file directories: hard coded temp folder or the scratch folder from arcgis
  • shortened the file and directory names
  • tripple checked write permissions, we have
  • used different users, even full previliged from their IT department
  • executed the script via GPService or via python shell, both results in the same OSError
  • tested with ExportToJPG and ExportToPNG, both also produces the same error
  • tested, if a PDF program is necessary (it is not)
  • tested, if a PDF in general could be produced with python (is possible)

So, this works:

    #Create the file and append pages
    pdfDoc = arcpy.mp.PDFDocumentCreate(pdfPath)
    pdfDoc.appendPages(referencePdf)

    pdfDoc.saveAndClose()

 

And now I don't have any other idea.

Anybody of you have any?

Has anybody a deeper look into that tool and can say, why this error could happen?

 

Kind regards,

Max

Tags (2)
4 Replies
DanPatterson
MVP Esteemed Contributor

remotely connected??

BUG-000107084: The arcpy.mp module exportToPDF method crashes web t.. (esri.com)

 but there are 12 bugs historically associated with exportToPDF that don't seem relevant

Esri Support Search-Results

Your best bet would be Tech Support


... sort of retired...
0 Kudos
SamPalmer
New Contributor III

I am having the same issue. I can get it to run in one environment but not the other. I've tried with both ArcGIS Desktop and ArcGIS Pro and both are failing on Windows Server 2019 Datacenter. The working environment is Windows Server 2012 R2 Standard. 

A possible breakthrough on the non-working server is to kill the python process when I'm finished doing the export. Here is a snippet of code that I'm using with 3.7.10

 

import signal
pid = os.getpid()
os.kill(pid, signal.SIGTERM)

 

 

My initial tests seem to show this is working, but I don't know what other issues this might cause. I am using FireDaemon to manage my service, and I had to adjust some of the lifecycle settings to get it to work.

One other note, this seems to be a BUG-000106464. There is recent activity on this bug, so hopefully, we get a fix or workaround soon.

edit to add:
My service always runs one time successfully, then hangs. Perhaps this is different than your issue?

0 Kudos
MaximilianGlas
Esri Contributor

Hi Sam,

it seems that we could locate the reason for our problem.

There were some texts in our layout that were apparently responsible for the problem. When we removed the texts, printing worked without any problems.
We have found a patch that should fix the problem. Initial tests are promising.

ArcGIS Server 10.8.1 Print Service and Text Element Patch

Maybe this will work for you too.

Kind regards,

Max


Kind regards,

Max

0 Kudos
SamPalmer
New Contributor III

Max,

Thanks so much for the reply! Unfortunately, I'm not using ArcGIS Server, only clients, so I can't try that patch. I did try renaming the layout and map frame in ArcGIS Pro, but that didn't fix the issue. Killing the python session is still working though, so I think I have a fix for now.

-Sam

0 Kudos