2014-10-01 120 views
3

我想打印在Iframe中即时生成的PDF,但无法打印此PDF文件。这就是我现在所拥有的,我做错了什么?在Google Chrome上运行良好,但在IE 11上运行得不错。请帮助我完成这项工作。在此先感谢打印使用PDF生成PDF的iframe 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上,这给我发电子邮件RROR“无效调用对象”

我想这也没有成功:

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

有什么解决办法吗? – SwR 2016-04-27 10:14:13

+0

看看我的解决方案:) – martinezjc 2016-04-27 14:27:54

回答

0

我的解决方法是首先生成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(); 

希望这有助于别人