简体   繁体   English

使用iframe在Javascript中打印PDF文件

[英]Print PDF File in Javascript using Iframe

here is my code to print a pdf file in JavaScript using iframe. 这是我的代码,用于使用iframe在JavaScript中打印pdf文件。 but i cant able to print nothing is happening , but this code is perfectly working in google chrome. 但是我无法打印任何内容,但是此代码在谷歌chrome浏览器中运行正常。 in IE8 not working. 在IE8中无法正常运作。 ContentLoaderDiv is html division. ContentLoaderDiv是html部门。 please help me.. 请帮我..

When I print a PDF document from our application which uses Java script to print instead of adobe Reader print dialog the browser print dialog is invoked. 当我从使用Java脚本而不是Adobe Reader打印对话框进行打印的应用程序中打印PDF文档时,将调用浏览器打印对话框。

Have anyone seen this problem? 有没有人看过这个问题? How to invoke the Adobe reader printer dialog instead of browser print dialog? 如何调用Adobe Reader打印机对话框而不是浏览器打印对话框?

 function printPdf() {
        var ContentLoaderDiv = document.getElementById('ContentLoaderDiv');
        ContentLoaderDiv.innerHTML = "";
        ContentLoaderDiv.innerHTML = '<div id="pdfdiv" style="position: relative;"><iframe id="frame1"  height="800" width="700" src="' + document.getElementById("<%= hdnPDFPathForObject.ClientID  %>").value + "print.pdf#scrollbar=1&toolbar=1&statusbar=0&messages=0&navpanes=1" + '"' + " /></iframe></div>";
        frame1.focus();
        frame1.print(); 

} }

Try this one: 试试这个:

function PdfUtil(url) {

    var iframe;

    var __construct = function(url) {
        iframe = getContentIframe(url);
    }

    var getContentIframe = function(url) {
        var iframe = document.createElement('iframe');
        iframe.src = url;
        return iframe;
    }

    this.display = function(parentDomElement) {
        parentDomElement.appendChild(iframe);
    }

    this.print = function() {
        try {
            iframe.contentWindow.print();
        } catch(e) {
            throw new Error("Printing failed.");
        }
    }

    __construct(url);
}

And you can use it as follows: 您可以按以下方式使用它:

var pdf = new PdfUtil(PDF_URL);
pdf.display(document.getElementById('placeholder'));

document.getElementById('printBtn').onclick = function() {
    pdf.print();
}

Obviously, though PDF_URL here is a "constant" , in your case it should most probably be generated. 显然,尽管这里的PDF_URL“常量” ,但在您的情况下,很可能应该生成它。

Works perfectly with IE8 and Chrome (should work also with other browsers). 与IE8和Chrome完美兼容(也应与其他浏览器一起使用)。 Also, the OO approach makes it far more maintainable and/or reusable. 同样,OO方法使它更易于维护和/或重用。 Slight modifications to the actual logic might still be required to fit all your needs. 为了满足您的所有需求,可能仍需要对实际逻辑进行一些修改。

I have struggled a bit to find a solution that works for both IE and Chrome. 我一直在努力寻找一种适用于IE和Chrome的解决方案。 This works for me: 这对我有用:

$(function() {
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf('MSIE ');
    var trident = ua.indexOf('Trident/');
    var edge = ua.indexOf('Edge/');
    var url = '/url/to/file.pdf';
    var pdf ='';
    var style = 'position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden;';

    if(msie > 0 || trident > 0 || edge > 0){
        pdf = '<object data="' + url + '" name="print_frame" id="print_frame" style="' + style + '" type="application/pdf">';
    }
    else{
        pdf ='<iframe src="' + url + '" name="print_frame" id="print_frame" style="' + style + '"></iframe>';
    }

    $(document.body).append(pdf);

    setTimeout(function(){
        window.frames["print_frame"].focus();
        window.frames["print_frame"].print();
    },2000);
});

...cheers. ...干杯。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM