Map2PDF PrintMap.js

779
8
01-24-2011 07:18 AM
AnnParker
New Contributor
I am a fairly new JSAPI programmer and I am trying to utilize the Map2PDF service in an application.  However it doesn't seem to be working completely.  I don't think I am calling the PrintMap.js function correctly as nothing is being returned for the layers parameter.  I keep receiving the error: "Invalid input json 'layers': A JSONObject text must begin with '{' at character 1 of   ." 

Any advice?
Thanks,
Ann
0 Kudos
8 Replies
MarcelKleinmann
New Contributor
Hi Ann,

It seems, as if your request parameter "layers" just contains a ".", instead of the layer urls. Perhaps try the whole thing this way:

I don't use the PrintMap.js, but the MapPrinter.js instead.

First I get the current http path of my own application and make a new instance of MapPrinter once in my init method, providing my esri.map and the path to the pdf directory of the application (line 1-4)

Later to generate a pdf print, I call the generatePdf method of MapPrinter, providing a pdf name (headline of the later pdf document), a ReportGenerator to use and an empty "params" string (line 6)


path = self.location.href;
pos = path.lastIndexOf('/');
path = path.substring(0,pos);
mapPrinter = new MapPrinter(map, (path+"/pdf"));

mapPrinter.generatePdf("pdfname", "SampleReport", "");


This works fine for me. Hope this helps,

Cheers
Marcello
0 Kudos
AnnParker
New Contributor
Marcello,
Thank you so much for your reply, I really appreciate your help.

Unfortunately it is still not working.  I took your advice and called MapPrinter.js instead.  I even looked at the sample.js which also used MapPrinter.js and is automatically installed with Map2PDF (and it works!) and tried to find what I'm doing wrong.  No luck.
The print button is now doing nothing and I have a browser error that says "Syntax Error; Line: 1; Char: 24"

I can't help but think that maybe the WAR file wasn't deployed properly.  Do I have to create a virtual directory for it in IIS?
Thanks for sticking with me,
Ann
0 Kudos
MarcelKleinmann
New Contributor
Hi Ann,

I don't have an idea, what it is, that throws you the syntax error. And I'm not familiar with IIS.

Can you post your code?


Cheers
0 Kudos
AnnParker
New Contributor
The code is attached.
Thank again for your help,
Ann
0 Kudos
MarcelKleinmann
New Contributor
Hi Ann,

the pdf generating function (print) must not be in your init() method, since there it would be called only once on load of the page.

Cut line 64 to 66 and paste to line 77 behind your init():

    var print = function() {
       mapPrinter.generatePdf("title", "report", "");
  };


Second thing: you don't call this function anywhere! Change your code in line 139 to:

<a href="javascript:print();" class="print">Print</a>



Best luck!

Cheers
Marcello
0 Kudos
MarcelKleinmann
New Contributor
Hi Ann,

did this help you?
0 Kudos
AnnParker
New Contributor
Sorry for not getting back to you sooner.  I had to focus on a more pressing issue.  Your last post helped quite a bit, especially pointing out that my print function cannot be in my init() method.  However I am still receiving an error.  IE is giving this error for the MapPrinter.js file:
'this.map.layerIds' is null or not an object
I am hopeful because at least it is calling the function now.  Any ideas?

Also, I should apologize about the code that I posted because I did not include the code within an external JS file that I was referencing.  That's where the print function (<a href="javascript:print();" class="print">Print</a>) was being called.  I've been troubleshooting so much that my code is a mess.

And once again, thank you thank you for your help.
~Ann
0 Kudos
MarcelKleinmann
New Contributor
Hi Ann,

obviously the MapPrinter.js doesn't find the layers of your map, when you try to generate the pdf.

The map.layerIds property is a basic property of each esri map, described as:
layerIds  String[]  Array of current TiledMapServiceLayers and DynamicMapServiceLayers added to the map.

The reason may be that your map variable is empty at the moment of pdf generation (print(); function). Do you use the right variable? When you call print(); in the external js, is the variable empty since referneced in a wrong way? Add an alert at this point to see the value of the property, like:

      alert("External JS: map layers: "+map.layerIds);


Best luck
Cheers
0 Kudos