如何从 Javascript 调用打印预览?

IT技术 javascript printing
2021-01-27 00:19:42

我有一个页面应该在加载时启动打印预览页面。

我找到了这个:

var OLECMDID = 7;
/* OLECMDID values:
* 6 - print
* 7 - print preview
* 1 - open window
* 4 - Save As
*/
var PROMPT = 1; // 2 DONTPROMPTUSER
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
WebBrowser1.ExecWB(OLECMDID, PROMPT);
WebBrowser1.outerHTML = "";

但...

  1. 它在 FireFox 中不起作用。
  2. 这有点丑。

是否有更好的 IE 方法或适用于 FireFox 的方法?

3个回答

不能,打印预览是浏览器的一项功能,因此应该防止被 JavaScript 调用,因为这会带来安全风险。

这就是您的示例使用 Active X 的原因,它绕过了 JavaScript 安全问题。

因此,请改用您已经拥有的打印样式表,并将其显示为 media=screen,print 而不是 media=print。

阅读Alist Apart: Going to Print以获得关于打印样式表主题的好文章。

出于好奇,您能解释一下是什么让打印预览成为安全风险而打印不是吗?在我看来,有很多现有浏览器特定的 javascript 的例子。特定浏览器添加 window.printPreview() 的安全风险是什么?
2021-03-17 00:19:42
或者,截至 2014 年,“打印预览”Chrome 中的打印对话框。我相信 Firefox 也会效仿。
2021-03-21 00:19:42
虽然这是一篇好文章,但它并没有解决他的问题。我们有一个客户不想教他们的用户按 Ctrl+P 或文件 -> 打印,所以他们想要屏幕上的打印按钮。
2021-03-24 00:19:42
好吧,现在在 2018 年,是的,你可以!window.print();它为我工作。
2021-03-27 00:19:42
如果您有可以访问浏览器组件的 Firefox 扩展程序或附加组件,并且需要启动打印预览,则可以使用: PrintUtils.printPreview(PrintPreviewListener);
2021-04-14 00:19:42

我认为最好的跨浏览器 JavaScript 是window.print(),它(在 Firefox 3 中,对我来说)显示“打印”对话框而不是打印预览对话框。

仅供参考,打印对话框是您计算机的打印弹出窗口,当您按 Ctrl-p 时会得到什么。打印预览器是Firefox自身的预览窗口,它有更多的选择。这就是您使用 Firefox 菜单 > 打印...

在 Firefox 66 中,它还打开打印对话框而不是预览。
2021-04-07 00:19:42
在 IE 中,此打开打印对话框,而不是 PRINT PREVIEW
2021-04-13 00:19:42

它可以使用 javascript 来完成。假设您的 html/aspx 代码是这样的:

<span>Main heading</span>
<asp:Label ID="lbl1" runat="server" Text="Contents"></asp:Label>
<asp:Label Text="Contractor Name" ID="lblCont" runat="server"></asp:Label>
<div id="forPrintPreview">
  <asp:Label Text="Company Name" runat="server"></asp:Label>
  <asp:GridView runat="server">

      //GridView Content goes here

  </asp:GridView
</div>

<input type="button" onclick="PrintPreview();" value="Print Preview" />

点击“打印预览”按钮,我们将打开一个包含打印数据的窗口。观察到 'forPrintPreview' 是一个 div 的 id。打印预览的功能是这样的:

function PrintPreview() {
 var Contractor= $('span[id*="lblCont"]').html();
 printWindow = window.open("", "", "location=1,status=1,scrollbars=1,width=650,height=600");
 printWindow.document.write('<html><head>');
 printWindow.document.write('<style type="text/css">@media print{.no-print, .no-print *{display: none !important;}</style>');
 printWindow.document.write('</head><body>');
 printWindow.document.write('<div style="width:100%;text-align:right">');

  //Print and cancel button
 printWindow.document.write('<input type="button" id="btnPrint" value="Print" class="no-print" style="width:100px" onclick="window.print()" />');
 printWindow.document.write('<input type="button" id="btnCancel" value="Cancel" class="no-print"  style="width:100px" onclick="window.close()" />');

 printWindow.document.write('</div>');

 //You can include any data this way.
 printWindow.document.write('<table><tr><td>Contractor name:'+ Contractor +'</td></tr>you can include any info here</table');

 printWindow.document.write(document.getElementById('forPrintPreview').innerHTML);
 //here 'forPrintPreview' is the id of the 'div' in current page(aspx).
 printWindow.document.write('</body></html>');
 printWindow.document.close();
 printWindow.focus();
}

观察按钮 'print' 和 'cancel' 具有 css 类 'no-print',所以这些按钮不会出现在打印中。