使用IE 11即时生成的PDF打印Iframe [英] Print Iframe with PDF generated on the fly IE 11

查看:497
本文介绍了使用IE 11即时生成的PDF打印Iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印在iframe中即时生成的pdf,但无法打印该pdf文件.这就是我现在所拥有的,我做错什么了吗?在Google Chrome上运行良好,但在IE 11上却无法正常运行.请帮助我完成这项工作.预先感谢

I wanted to print a pdf which is generated on the fly in an Iframe, but can't get that print this pdf file. This is what i have now, am i doing something wrong?. Works fine on Google Chrome but not on IE 11. Please help me to get this work. Thanks in advance

这是HTML标记

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

这是我的JavaScript,其中我为iframme的src分配了动态URL(此URL生成pdf)

Here is my javascript where i assign the src of the iframme with the dynamic URL (this url generates the 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;

最后是我尝试打印的地方

And finally here is where i try to print

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

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

On IE 11 this send me the error "Invalid calling object"

我也尝试了这个但没有成功:

I tried this too without success:

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

推荐答案

我的解决方法是先生成pdf(保存到磁盘),然后使用生成的url并将其放入<embed>对象(使用此方法,能够使用IE的打印方法)

My workaround was generate the pdf first (save into disk) and then use the url of the generated and place it into an <embed> object (with this you are able to use the print method from 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(可行)

For other browsers like Chrome you can use the same approach (save file into disk) and use iframe (this works)

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';

在打印"按钮中,您可以将其放置:

In the print button you can place this:

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

希望这对其他人有帮助

这篇关于使用IE 11即时生成的PDF打印Iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