Select to view content in your preferred language

Synchronous Custom Printing - output names are not unique?

2953
17
Jump to solution
06-20-2013 11:38 AM
TracySchloss
Honored Contributor
I have a custom print service set up, defined as synchronous.  I'm using the print widget and at first I thought I had everything working.  As I'm testing, I started to notice that some of my printouts weren't what I currently had on the screen, they were ones that I had created a little earlier.

I started looking closer at the output created in my arcgisoutput\printTemplate\ExportWebMap_GPServer, studying how long those files were around before they were automatically cleared out.  I haven't got it down to the minute, but around 15 minutes seems to be their life cycle.  Next I realized the once these were cleared, new files getting generated have the exact same names as the ones that I created several minutes ago!  Because the names are the re-used, the browser is displaying the cached PDF from the earlier print request, not the one I just created.


How can I work around this?  Why aren't these names unique?  Is this the behavior of the out of the box printTools or specific to my custom service?  Do I Just tell the users they have to clear their browser histories if they start seeing this?

I originally had this set up as asynchronous, but that came with its own set of problems, probably because the code sample I'm using was based on synchronous print requests.  I am 3 weeks into trying to get this print to work and I'm running out of time!
0 Kudos
1 Solution

Accepted Solutions
JianHuang
Deactivated User
try time.getTime(), which returns the unix time.

View solution in original post

