Is there a print complete event in ArcGIS JavaScript 4.x

1615
16
Jump to solution
08-30-2019 12:13 PM
DanielWebb
Occasional Contributor

In ArcGIS JavaScript 3.x, there was an event for print-complete.

https://developers.arcgis.com/javascript/3/jsapi/print-amd.html#events

Does 4.x have that?  Or is there a way to see when the print widget has completed?

I noticed the Print widget in 4.x has the method 'hasEventListener()' but I can't figure out how it works.

Thanks!

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Daniel,

  Here's a bit of a workaround, because I don't know why the PrintViewModels state never changes to "complete".

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="initial-scale=1, maximum-scale=1,user-scalable=no"
    />
    <title>Print widget - 4.12</title>
    <link
      rel="stylesheet"
      href="https://js.arcgis.com/4.12/esri/themes/light/main.css"
    />

    <style>
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
        overflow: hidden;
      }
    </style>

    <script src="https://js.arcgis.com/4.12/"></script>
    <script>
      require([
        "esri/views/MapView",
        "esri/widgets/Print",
        "esri/WebMap",
        "dojo/aspect",
        "esri/core/watchUtils"
      ], function(MapView, Print, WebMap, aspect, watchUtils) {
        var webmap = new WebMap({
          portalItem: {
            // autocasts as new PortalItem()
            id: "d6d830a7184f4971b8a2f42cd774d9a7"
          }
        });

        var view = new MapView({
          container: "viewDiv",
          map: webmap
        });

        view.when(function() {
          var print = new Print({
            view: view,
            // specify your own print service
            printServiceUrl:
              "https://utility.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task"
          });
          
          watchUtils.when(print, 'viewModel', function(){
            aspect.after(print.viewModel, "print", function(deferred){
              return deferred.then(function(rslt){
                console.log("Print complete");
              });
            });
          });
          
          // Add widget to the top right corner of the view
          view.ui.add(print, "top-right");
        });
      });
    </script>
  </head>

  <body class="calcite">
    <div id="viewDiv"></div>
  </body>
</html>

View solution in original post

16 Replies
Noah-Sager
Esri Regular Contributor

If you're using the print widget, you could read the state of the PrintViewModel, and when the print completes, the state should change to complete.

https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Print-PrintViewModel.html...

DanielWebb
Occasional Contributor

I am using the Print widget.

Would the same problem exist?  It still needs an event finish in order for me to look at when the PrintViewModel changed status.  What would the code look like for something like that?

Thanks!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Daniel,

  Here's a bit of a workaround, because I don't know why the PrintViewModels state never changes to "complete".

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="initial-scale=1, maximum-scale=1,user-scalable=no"
    />
    <title>Print widget - 4.12</title>
    <link
      rel="stylesheet"
      href="https://js.arcgis.com/4.12/esri/themes/light/main.css"
    />

    <style>
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
        overflow: hidden;
      }
    </style>

    <script src="https://js.arcgis.com/4.12/"></script>
    <script>
      require([
        "esri/views/MapView",
        "esri/widgets/Print",
        "esri/WebMap",
        "dojo/aspect",
        "esri/core/watchUtils"
      ], function(MapView, Print, WebMap, aspect, watchUtils) {
        var webmap = new WebMap({
          portalItem: {
            // autocasts as new PortalItem()
            id: "d6d830a7184f4971b8a2f42cd774d9a7"
          }
        });

        var view = new MapView({
          container: "viewDiv",
          map: webmap
        });

        view.when(function() {
          var print = new Print({
            view: view,
            // specify your own print service
            printServiceUrl:
              "https://utility.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task"
          });
          
          watchUtils.when(print, 'viewModel', function(){
            aspect.after(print.viewModel, "print", function(deferred){
              return deferred.then(function(rslt){
                console.log("Print complete");
              });
            });
          });
          
          // Add widget to the top right corner of the view
          view.ui.add(print, "top-right");
        });
      });
    </script>
  </head>

  <body class="calcite">
    <div id="viewDiv"></div>
  </body>
</html>
DanielWebb
Occasional Contributor

Thanks Robert!  That does the trick.

0 Kudos
DanielWebb
Occasional Contributor

Actually, it doesn't work. The print would "complete" but when I click the link to the pdf it errors.

The link it gives is "<website>/undefined".

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Daniel,

   It works fine for me. Maybe you are unaware that the print server does not maintain the output file for an extended period of time. The Server deletes all files in the output folder at a certain age interval.

ArcGIS Server periodically deletes (or cleans) old unneeded files from the jobs, output, and system directories to prevent them from taking more disk space than necessary.

DirectoryCleanup modeMaximum file age

jobs

Time since last modified

360 minutes (6 hours)

Server directories—ArcGIS Server Administration (Windows) | ArcGIS Enterprise 

0 Kudos
DanielWebb
Occasional Contributor

That's not it. I click the link as soon as the export is finished.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Daniel,

  I don't get that at all on my end. Are you using your own print service or something?

0 Kudos
DanielWebb
Occasional Contributor

Yes, it's a custom print service

0 Kudos