在隐藏的iframe javascript / jquery C#MVC中打印pdf

时间:2015-07-21 07:17:08

标签: javascript c#-4.0 iframe printing pdf-generation

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

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

我正在尝试打印在id中隐藏的iff中呈现的pdf,因为不希望用户看到该文件。 下面是我的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()中给出了以下错误;命令为无效的调用对象

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

1 个答案:

答案 0 :(得分:-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();  
        });