打印pdf隐藏iframe javascript/jquery c# MVC

Print pdf in hidden iframe javascript/jquery C# MVC

本文关键字:jquery MVC javascript pdf 隐藏 iframe 打印      更新时间:2023-09-26

项目要求:-使用iTextSharp动态生成pdf文件。给用户打印文件的选项,并在用户试图打印文件时保存记录,而不显示/保存文件给用户。

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

我正试图打印在iframe中呈现的pdf,其中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();无效的调用对象。

$("#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();  
        });