Select to view content in your preferred language

PrintTemplate: change map title dynamically?

4779
12
Jump to solution
10-23-2015 01:46 PM
DavidChrest
Occasional Contributor II

Is there a straightforward way for a user to change the map title text when printing a map instead of having it hard coded in the layoutOptions of the PrintTemplate?

I tried the following but did not work.

HTML:

<div id="print_button"></div> Title:
  <input type="text" id="mapTitle" size="5" value="title" style="height: 18px; width:200px; border: 1px solid #759DC0;" />

JS:

var MT = dom.byId("mapTitle").value


  // create a print template for each choice
  templates = array.map(templateNames, function (ch) {
  var plate = new PrintTemplate();
  plate.layout = plate.label = ch;
  plate.format = "PDF";
  plate.showAttribution = false;
  plate.exportOptions = {
  dpi: 150
  };
  plate.layoutOptions = {

  "titleText": MT,
  "scalebarUnit": "Miles"
  };
  return plate;
  });


  // create the print dijit
  printer = new Print({
  "map": map,
  "templates": templates,
  url: printUrl
  }, dom.byId("print_button"));
  printer.startup();
  }

Any help is very much appreciated.

0 Kudos
12 Replies
DavidChrest
Occasional Contributor II

Got it! I tool a closer look at Chris' code in the link he mentions above. https://github.com/csergent45/gisTemplate.

I commented out my JS and HTML portions related to my print widget, placed in his print HTML and JS segments, and everything worked wonderfully. I did some HTML fine tuning and I can now pop in any map title I want. His setup was also more concisely organized than mine.

Here is what I have:

Capture.PNG

TracySchloss
Frequent Contributor

Just to be clear, this still doesn't use Print widget, correct?  You're using code that looks like it, but is based on printTask and printParameters?  That's the only kind of solution I could ever figure out.

I've had good luck with a deferred, since I wanted the PDF the open automatically when it was done generating (I had complaints from users when I offered a 'view printout' button).  Without the deferred, the PDF tried to open  before it's was done generating. 

I've also had some problems with the print service returning old output.  It turns out the same few names are recycled and the print cache on the server cleared every XX minutes (don't remember the time). You'll see this especially in your testing, since you're likely to be hitting your print button frequently.  If you append the date to the name, it will also incorporate a timestamp so that doesn't happen. 

 <!DOCTYPE html>
  <html>  
  <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/> 
    <title>Basic Print with Print Task</title> 
    <link rel="stylesheet" href="https://community.esri.com//js.arcgis.com/3.13/dijit/themes/claro/claro.css"/> 
    <link rel="stylesheet" type="text/css" href="https://community.esri.com//js.arcgis.com/3.13/esri/css/esri.css"/> 
    <link type="text/css" rel="stylesheet" href="css/style.css">


   <script type="text/javascript">
     var dojoConfig = { 
      parseOnLoad: false,
      async:true
      };
  </script>


  <script type="text/javascript" src="https://js.arcgis.com/3.13compact/"></script>
  </head>
  
