简体   繁体   English

可靠地渲染PDF,当加载到网页上时可以打开打印对话框

[英]Reliably render a PDF which opens a print dialog when loaded on a web page

I have a web page (SSL) that contains a barely visible iframe whose source is a PDF streamed from a Java servlet. 我有一个网页(SSL),其中包含几乎看不见的iframe,其来源是从Java servlet流式传输的PDF。 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. 几乎看不见的iframe的目的是渲染PDF,该PDF在加载页面后立即打开打印对话框。

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. 通常,在加载页面时,会正确显示“ PDF打印”对话框。 Occasionally, though, the PDF print dialog box is never rendered when the page loads. 但是,有时在页面加载时永远不会渲染PDF打印对话框。 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. 我知道这不是Java servlet中的问题,因为我可以从日志中验证PDF是否正确传输到调用页面。 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. 我需要找到一种方法来确保99.9999 .......%的可靠性,如果可能的话,在加载页面时始终打开PDF打印对话框。 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). 这需要适用于Internet Explorer(尤其是版本8)。

This is the JavaScript that opens the page that contains the PDF iframe: 这是JavaScript,可打开包含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: 这是包含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>

Are you OK with assuming that whoever is viewing your file is using Acrobat Reader or some other "javascript-enabled" PDF reader? 您是否可以确定正在查看文件的人员正在使用Acrobat Reader或其他“启用JavaScript的” PDF阅读器?

If that is the case, you could then modify your file and generate a PDF that automatically prints instead. 在这种情况下,您可以修改文件并生成自动打印的PDF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在打印对话框中的页面时数据未加载到表中 - why the data not loaded in the table when the page in the print dialog 如何增加打开打印对话框直到页面或PDF完全加载的延迟:JS - How to add delay in opening Print Dialog untill page or PDF fully loaded:JS 打印网页而不显示打印对话框和此网页 - print web page without show the print dialog and this web page 模态对话框,使用jQuery打开新页面 - Modal dialog box which opens new page using jQuery jQuery对话框打开时调整页面大小会导致页面上的滚动条 - Resizing of page when jquery dialog opens causes scrollbars on the page 加载页面时打开全屏的javascript函数 - javascript function that opens to full screen when page is loaded 当页面使用javascript和CSS加载时,Div垂直打开 - Div opens vertically when page loaded with javascript and css window.print()在Chromium v​​25中重新加载页面时打开打印对话框 - window.print() opens print dialog just on page reload in Chromium v25 在页面重新加载时,“window.print()”不会在 Google Chrome 77.0.3865.120 中加载打印对话框。 当 IFRAME 与 PDF 作为 SRC 一起使用时,会发生这种情况 - On page reload, "window.print()" does not load the print dialog in Google Chrome 77.0.3865.120. This happens when an IFRAME is used with a PDF as SRC 如何从网页打印PDF - How to print a PDF from a web page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM