Select to view content in your preferred language

PrintTask not working

1468
7
10-24-2013 09:09 AM
StephenFricke
Deactivated User
I am trying to print out a pdf of my web map, with a legend, scale bar, ect.  It seems like the PrintTask was the best way to do this.  For some reason my code is failing.  Here is where the button is created to run the PrintTask:

<br><button dojoType="dijit.form.Button"  onClick="PrintMap();">Print Map</button>


and here is the function where I am trying to print the map:

var printUrl= 'https://inside-dev2.nkn.uidaho.edu:6080/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task';

     function PrintMap(){

var printTask = new esri.tasks.PrintTask(printUrl);  
var params = new esri.tasks.PrintParameters();
var template = new esri.tasks.PrintTemplate();
params.map = map; 
template.layout= "A3 Landscape"
template.format= "PDF";
template.preserveScale = false;
params.template = template;

printTask.execute(params, printResult, printError)
      

    }
function printResult(result){
        console.log(result.url)
    }
    function printError(result){
        console.log(result);
    }
          


I seem to be going to the error function every time.  Can anyone pick up anything that is wrong with my code?  Thank you!
0 Kudos
7 Replies
StephenFricke
Deactivated User
As a follow up to this, when I change the printURL to "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export Web Map Task", I am able to open a print page with just the basemap, but when I add any map service layers to the map, the printTask does not work.  Is this an issue with the map services being located on a separate server from the ArcGIS server, and I need a proxy page in order to solve this issue?  I used one of the map services off of arcgisonline, "http://sampleserver6.arcgisonline.com/arcgis/rest/services/SampleWorldCities/MapServer", and when I added this to my map the printTask worked fine, so I am almost sure that it has to do with permissions to access my map services.  Any advice?
0 Kudos
JeffPace
MVP Alum
as far as i know, to use the print task all your services must be publicly accessable
0 Kudos
StephenFricke
Deactivated User
as far as i know, to use the print task all your services must be publicly accessable


The security setting for my map services are "public, available to everyone".
0 Kudos
TracySchloss
Honored Contributor
One of the things I had to do to get mine to work was to use proxy pages, and then include the serverUrl in it where my printService was being hosted.
0 Kudos
SubaKrishnan
Esri Contributor
I would suggest watching the network traffic using developer tools in Chrome or Firebug in Firefox browser, and look for console error messages. Also, watch the request that sent out and look if it throws an error message in the response.
0 Kudos
StephenFricke
Deactivated User
I would suggest watching the network traffic using developer tools in Chrome or Firebug in Firefox browser, and look for console error messages. Also, watch the request that sent out and look if it throws an error message in the response.


Thank you for the response.  When I use the PrintTask with one of my map service layers in the map, I get some errors in the console box which say, "Error {code: 400, message: "Unable to complete operation.", details: Array[1], log: undefined, _ssl: undefined}".  Do you think this confirms the fact that I need a proxy page or might there be something else going on?
0 Kudos
TracySchloss
Honored Contributor
When I've used PrintTask, it was using locally hosted services, not AGO, so my experience might not apply.  Past experience in general tells me that there's lot of situations where you end up needing a proxy page.  It's not just for security, but any time you are trying to pass too many characters in a request, that's a circumstance that requires a proxy page too. 

From the help:
A proxy page is necessary in the following situations:

    The application creates requests that exceed 2048 characters. While there is no official maximum length for a URL some modern browsers have imposed limits. Using a proxy works around this issue by performing a POST request rather than a GET request. POST requests are not limited by the 2048 character maximum because the information is transferred in the header instead of the URL. Common situations where you may exceed the URL length are:
        Buffer complex polygons then use the buffered geometry with a query task.
        Specify a spatial reference using well-known text (wkt).
    The application uses a service that is secured with token-based authentication , and you do not wish to allow users to view the token, or you do not want to transmit the token over the network between your Web server and your users.
    The application edits features in a feature service, unless the application is on the same domain as the GIS Server.
0 Kudos