PrintTask Issues - Javascript 4.3 SDK

1390
9
04-13-2017 11:28 PM
JordanBaumgardner
Occasional Contributor III

After many hours of looking at this, I think I'm brave enough to call these issues bugs in the Javascript 4.3 SDK

I have found workarounds (included below) for the following issues.

Bug #1
-- in PrintTask._setPrintParams(), the resulting Web_Map_as_JSON does not include MapView.rotation

Bug #2
-- in PrintTask._setPrintParams(), the resulting Web_Map_as_JSON does not include a visibleLayers []. For some layers, the GP service does not respect sublayer visibility. I have not determined what the difference is between those that work and those that do not.

Bug #3
-- in PrintTask._setPrintParams(), the resulting Web_Map_as_JSON graphic point layers symbols do not have a style attribute.  Web_Map_as_JSON -> Graphics layer -> Points -> Symbols -> Style missing

My Workaround

wapp.objects.CustomPrintTask = EsriPrintTask.createSubclass({
      execute: function (a, b) {
            internalPrintPrams = this._setPrintParams(a);

            // parse current esri version
            var mapJson = JSON.parse(internalPrintPrams.Web_Map_as_JSON);

            // add rotation
            mapJson.mapOptions.rotation = -1 * a.view.rotation;

            // create visibleLayers node
            $(mapJson.operationalLayers).each(function (opIdx, opLayer) {
                  var visibleLayers = [];
                  $(opLayer.layers).each(function (subIdx, subLayer) {
                        visibleLayers.push(subLayer.id);
                  });
                  opLayer.visibleLayers = jQuery.makeArray(visibleLayers);
            })// for each Operational Layer

            // Remove empty layers
            mapJson.operationalLayers = jQuery.grep(mapJson.operationalLayers, function (layer, idx) {
                  return ((layer.visibleLayers.length > 0) 
                        || ((layer.featureCollection) && (layer.featureCollection.layers) && (layer.featureCollection.layers.length > 0)));
            }); // grep Operational Layers

            // Add "style": "esriSMSCircle", to draw features that don't have it
            jQuery.each(mapJson.operationalLayers, function (opIdx, opLayer) {
                  if ((opLayer.featureCollection)&&(opLayer.featureCollection.layers) && (opLayer.featureCollection.layers.length > 0)) {
                        jQuery.each(opLayer.featureCollection.layers, function (featureSetIdx, featureSet) {
                              jQuery.each(featureSet.featureSet.features, function (featureIdx, feature) { 
                                    if ((feature.symbol)&&(!feature.symbol.style)) {
                                          feature.symbol.style = "esriSMSCircle";
                                    }
                              }); // for each featureSet
                        });
                  }
            }); // for each opLayer, add style to symbols that don't have it

            internalPrintPrams.Web_Map_as_JSON = JSON.stringify(mapJson);
            return this._geoprocessor["async" === this.mode ? "submitJob" : "execute"](internalPrintPrams, b)
                  .then(this._handleExecuteResponse)
      }
});
0 Kudos
9 Replies
UndralBatsukh
Esri Regular Contributor

At 4.3, PrintTask does not support rotation. This feature is coming at 4.4. I will investigate the other two points you brought up. 

Thanks,

-Undral

DavidPlume1
New Contributor III

I'm also having problems showing all of my visible sublayers in my printTask.  Undral -- any insights into that problem would be appreciated very much.

Thanks

David

0 Kudos
JordanBaumgardner
Occasional Contributor III

There is a bug in the Web_Map_as_JSON of the EsriPrintTask. I worked around it by creating my own subclass and walking the layer chain and building the visibility list.

(function (wapp) {
wapp.objects = wapp.objects || {};
require([
"esri/tasks/PrintTask",
"esri/core/Accessor"
], function (
EsriPrintTask,
Accessor
) {
wapp.objects.CustomPrintTask = EsriPrintTask.createSubclass({
execute: function (a, b) {
internalPrintPrams = this._setPrintParams(a);

// parse current esri version
var mapJson = JSON.parse(internalPrintPrams.Web_Map_as_JSON);

// add rotation
mapJson.mapOptions.rotation = -1 * a.view.rotation;
// Force scale
mapJson.mapOptions.scale = a.view.scale;

// create visibleLayers node
$(mapJson.operationalLayers).each(function (opIdx, opLayer) {
var visibleLayers = [];
$(opLayer.layers).each(function (subIdx, subLayer) {
visibleLayers.push(subLayer.id);
});
opLayer.visibleLayers = jQuery.makeArray(visibleLayers);
})// for each Operational Layer

// Remove empty layers
mapJson.operationalLayers = jQuery.grep(mapJson.operationalLayers, function (layer, idx) {
return ((layer.visibleLayers.length > 0)
|| ((layer.featureCollection) && (layer.featureCollection.layers) && (layer.featureCollection.layers.length > 0)));
}); // grep Operational Layers

// Add "style": "esriSMSCircle", to draw features that don't have it
jQuery.each(mapJson.operationalLayers, function (opIdx, opLayer) {
if ((opLayer.featureCollection)&&(opLayer.featureCollection.layers) && (opLayer.featureCollection.layers.length > 0)) {
jQuery.each(opLayer.featureCollection.layers, function (featureSetIdx, featureSet) {
jQuery.each(featureSet.featureSet.features, function (featureIdx, feature) {
if ((feature.symbol)&&(!feature.symbol.style)) {
feature.symbol.style = "esriSMSCircle";
}
}); // for each featureSet
});
}
}); // for each opLayer, add style to symbols that don't have it

internalPrintPrams.Web_Map_as_JSON = JSON.stringify(mapJson);
return this._geoprocessor["async" === this.mode ? "submitJob" : "execute"](internalPrintPrams, b)
.then(this._handleExecuteResponse)
}
});
}); // require
}(window.wapp = window.wapp || {}));

0 Kudos
DavidPlume1
New Contributor III

Jordan, 

Thanks for your response -- it is looking like I might have to study your work around in detail.  I'm a bit surprised that ESRI would leave such a bug unfixed -- and if it is unfixed, I'm surprised we're not seeing more conversation about it.

0 Kudos
JordanBaumgardner
Occasional Contributor III

np, let me know if you have any questions. Basically, all it does is intercept the WebMapToJson and make some tweaks to add Visibility and rotation.

0 Kudos
JordanBaumgardner
Occasional Contributor III

Oh, and you can ignore the Graphics stuff - there is an error printing custom graphics that is probably not needed for what you are doing. We had an odd requirement to have unique graphics for each feature.

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi Jordan Baumgardner‌ and David Plume‌,

So I failed to check on the issues that Jordan brought up. I apologize for that. Here is a really simple application that prints webmap using print widget (which uses printtask) and I am not able to reproduce the issue. https://codepen.io/anon/pen/gXOPmP?&editors=100 

Is it possible for one of you two share a simple test case with me? Or the webmap in question. If you cannot create a simple test case, then can you copy and paste the part where printparameters are being set. If you cannot share your webmap, then can you please give some ideas what kind of layers you have in the webmap and if min/max scales are being set for the layers. 

Thank you,

-Undral

0 Kudos
JordanBaumgardner
Occasional Contributor III

I'll try to set something up. But you can see rotation and the visible layer issue right away. Simply rotate the map and print. The map will not be rotated with 4.3 - It's simply not there. Same with opLayer.visibleLayers .

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi Jordan, 

Have you tried the app with 4.4 or 4.5? The rotation support is in. I am still not able to reproduce the issue with visible layers. 

0 Kudos