3

I have a web page (SSL) that contains a barely visible iframe whose source is a PDF streamed from a Java servlet. The purpose of the barely visible iframe is to render a PDF which opens a print dialog box right away when the page is loaded.

This process is working in a sporadic manner. More often than not, when the page is loaded, the PDF print dialog box comes up correctly. Occasionally, though, the PDF print dialog box is never rendered when the page loads. I know it's not an issue in the Java servlet as I can verify from the logs that the PDF is streamed correctly to the calling page. I need to find a way to make it 99.9999.......% reliable if at all possible that the PDF print dialog ALWAYS opens when the page is loaded. So, I'm looking for any tips on how to ensure that this will happen.

This needs to work for Internet Explorer (version 8 in particular).

This is the JavaScript that opens the page that contains the PDF iframe:

function openPrintCheckWindow(){
    pc=window.open("/print_check.jsp", "pc", "toolbar=no,scrollbars=no,resizable=no");
    pc.resizeTo(1000,700);
    pc.moveTo(80,80);
}

Here's the source for the page that contains the PDF iframe:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
    <TITLE></TITLE>

    <style> 
    #createpdf {
        position:absolute;
        top:0; 
        right:0; 
    }
    .micr {
        font-family: "MICR Encoding", sans-serif;
        font-size: 0.2in;
    }
    </style>

    <script type="text/javascript"> 
    function doAlert(msg){
    if(msg.length>0){
        alert(msg);
    }
    }

    //doesn't block the load event
    function createIframe(){
        var i = document.createElement("iframe");
        i.src = "/PrintCheckServlet";
        i.width = "2px";
        i.height = "2px";
        document.getElementById("createpdf").appendChild(i);
    };

    // Check for browser support of event handling capability
    if (window.addEventListener)
        window.addEventListener("load", createIframe, false);
    else if (window.attachEvent)
        window.attachEvent("onload", createIframe);
    else 
        window.onload = createIframe;
    </script>

</HEAD>

<BODY BGCOLOR="#DFD9D0" TEXT="#000066" onLoad="doAlert('')">
    <FORM METHOD="POST" TARGET="main_window" ACTION="ControllerServlet">            
    <P ALIGN="LEFT">
        <INPUT TYPE="BUTTON" NAME="CLOSE" VALUE="Close" 
         onClick="this.form.submit();window.close();window.opener.focus();window=null;">
        <br />
        <H3 id="print_message">A print dialog box will show momentarily</H3>
    </P>
    <div id="createpdf"></div>
    </FORM>
</BODY>
</HTML>
Zack Macomber
  • 6,682
  • 14
  • 57
  • 104
  • 1
    `window=null;window.opener.focus();` -- that doesn't seem like a good idea :-) – Pointy Apr 23 '12 at 16:51
  • 2 thoughts... 1.) you said you serve this up on SSL... if you have Internet Explorer users, be sure to **NOT** explicitly deny caching in your HTTP Headers - IE will delete the file before it loads in some cases. 2.) again for IE (this time IE9 and above) IE changed its behavior to **NOT** wait until a popup has fully loaded before continuing to execute script content... if your code after launching a popup *needs* to interact with it, be sure to add enough delay to ensure it exists and is ready. – scunliffe Apr 23 '12 at 16:56
  • @Pointy - yes, it sure doesn't...that code seems to be working ok, though - it was code that I "inherited" from the previous developer - would it be better to do `this.form.submit();window.close();window.opener.focus();window=null;`? – Zack Macomber Apr 23 '12 at 16:57
  • 1
    Setting `window` to `null` *before* calling `window.opener.focus()` will just cause a JavaScript error, so yes :-) – Pointy Apr 23 '12 at 16:58
  • 1
    Do you get different results on different browsers? You say it's sporadic - is that the case on Chrome/FF/IE/Safari equally? – halfer Apr 23 '12 at 16:59
  • 2
    Just updated question to indicate this is for IE8 in particular – Zack Macomber Apr 23 '12 at 17:03
  • @scunliffe - I've read a little bit up on setting cache in HTTP Headers but I've never actually implemented it. This is a Java web app with an app server of WebLogic. Could you provide links for reading material for how to not explicitly deny caching in HTTP Headers? – Zack Macomber Apr 23 '12 at 17:15
  • @ZackMacomber - the HTTP Headers are what your server (WebLogic) sends to the client (Internet Explorer). If you view your app in Firefox w/Firebug or Chrome, or IE (with HTTP Watch installed) you can see the actual requests and responses going to/from the server. You specifically want to ensure that you are not sending a "Cache-Control: no-store, max-age=0, must-revalidate, no-cache" or similar header... or an "Expires: Some Date In The Past" header... if you are, find out where it is being set and remove it. – scunliffe Apr 23 '12 at 17:37
  • possible duplicate of [Generate a PDF that automatically prints](http://stackoverflow.com/questions/6167995/generate-a-pdf-that-automatically-prints) – yms May 02 '13 at 15:41

1 Answers1

-1

Are you OK with assuming that whoever is viewing your file is using Acrobat Reader or some other "javascript-enabled" PDF reader?

If that is the case, you could then modify your file and generate a PDF that automatically prints instead.

Community
  • 1
  • 1
yms
  • 10,361
  • 3
  • 38
  • 68
  • The 2nd part of the accepted answer of that question is interesting to me - I think I'll try it out. I am using iText to create the PDF. In my case, it gets rendered as a byte stream and not saved (as far as I know) at all. I'm currently using the 1st approach of that answer - `writer.setOpenAction(action);` Might be more reliable to use the 2nd approach. – Zack Macomber Apr 23 '12 at 17:10