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



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

这是HTML标记

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

这是我的javascript,我用动态URL分配iframme的src(这个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();

希望这能帮助其他人

最新更新