How to use a click button to print map which is already created

7586
29
03-23-2015 02:08 PM
RyanYang
New Contributor

I just got a task to add a print button to web page. Once user clicks this button, the map should be printed. Since the project is already there, I don't want make too much change to it. Thanks in advance.

0 Kudos
29 Replies
RudyProsser
Esri Contributor

Ryan,

Did you mix the AMD and legacy coding styles in a single app? I interpret the Uncaught Error as the map is not defined in the section with the Print widget, even though it is a global variable. Instead of using AMD for the Print widget, try the legacy dojo.require to accomplish the same thing.

Add this to the list of modules you're loading:

     dojo.require("esri.dijit.Print");

Add this to your initialization code after you've completed building your map:

    var myPrinter = new esri.dijit.Print({

          map: map,

          url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%2..."

        }, dom.byId( "printButton" ) );

    myPrinter.startup(); 

Rudy

0 Kudos
RyanYang
New Contributor

Hi Rudy,

I add the following code in the end of the init() function.

var myPrinter = new esri.dijit.Print( {

            map: map,

            url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%2..."

          }, dojo.dom.byId( "printButton" ) );

        myPrinter.startup();

The I get a print button on the page. But the print button doesn't take any action no matter how I click it. And there is not any error or warning message.

Thanks,

Ryan

0 Kudos
RudyProsser
Esri Contributor

Ryan,

When you click on the Print button it should go from Print to Printing to Printout, with Printout being a link to your printed document. Take a look at the Print and Print webmap samples to see the Print widgets behavior.

I see you're using the PrintingTools service on SampleServer6. You should switch to the PrintingTools service on your own ArcGIS for Server instance. The print service needs to access your services in order to create the output.

Rudy

0 Kudos
RyanYang
New Contributor

Hi Rudy,

Thanks for your reply.

If I put printButton outside of the map container, then the click button works. But there are some other problems.

Since the layer is dynamically generated on a server, so I set

     esri.config.defaults.io.proxyUrl = "mapping";

     esri.config.defaults.io.alwaysUseProxy = true;

in the very beginning of init().

When we try to print the map, we need bypass this proxy server, otherwise the application cannot find mapping\http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%2...

which prefixes the printUrl with mapping\.

To bypass the proxy server, I set 

     esri.config.defaults.io.alwaysUseProxy = false

in the printMap() function. Now the print url is correct, but run into another problem, I cannot see the map any more. I guess it's because ESRI map is asynchronous. But how to arrange events happen one by one?

Thanks,

Ryan

0 Kudos
OttoArturo
New Contributor

Hello

I am trying to use Print widget exactly as you say. I am using the ESRI proxy in my app. I already tested your example with the layers I am using in my app and work perfectly. But I can not make it work in my own app which uses the ESRI proxy. Any idea about how to figure it out. This widget is really useful and simple to use but it does not work in my case. Please help.

thank you.

0 Kudos
RyanYang
New Contributor

Hi everybody,

Thank you very much. Now I know how to print the map. Actually I just need know the url to the newly created map, and then design the print button in any way as I want. Very simple. But in the begin, I don't know the web flow of ESRI, so it was difficult.

Thanks again,

Ryan

0 Kudos
ChrisSergent
Regular Contributor III

So, do you have it working, or are you trying to figure out the URL still? It should open when you run the task.

0 Kudos
RyanYang
New Contributor

Now it works very well. JS set parameters of map based on user's action, like drag or zoom, then send a request our controller method. This method create a url to a GIS server This server will generate a map and return it. I just need save the url string in session, and then put this url in <a>. Maybe you have better idea.

The only problem is that I cannot print title, logo or something else.

Thanks again,

Ryan

0 Kudos
RyanYang
New Contributor

in <img>, not <a>

0 Kudos
NhuMai
by
New Contributor II

Hi Ryan,

Were you able to get the print output url from the print dijit or print task? I'm trying to access the same thing to get the print output to open automatically instead of just showing the link.

0 Kudos