0 Kudos
17 Replies
KenBurcham
Deactivated User
We have this same problem with printing from our (customized) Tax Parcel Viewer web app (haven't tried with others).  I have a note to look into it but haven't yet. 

The urls that come back from the server linking to the generated pdf will have a guid-like string of characters, with the last digit incrementing sequentially.  It seems like there is a session timeout mis-match or something, where the user who prints a couple of maps and then waits a bit and then goes to print another, will find themselves getting the first map they printed. The next time they try the process, they get the second print.  And it increments along until they get back to the actual map they were trying to get!  If you manually guess and change the last digit, sometimes you can find the one you're after faster by the ol' trial and error method.

Hope that gives a tiny bit more info as to what is happening.  I would love to know a better workaround or better yet, a fix!

Thanks,

ken.
0 Kudos
TracySchloss
Honored Contributor
It isn't a solution, but it does make me feel a little better that I'm not just seeing things.  I have observed the same 'stepping through the set' of PDFs that you are describing.  When I noticed the older printouts, at first I thought it was random.  Now that I've deliberately created several and monitored the output, it is definitely systematically walking through the list. 

My expectation was that the guid was forever unique, maybe based on a date/time somehow.  I've made a note of what that number is today, I'll see if it is different tomorrow. 

It did help to set the browsers to clear that history on exit.  I don't think that's a very user friendly solution.
0 Kudos
BenFousek
Deactivated User
var time = new Date();
dojo.place('<div style="padding-bottom:2px;border-bottom:1px solid #B5BCC7;margin-bottom:4px;"><a href="' + result.url + '?time=' + time.getTime() + '" target="_blank">' + title + '</a><br /><span style="font-size:10px;color:#444;">' + time.toLocaleString() + '</span></div>', 'tasks-print-result', 'last');


Add a timestamp to the url.

Tracy - You should throw a party once you get all the kinks out of printing. I did. 🙂
0 Kudos
TracySchloss
Honored Contributor
I have no clue where I would put that code - too cryptic for me!  Nowhere in my version of a print set up, based on the printer widget, do I see a place for me to specify a different output URL name.  You must be going about it the other way.  I thought about switching to purely printTask and printParameters, but I could find no examples of this and I'm SO CLOSE to getting this other to work.  Or so I thought. 

//functions for printing
function openPrint () {
  clearTextInput('txtTitle');
  var fp = dijit.byId('floater_print');
   if ((fp.style =="visibility: hidden;") || (fp.style="VISIBILITY:hidden;")) {
      fp.style.visibility="visible";
      fp.show();
   }      
}
function setupPrinting(){     //called from my init function        
var layouts = [{
            "layoutName": "FMDC_Landscape",
            "label": "Landscape - PDF",
            "format": "PDF",
            "options": {
                "legendLayers": [], // empty array means no legend
                "scalebarUnit": "Miles",
                "titleText": "",
                "customTextElements": [{
                    "legalDescriptionText": ""
                }, {
                    "subTitleText": ""
                }]
            }
        }, {
            "layoutName": "FMDC_Portrait",
            "label": "Portrait - PDF",
            "format": "PDF",
            "options": {
                "legendLayers": [], // empty array means no legend
                "scalebarUnit": "Miles",
                "titleText": "",
                "customTextElements": [{
                    "legalDescriptionText": ""
                }, {
                    "subTitleText": ""
                }]
            }
        }];
        
        // create the print templates, could also use dojo.map
        dojo.forEach(layouts, function(lo){
            var t = new esri.tasks.PrintTemplate();
            t.layout = lo.layoutName;
            t.label = lo.label;
            t.format = lo.format;
            t.layoutOptions = lo.options;
            templates.push(t);
        });
        
        var printer = new esri.dijit.Print({
            "map": map,
            "templates": templates,
            url: printUrl
        }, dojo.byId("PrintDiv2"));
        
        printer.startup();
        
        dojo.connect(printer, "onPrintStart", function(){
            var printTitle = dojo.byId("txtTitle").value;
            if (printTitle.length < 1) {
                printTitle = currentCountyName + " County";
            }
            var customElementString;
            var checkName = "";
            if (printName.length > 5) {
                checkName = 'Owner: ' + printName;
            }
            if (printDeed.length < 16) {   //feature has deed book and page information         
            customElementString = [
                {"legalDescriptionText": checkName+'\r\n Description :' + fullLegalString},
             { "subTitleText": print_subTitle}
            ];
            }else{
               customElementString = [
                {"legalDescriptionText": checkName+'\r\n Description :' + fullLegalString + '\r\n' + printDeed},
             { "subTitleText": print_subTitle}
            ];
            }
           // console.log("fullLegalString = "+fullLegalString + "<br> subTitleText = " + print_subTitle);
            for (var i = 0; i < templates.length; i++) {
                this.templates.layoutOptions.titleText = printTitle;
                this.templates.layoutOptions.customTextElements = customElementString;
            }
            
            dojo.connect(printer, "onError", function(err){
                console.log("Printer Error: " + err);
            });
            dojo.connect(printer, 'onPrintComplete', function(value){
                console.log('The url to the print image is : ' + value.url);
            });
        });
    }


The print tool is in a floating pane opened by a button. 

     <div id="floater_print" dojoType="dojox.layout.FloatingPane" title='Create Print Page' dockTo="dock_print" style="visibility:hidden; height:260px" closable="false" resizable="true" dockable="true">
          <div id="info">
            <div id="note">
              <ol>   
              <li> Enter a Title    
              <input id="txtTitle" style="width:250px" type="text"  value="Enter a map title" class="textBox"
              onclick="clearTextInput('txtTitle');"  />     
              </li>   
                <li> Click the dropdown arrow on the Print button to choose Landscape or Portrait. </li>
                <li> The 'Printing' prompt indicates your page is being generated. </li>
                <li> When the printout is ready, the print button will change to a link Printout.</li> 
                <li>Click Printout to view your PDF.</li>
             </ol>
            </div>
        <div id="PrintDiv2" > </div>
    </div> 
   </div>
0 Kudos
BenFousek
Deactivated User
Adding the querystring "?time=SOME_TIME" to the end of the url prevents cache.

In your code:

dojo.connect(printer, 'onPrintComplete', function(value){
    var time = new Date();
    var url = value.url + '?time=' + time;
    console.log('The url to the print image is : ' + url);
});
0 Kudos
TracySchloss
Honored Contributor
In any code related to the onPrintComplete event, wouldn't the value already be determined by the outcome of the print request? Could you maybe do something in onPrintStart instead?  

If I append the date to the url within the function, it doesn't seem like I'm changing the name of the URL or the output file name.  When I tried it, my console log looked different, but both the name of the PDF in the browser and the name generated on the server were the same names as always.
0 Kudos
BenFousek
Deactivated User
I don't know how you could do something onPrintStart because you don't have the image url back from the server yet.

Wait a second...I just noticed you are using the print widget. There isn't a way to alter the returned url before the widget turns the button into a link without hacking the widget on a local install. I use esri.tasks.PrintTask, which returns the output url, which I'm able to add the timestamp to before creating a link or button.
0 Kudos
JohnGravois
Deactivated User
i just wanted to chime in and let you all know that this is a REST bug that I logged a little while back which has been tested and confirmed fixed in 10.2.

[NIM090204:When using a custom print service, sometimes old print requests are returned due to the way output files are named]

sorry for the inconvenience.
0 Kudos
TracySchloss
Honored Contributor
Is there a suggested workaround to this bug?  Is it specific to synchronous requests? It seems odd that the report is "sometimes old print requests are returned" and not all the time. 

I'd be interested in finding a print example that does not work with the dijit, but instead works directly with the printTask.   From other threads, it sounds like there used to be some examples posted in earlier versions of the API.  I assume once the dijit was offered as the main print solution, these were removed.   If I can specify the name of the output file, that should be a solution to the repeated use of the output names.

If I use printTask and printParameters instead, then should I execute it as deferred?  If I submit my request that way, I assume this works as a submitted job and I need to listen for the results it?  Does that mean my print service needs to be defined as asynchronous?  It's synchronous right now.
0 Kudos