Advertisement
Guest User

Untitled

a guest
May 27th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require(["esri/tasks/PrintTemplate", "esri/tasks/LegendLayer", "esri/tasks/PrintTask", "esri/tasks/PrintParameters"],
  2.             function (PrintTemplate, LegendLayer, PrintTask, Printparameters) {
  3.                 $("#print-progress").show(); //Printing is usually async and takes a while, show some progress
  4.                 map.getLayer("layer0").setVisibility(false); //Hide any layers you don't want to show up in the map, in this case the  base map
  5.                 var printTemplate = new PrintTemplate();
  6.  
  7.                 printTemplate.format = $("#print-format").val(); //[pdf | png | png8 | jpg | gif | eps | svg | svgz]
  8.                 printTemplate.layout = $("#print-template").val(); //The file name of a layout template in the server’s print template directory, you can generally count on MAP_ONLY, A3 Landscape, A3 Portrait, A4 Landscape, A4 Portrait, Letter ANSI A Landscape, Letter ANSI A Portrait, Tabloid ANSI B Landscape and Tabloid ANSI B Portrait to be there.
  9.  
  10.                 printTemplate.preserveScale = true; //Yep
  11.                 printTemplate.showAttribution = false; //Show the imagery sources
  12.  
  13.                
  14.                 printTemplate.exportOptions = { dpi: Number($("#print-dpi").val()) }; //DPI
  15.  
  16.  
  17.                 //Here we want to set up our legend.. generally we want to exclude the legend for things like base imagery layers and such (the RGB legend one)
  18.                 var legendLayers = [];
  19.  
  20.                 for (var layer in layers) { //Layers here is an object of key value pairs where the key is a name and the value is a reference to an esri Layer
  21.                     if (layers[layer] == layers.RentonImagery || layers[layer].visibleLayers == [-1]) //Exclude the layers we don't want in there.. including layers that aren't visible (have visible layers set to -1)
  22.                         continue;
  23.  
  24.                     //Do add the ones we want to appear in the legend
  25.                     var legendLayer = new LegendLayer();
  26.                     legendLayer.layerId = layers[layer].id;
  27.                     legendLayers.push(legendLayer);
  28.                 }
  29.                 printTemplate.layoutOptions = { titleText: "Boeing Renton Environmental Data Management System", legendLayers: legendLayers }  //Other options include authorText, copyrightText, scalebarUnit, legendLayers(esri.tasks.LegendLayer[]), customTextElements (object[])
  30.  
  31.                 var printTask = new PrintTask(exportURL, { async: true }); //export url is the url of the print service, something like https://gis1imcloud1.amec.com/arcgis/rest/services/7622/SecurePrint/GPServer/Export%20Web%20Map
  32.                 var printParams = new PrintParameters();
  33.  
  34.                 printParams.map = map; //Yep
  35.                 printParams.outSpatialReference = map.spatialReference; //Yep
  36.                 printParams.template = printTemplate; //Yep
  37.  
  38.                 var success = function (result) {
  39.                     //Result will have a url attribute with the location of the exported map
  40.                     $("#print-progress").hide();
  41.                     $("#print-tool").modal("hide");
  42.                     window.open(result.url);
  43.                 };
  44.                 var fail = function (error) {
  45.                     //Error handling
  46.                     $("#print-progress").hide();
  47.                     $("#print-error").text("Failed to print.");
  48.                     $("#print-error").show();
  49.                 };
  50.  
  51.                 printTask.execute(printParams).then(success, fail); //Finally make the call
  52.  
  53.                 map.getLayer("layer0").setVisibility(true); //Turn our basemap/other layers back on
  54.  
  55.         });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement