javascript - 使用 IE 11 即时生成的 PDF 打印 Iframe

标签 javascript internet-explorer pdf iframe

我想打印一个在 Iframe 中即时生成的 pdf,但无法打印此 pdf 文件。这就是我现在所拥有的,我做错了什么吗?在 Google Chrome 上运行良好,但在 IE 11 上运行不正常。请帮助我完成这项工作。提前致谢

这是 HTML 标记

<div id="contractFrameContainer" class="modal-body row">        
  <iframe id="contractIframe" name="contractIframe" width="100%" src=""></iframe>
</div>

这是我的 javascript,我将 iframme 的 src 分配给动态 URL(此 url 生成 pdf)

var url = currentUrl + '/contracts/createpdf?ProductId=' + Id + '&type='
                                    + Type + '&term=' + Term
                                    + '&deductible=' + Deductible
                                    + '&key=' + OrderNumber
                                    + '&financedAmount=' + financedAmount
                                    + '&downpayment=' + downpayment
                                    + '&apr=' + apr
                                    + '&tire=' + tireRotation
                                    + '&interval=' + interval
                                    + '&salesPrice=' + salesPrice
                                    + '&dealerCost=' + DealerCost
                                    + '&mileage=' + Mileage 
                                    + '&serviceDate=' + serviceDate
                                    + '&penSurcharges=' + penSurcharges
                                    + '&price=' + Price;

        var iframe = document.getElementById("contractIframe");
        iframe.height = heightBody;        
        iframe.src = url;

最后这是我尝试打印的地方

document.getElementById('contractIframe').focus();
document.getElementById('contractIframe').contentWindow.print();

在 IE 11 上,这会向我发送错误“无效的调用对象”

我也试过,但没有成功:

window.frames["contractIframe"].focus();
window.frames["contractIframe"].print();

最佳答案

我的解决方法是先生成 pdf(保存到磁盘中),然后使用生成的 url 并将其放入 <embed> 中对象(有了这个你就可以使用 IE 的打印方法)

var ua = window.navigator.userAgent,
        msie = ua.indexOf("MSIE "),
        obj = null;

// Generate the embed object for IE
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
        obj = document.createElement('embed');
        obj.setAttribute("type", "application/pdf");
        obj.setAttribute("id", "pdf1");
        obj.style.width = '100%';
        obj.height = heightBody + 'px';
        obj.setAttribute("src", url); // here set the url for generated pdf file
}

// Place this where you print the embed (button, link...)
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
  document.getElementById('pdf1').print();
}

对于 Chrome 等其他浏览器,您可以使用相同的方法(将文件保存到磁盘中)并使用 iframe(这可行)

obj = document.createElement('iframe');
obj.setAttribute("type", "application/pdf");
obj.setAttribute('id', 'pdf2');
obj.setAttribute('src', url);
obj.style.width = '100%';
obj.height = heightBody + 'px';

在打印按钮中你可以放置这个:

document.getElementById('pdf2').contentWindow.print();

希望这对其他人有帮助

关于javascript - 使用 IE 11 即时生成的 PDF 打印 Iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26147064/

相关文章:

javascript - window.print() 在 IE 中不工作

r - 从 PDF 的特定位置提取数据?

javascript - 打印/生成嵌入式 power bi 报告的 PDF

javascript - 循环在 Internet Explorer 中不起作用

javascript - 如何让 SVG 与 IE 兼容?

c# - 使用 iTextSharp 填写 PDF 表格并通过电子邮件发送填写好的表格

html - xhtml2pdf 背景图像未缩放

Javascript if else语句优化

javascript - Redux:这种全能 Action 创建者模式是不是错了?

javascript - "typeError: Cannot read property ' 用户名 ' of undefined."