2016-04-24 37 views
0

我想在html报告页面中执行打印操作。我只需要点击“导出PDF”按钮中的PDF页面的某些部分。 因此,我将所需内容放置到一个新的iframe中,并在单击按钮时打印iframe。其工作完美。打印iframe内容jquery不能在远程服务器上工作

这里是我的代码

$('#btnprint').on('click',function(){ 
 
     $('#print-iframe').contents().find('html').html($('#divprintheader').html() + $('#divreport').html()); 
 

 
    $("#print-iframe").get(0).contentWindow.print(); 
 
});
<table id="tblentrancereport" class="reporttable" style="width: 100%"> 
 
           <thead> 
 
           <tr> 
 
            <th>Name</th> 
 
            <th>Count</th> 
 
            <th>Amount</th> 
 
           </tr> 
 

 
           </thead> 
 
           <tbody></tbody> 
 
          </table> 
 
<div id="divprintheader" style="display: none"> 
 

 
        From : <span id="spnfromdate"></span> &nbsp;&nbsp;&nbsp;&nbsp; To : <span id="spntodate"></span> 
 
        <div id="divprintfilter"> 
 
         Filtered By : <span id="spnfilteredby"></span> 
 
         <div id="divcontentbelongto"> 
 

 
         </div> 
 
        </div> 
 

 
       </div> 
 

 
<button id="btnprint">Export PDF</button> 
 
       <iframe name="print_frame" id="print-iframe" width="0" height="0" frameborder="0" src="about:blank"></iframe>

不幸的是,当我的代码移动到我的生产服务器(Windows Server 2012),我不是在iframe中获取内容(打印窗口是未来)。尽管iframe中填充了新的动态构成内容(我可以在检查页面元素时看到),但它并未显示在打印内容中。任何人都可以弄清楚我错误的地方。

下面是截图

Image 1当我在我的本地

Image 2打印试过当我在我的服务器尝试打印

UPDATE 而奇怪的是,当我检查元素并扩展开发人员工具中的内容,然后单击“导出PDF”按钮,其工作..:O

不能描绘出错误:(

+1

也许试着用'sandbox =“allow-forms allow-modals allow-popups allow-same-origin allow-scripts来移除'src'属性'' –

+0

@MichaelSchwartz谢谢你的回复!但不工作.. :( – NCA

+0

而奇怪的是,当我检查元素和展开内容的开发工具,然后点击“导出PDF”按钮,其工作。 无法找出错误:( – NCA

回答

0

<head> 
 
<script type="text/javascript"> 
 
function ClickHereToPrint(){ 
 
    try{ 
 
     var oIframe = document.getElementById('ifrmPrint'); 
 
     var oContent = document.getElementById('divToPrint').innerHTML; 
 
     var oDoc = (oIframe.contentWindow || oIframe.contentDocument); 
 
     if (oDoc.document) oDoc = oDoc.document; 
 
\t \t oDoc.write("<html><head><title>title</title>"); 
 
\t \t oDoc.write("</head><body onload='this.focus(); this.print();'>"); 
 
\t \t oDoc.write(oContent + "</body></html>"); \t  
 
\t \t oDoc.close(); \t  
 
    } 
 
    catch(e){ 
 
\t  self.print(); 
 
    } 
 
} 
 
</script> 
 
</head> 
 

 

 
<body> 
 

 
<a onclick="ClickHereToPrint();">Print</a> 
 

 
\t <iframe id='ifrmPrint' src='#' style="width:0px; height:0px;"></iframe> 
 
\t <div id="divToPrint"> 
 
\t \t content 
 
\t </div> 
 
</body>

虽然我无法弄清楚这个问题,该解决方案可以完美运行。谢谢 !!

相关问题