【问题标题】:Print PDF File in IFrame using javascript getting one page only使用javascript在IFrame中打印PDF文件只获取一页
【发布时间】:2013-09-24 03:37:56
【问题描述】:

这是我打印 pdf 文件的代码。在这里,打印时间我只得到一页,我需要一个解决方案

  function printPdf(){
     var ifr = document.getElementById("frame1");
         //PDF is completely loaded. (.load() wasn't working properly with PDFs)
     ifr.onreadystatechange = function () {
        if (ifr.readyState == 'complete') {
              ifr.contentWindow.focus();
              ifr.contentWindow.print();
           }
       }
 }

【问题讨论】:

标签: javascript internet-explorer pdf


【解决方案1】:

我怀疑这是因为整个窗口都被打印了(它具有iframe 的当前视图,并呈现了 PDF 的第一页)。请改用<object>

<!DOCTYPE html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>

    <script>
    function PrintPdf() {
        idPrint.disabled = 0;
        idPdf.Print();
    }

    function idPdf_onreadystatechange() {
        if (idPdf.readyState === 4)
            setTimeout(PrintPdf, 1000);
    }
    </script>

</head>

<body>
    <button id="idPrint" disabled=1 onclick="PrintPdf()">Print</button>
    <br>
    <object id="idPdf" onreadystatechange="idPdf_onreadystatechange()"
        width="300" height="400" type="application/pdf"
        data="test.pdf?#view=Fit&scrollbar=0&toolbar=0&navpanes=0">
        <span>PDF plugin is not available.</span>
    </object>
</body>

此代码已通过 IE 验证。其他浏览器仍会呈现 PDF,但可能无法打印。

[UPDATE]如果需要动态加载和打印,上面代码的改动很小:

<!DOCTYPE html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>

    <script>
    function PrintPdf() {
        idPdf.Print();
    }

    function idPdf_onreadystatechange() {
        if (idPdf.readyState === 4)
            setTimeout(PrintPdf, 1000);
    }

    function LoadAndPrint(url)
    {
        idContainer.innerHTML = 
            '<object id="idPdf" onreadystatechange="idPdf_onreadystatechange()"'+
                'width="300" height="400" type="application/pdf"' +
                'data="' + url + '?#view=Fit&scrollbar=0&toolbar=0&navpanes=0">' +
                '<span>PDF plugin is not available.</span>'+
            '</object>';
    }
    </script>
</head>

<body>
    <button id="idPrint" onclick="LoadAndPrint('http://localhost/example.pdf')">Load and Print</button>
    <br>
    <div id="idContainer"></div>
</body>

【讨论】:

  • 感谢您的回复。实际上,在我使用对象之前,我更改为 iframe,因为也许 IE9 会产生使用对象的问题,您可以尝试使用 iframe..
  • 我认为这不可能。您可以打印的最好的是整个窗口,而不是特定的&lt;iframe&gt;,当它里面有 PDF 时。 &lt;object&gt; 方法虽然适用于 IE9。也许您应该确保 Acrobat Reader 是最新的。
  • 但它对我不起作用..我想在加载对象后打印
  • 一些想法:尝试idPdf.gotoLastPageidPdf.printAll(),或增加超时时间:setTimeout(PrintPdf, 2000)(这是在文件加载后呈现初始 PDF 视图的延迟)。这是一个related post,它列出了 PDF &lt;object&gt; 上可用的所有方法。
  • 没问题。上面哪一个选项确实有帮助?
【解决方案2】:
<iframe src="teste.pdf" id="meupdf" width="800" height="600" />

function printPdf) {

    var PDF = document.getElementById("meupdf");
    PDF.focus();
    PDF.contentWindow.print();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    相关资源
    最近更新 更多