javascript 使用javascript在IFrame中打印PDF文件仅获取一页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18888131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:34:34  来源:igfitidea点击:

Print PDF File in IFrame using javascript getting one page only

javascriptinternet-explorerpdf

提问by Vijay P.V

here is my code to print a pdf file. here while printing time iam getting one page only i need a solution for that

这是我打印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();
           }
       }
 }

采纳答案by noseratio

I suspect that's because the whole window gets printed (which has the current view of the iframewith the 1st page of the PDF rendered). Use <object>instead:

我怀疑这是因为整个窗口都被打印出来了(它的当前视图是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>

This code is verified with IE. Other browsers will still render the PDF, but may not print it.

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

[UPDATE]If you need dynamic loading and printing, the changes to the above code are minimal:

[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>

回答by paulo henrique

<iframe src="teste.pdf" id="meupdf" width="800" height="600" />

function printPdf) {

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