<body class="claro">
    <script type="text/javascript">
      var printServiceUrl = "/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task"; 
      var pathName = "https://ourserver.mo.gov";
      var layouts = [];
      var templates = [];
      var template;
      
     require([ 
      "dojo/on",
      "dojo/dom",
      "dojo/parser",
      "esri/map", 
      "esri/config",
      "esri/urlUtils",
      "esri/tasks/PrintTask",
      "esri/tasks/PrintTemplate", 
      "esri/tasks/PrintParameters",
      "dojo/_base/array", 
      "dijit/form/Button",
      "dijit/form/Select",
      "dijit/registry",
      "dojo/domReady!" ], 
      function(
        on, dom,parser,Map, esriConfig,urlUtils, PrintTask,PrintTemplate,PrintParameters,arrayUtils,Button,Select,registry ) {
  
      parser.parse();
  
  esriConfig.defaults.io.proxyUrl = pathName+"/proxy/proxy.ashx";//using proxy in [HOME] folder on server
  

   var map = new Map("mapDiv", { 
      center: [-92.593, 38.5],
      zoom: 7,
      basemap:'topo'
    });   


 on(map,'load', setUpPrinting);


//---- functions for printing ----
function setUpPrinting() {
    layouts = [{
            "layoutName": "Letter ANSI A Landscape",
            "label": "Landscape",
            "format": "PDF",
            "options": {
                "legendLayers": [], // empty array for no legend
                "scalebarUnit": "Miles",
                "authorText": "Missouri Office of Geospatial Information",
                "titleText": ""
            }
        }, {
            "layoutName": "Letter ANSI A Portrait",
            "label": "Portrait",
            "format": "PDF",
            "options": {
                "legendLayers": [], // empty array for no legend
                "scalebarUnit": "Miles",
                "authorText": "Missouri Office of Geospatial Information",
                "titleText": ""
              }
            }, {
            "layoutName": "MAP_ONLY",
            "label": "Map Only",
            "format": "JPG",
            "options": {
                "legendLayers": [], // empty array for no legend
                "scalebarUnit": "Miles",
                "titleText": "" 
             }
            }];
     var templateList = registry.byId('templateSelect');
     
       arrayUtils.forEach(layouts, function(layout){
            var pTemplate = new PrintTemplate();
            pTemplate.layout = layout.layoutName;
            pTemplate.label = layout.label;
            pTemplate.format = layout.format;
            pTemplate.layoutOptions = layout.options;
            pTemplate.showAttribution = false;
            templates.push(pTemplate);
                       
            templateList.addOption({
             label: layout.label,
             value: layout.label
           });            
        });   
  registry.byId("btnPrintSubmit").on("click", submitPrint); 
}


   function submitPrint(){
    var deferred;
    var printTitle = registry.byId("txtTitle").get("value");
    if (printTitle.length < 1) {
      printTitle = "Sample Map";
    }
    var printParams = new PrintParameters();
    printParams.map = map;
    var status = dom.byId("printStatus");
    status.innerHTML = "Generating ...";
    var e = registry.byId("templateSelect");
    var choice = e.value;
    template = templates[0];
    switch (e.value) {
      case "Portrait":
        template = templates[1];
        break;
      case "Landscape":
        template = templates[0];
        break;
      default:
        template = templates[0];
    }
    printParams.template = template;
    template.layoutOptions.titleText = printTitle;
  
    var printTask = new PrintTask(pathName+printServiceUrl, printParams);
    deferred = printTask.execute(printParams);
    deferred.addCallback(function(response){
      var d = new Date();
      var dateTime = d.getTime();
      var outputUrl = response.url + '?time=' + dateTime;
      status.innerHTML = "";
   //   console.log("outputURL: " + outputUrl);
      var select = registry.byId("templateSelect");
      var selectOptions = select.getOptions();
      select.set("value", "Choose Page Format");
      window.open(outputUrl, "_blank");
    });
    deferred.addErrback(function(error){
      console.log("Print Task Error = " + error);
      status.innerHTML = "Error generating printout, try again";
    });
  }
});
</script> 


<div id="mainWindow" data-dojo-type="dijit/layout/BorderContainer" design="headline" style="width:100%; height:100%;">
    <div id="banner" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'top'">
        <div id="header">
            <b> Enter a Title for Your Map:</b>
            <div id="printContent">
                <input id="txtTitle" data-dojo-type="dijit/form/TextBox" placeHolder="Enter a map title" />
            </div>
            <select id='templateSelect' data-dojo-type='dijit/form/Select'>
                <option value="choose">Choose Page Format</option>
            </select>
            <button id="btnPrintSubmit" data-dojo-type="dijit/form/Button">
                Submit
            </button>
            <div id="printStatus">
            </div>
        </div>
    </div>
    <div data-dojo-type="dijit/layout/ContentPane" id="centerPane" data-dojo-props="region:'center'">
        <div id="mapDiv" data-dojo-type="dijit/layout/ContentPane">
        </div>
    </div>
</div>
   </body>
  </html>
0 Kudos
DavidChrest
Occasional Contributor II

Yes, it certainly looks like his code is mostly utilizing printTask and printParameters. Have a look at his link on GitHub. You will want to look at https://github.com/csergent45/gisTemplate/blob/master/simpleMap.html and https://github.com/csergent45/gisTemplate/blob/master/js/reactor.js.

The print code is not that much, really. This does all I need for now. Having the PDF open automatically is one step better, but for now, I'm just glad this works. We use Server 10.3. I will definitely look at your code above. Looks like we have things covered.

Thanks so much!