【问题标题】:Print pdf in hidden iframe javascript/jquery C# MVC在隐藏的 iframe javascript/jquery C# MVC 中打印 pdf
【发布时间】:2015-07-21 07:17:07
【问题描述】:

项目要求:- 使用 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() 处给出以下错误;命令作为调用对象无效

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

【问题讨论】:

  • 您在寻找link吗?

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


【解决方案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();  
        });

【讨论】:

  • 为了在打印之前等待,请尝试为 Popup 淡出。
  • 我没有打印普通的 HTML 内容,我正在打印 PDF 文件并且不希望用户看到它。您建议的代码是打印 HTML 内容。
猜你喜欢
  • 2010-11-18
  • 2013-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-19
  • 2023-03-06
  • 2023-03-09
  • 1970-01-01
相关资源
最近更新 更多