2015-07-21 29 views
1

项目需求: - 使用iTextSharp动态生成pdf文件。 授予用户选项以打印该文件,并在用户尝试打印时不保存文件给用户而保存记录。在隐藏iframe中打印pdf javascript/jquery C#MVC

经过大量的试验和错误和研究使用下面的代码进行打印操作。请帮忙!

我想打印在iframe中呈现的哪个id隐藏,因为不想让用户看到该文件。 下面是我的jQuery代码: -

$('.btnPrintForm').click(function() { 
      if (confirm('Quetion to ask whether to go ahead with printing or  not?')) { 
       $("#waitPrintDiv").show(); 
       var PDF = document.getElementById('iPrintform'); 
       console.log(PDF); 
       PDF.focus(); 
       PDF.contentWindow.print(); 

       PDF.contentWindow.onafterprint = function() { 
        console.log("Printing completed..."); 
        alert("Printing completed..."); 
       } 
       window.location = 'PrintAcceptanceURL'; 
      } 
      else { 

       window.location = 'PrintRejecetionURL'; 
      } 
     }); 

在其所有的行为不同的浏览器。

1)CHRMOE: - 此代码在Chrome中工作正常,但打印弹出窗口出现时,它不会等待,它会重定向到第二个或第二个链接。所以我希望它等待用户给出打印命令或取消它,只有在它应该重定向到'PrintAcceptanceURL'之后。

2)Mozilla 此代码正在工作,但它会自动开始下载PDF文件,我不想发生?我该如何阻止它?

3)IE 10: - 在IE代码没有按预期工作。首先它提供选项查看/保存PDF文件选项,我不希望用户得到该选项。其次,它在PDF.contentWindow.print()中给出了以下错误。命令为无效的调用对象

请检查您是否可以以任何方式帮助我。

+0

你在找[link](http://stackoverflow.com/questions/16239513/print-pdf-directly-from-javascript)? –

回答

-1
$("#btnPrint").live("click", function() { 
      var divContents = $("#dvContainer").html(); 
      var printWindow = window.open('', '', 'height=400,width=800'); 
      printWindow.document.write('<html><head><title>DIV Contents</title>'); 
      printWindow.document.write('</head><body >'); 
      printWindow.document.write(divContents); 
      printWindow.document.write('</body></html>'); 
      printWindow.document.close(); 
      printWindow.print(); 
     }); 
+0

为了在打印之前等待弹出,尝试使用fadeOut。 –

+0

我不打印正常的HTML内容,我正在打印PDF文件并且不希望用户看到它。您建议的代码是打印HTML内容。 – DaxeshVora