One query that turns up a lot in the various Javascript forums asks how to send the page directly to the printer without first displaying the print dialog box.

在各种Javascript论坛中,经常出现的一个查询询问如何将页面直接发送到打印机而不先显示打印对话框 。

Rather than just telling you that it can't be done perhaps an explanation of why such an option is not possible would be more useful.

与其仅告诉您无法完成操作,不如解释为什么无法执行该选项可能会更有用。

Which print dialog box displays when someone presses the print button in their browser or the Javascript window.print() method runs depends on the operating system and what printers are installed on the computer.

当某人在浏览器中或在Javascript window中按下打印按钮时 ,显示哪个打印对话框。print()方法的运行取决于操作系统以及计算机上安装了哪些打印机。

As most people run Windows on their computer, let's first describe how the printing setup works on that operating system. The *nix and Mac operating systems differ slightly in the details but overall are set up similar.

当大多数人在计算机上运行Windows时,让我们首先描述打印设置在该操作系统上的工作方式。 * nix和Mac操作系统在细节上略有不同,但总体设置类似。

( Print Dialog )

There are two parts to the print dialog box on Windows. The first of these is part of the Windows API (Application Programming Interface). The API is a set of common code pieces that are held in the various DLL (Dynamic Link Library) files that are part of the Windows operating system. Any Windows program can (and should) call the API to perform common functions such as displaying the Print Dialog box so that it will work the same way in all programs and not have different options in different places the way the print option did back in DOS program days. The Print Dialog API also provides a common interface allowing all programs access to the same set of printer drivers rather than printer manufacturers having to create driver software for their printer for each individual program that wanted to use it.

Windows上的“打印”对话框有两个部分。 其中的第一个是Windows API(应用程序编程接口)的一部分。 该API是一组通用代码段,它们保存在Windows 操作系统的各种DLL( 动态链接库 )文件中。 任何Windows程序都可以(并且应该)调用API来执行诸如显示“打印对话框”之类的通用功能,以便它在所有程序中都可以以相同的方式工作,并且在不同的位置上不具有与DOS中的打印选项相同的选项计划天数。 Print Dialog API还提供了一个通用接口,该接口允许所有程序访问同一组打印机驱动程序,而不是打印机制造商必须为要使用它的每个单独程序为其打印机创建驱动程序软件。

The printer drivers are the other half of the print dialog. There are several different languages that different printers understand that they use to control how the page prints (eg. PCL5 and Postscript). The printer driver instructs the Print API as to how to translate the standard internal print format that the operating system understands into the custom markup language that the specific printer understands. It also adjusts the options that the Print dialog displays to reflect the options offered by the specific printer.

打印机驱动程序是打印对话框的另一半。 不同的打印机可以使用几种不同的语言来控制页面的打印方式(例如PCL5和Postscript)。 打印机驱动程序指示Print API关于如何将操作系统可以理解的标准内部打印格式转换为特定打印机可以理解的自定义标记语言。 它还会调整“打印”对话框显示的选项,以反映特定打印机提供的选项。

( Operating the Printer )

An individual computer may have no printers installed, it may have one local printer, it may have access to several printers over a network, it may even be set up to print to PDF or preformatted print file. Where more than one "printer" is defined one of them is designated the default printer which means that it is the one that displays its details in the print dialog when it first appears.

一台个人计算机可能没有安装打印机,可能有一台本地打印机,可能通过网络访问了几台打印机,甚至可以设置为打印为PDF或预格式化的打印文件。 在定义了多个“打印机”的情况下,其中一个被指定为默认打印机,这意味着它是首次出现时在打印对话框中显示其详细信息的打印机。

The operating system keeps track of the default printer and identifies that printer to the various programs on the computer. This allows the programs to pass an extra parameter to the print API telling it to print directly to the default printer without displaying the print dialog first. Many programs have two different print options - a menu entry that displays the print dialog and a toolbar fast print button that sends directly to the default printer.

操作系统会跟踪默认打印机,并根据计算机上的各种程序识别该打印机。 这使程序可以向打印API传递一个额外的参数,告诉它直接打印到默认打印机,而无需先显示打印对话框。 许多程序具有两个不同的打印选项-显示打印对话框的菜单项和直接发送到默认打印机的工具栏快速打印按钮。

When you have a web page on the internet that your visitors are going to print, you have next to no information about what printer(s) they have available. Most printers around the world are configured to print out on A4 paper but you can't guarantee that the printer is set up to that default. One North American country uses a non-standard paper size that is shorter and wider than A4. Most printers are set up to print out in portrait mode (where the narrower direction is the width but some may be set to the landscape where the longer dimension is the width. Of course, each and every printer also has different default margins at the top, bottom, and sides of the page even before the owners go in and change all of the settings to get the printer the way that they want.

当您的访客在互联网上有一个要打印的网页时,您几乎没有关于他们可以使用的打印机的信息。 世界上大多数打印机都配置为在A4纸上打印,但是您不能保证打印机设置为该默认值。 一个北美国家/地区使用的非标准纸张尺寸比A4短和宽。 大多数打印机设置为以纵向模式打印(其中较窄的方向是宽度,但有些打印机可能设置为横向,而较长的尺寸是宽度。当然,每台打印机的顶部都有不同的默认边距) ,页面底部和页面两侧,甚至在所有者进入并更改所有设置以使打印机以他们想要的方式进入之前。

Given all of these factors, you have no way to tell whether the default printer with its default configuration will print out your web page on A3 with negligible margins or on A5 with huge margins (leaving little more than a postage stamp sized area in the middle of the page). You can probably assume that most will have a print area on the page of approximately 16cm x 25cm (plus or minus 80%).

考虑到所有这些因素,您无法确定具有默认配置的默认打印机是在A3上以可忽略的边距打印页面还是在A5上以巨大的边距打印页面(中间仅剩邮票大小的区域)页面的内容)。 您可能假设大多数页面上的打印区域约为16cm x 25cm(正负80%)。

( Printing Needs )

Since printers vary so much between your potential visitors (did someone mention laser printers, inkjet printers, color or black and white only, photo quality, draft mode, and lots more) you have no way to tell what they will need to do to print out your page in a reasonable format. perhaps they have a separate printer or a second driver for the same printer providing totally different settings specifically for web pages.

由于潜在访问者之间的打印机差异很大(有人提到激光打印机,喷墨打印机,仅彩色或黑白,照片质量,草稿模式等等),所以您无法说出他们需要做什么来进行打印以合理的格式显示您的页面。 也许他们有单独的打印机或同一台打印机的第二个驱动程序,专门为网页提供完全不同的设置。

Next, comes the matter of what they may want to print. Do they want the whole page or have they selected just a part of the page that they want to print? If your site uses frames do they want to print all of the frames the way they appear on the page, do they want to print each frame separately, or do they just want to print a specific frame?

接下来是他们可能要打印的内容。 他们需要整个页面还是只选择了要打印的页面的一部分? 如果您的网站使用框架,他们是否要按照页面上显示的方式打印所有框架,是要分别打印每个框架,还是只想打印特定的框架?

The need to answer all of these questions makes it just about essential that the print dialog appears whenever they want to print something so that they can make sure that the settings are all correct before they hit the print button. Most browsers also provide the ability to add a "fast print" button to one of the browser toolbars to allow the page to be printed to the default printer using default browser settings as to what is to be printed and how.

由于需要回答所有这些问题,因此几乎必须要在他们想要打印任何内容时出现打印对话框,以便他们可以确保在按下打印按钮之前所有设置都是正确的。 大多数浏览器还提供了向浏览器工具栏之一添加“快速打印”按钮的功能,以允许使用关于要打印的内容和方式的默认浏览器设置将页面打印到默认打印机。

( Javascript )

Browsers do not make this multitude of browser and printer settings available to Javascript. Javascript is primarily concerned with modifying the current web page and so web browsers provide minimal information about the browser itself and next to no information about the operating system available to Javascript because Javascript doesn't need to know those things to perform those things that Javascript is intended to do.

浏览器不会将大量浏览器和打印机设置提供给Javascript。 Javascript主要与修改当前网页有关,因此Web浏览器仅提供有关浏览器本身的最少信息,并且几乎不提供有关Javascript可用的操作系统的信息,因为Javascript不需要知道那些事情即可执行Javascript要做的那些事情。打算做。

Basic security says that if something like Javascript doesn't need to know about the operating system and browser configuration in order to manipulate the web page then it should not be provided with that information. It is not like Javascript ought to be able to change the printer settings to appropriate values for printing the current page because that is not what Javascript is for - that is the job of the print dialog. Browsers therefore only make available to Javascript those things that Javascript needs to know such as the size of the screen, the available space in the browser window to display the page, and similar things which help Javascript work out how the page is laid out. The current web page is Javascript's one and only concern.

基本安全性表示,如果像Javascript之类的东西不需要了解操作系统和浏览器配置来操纵网页,则不应向其提供该信息。 Java不是应该能够将打印机设置更改为适当的值以打印当前页面,因为那不是Javascript的目的-那是打印对话框的工作。 因此,浏览器仅向Javascript提供Javascript需要知道的那些东西,例如屏幕的大小, 浏览器窗口中用于显示页面的可用空间以及类似的东西,这些东西可以帮助Javascript确定页面的布局方式。 当前网页是Javascript唯一的关注点。

( Intranets )

Intranets are of course an entirely different matter. With an intranet, you know that everyone accessing the page is using a specific browser (usually a recent version of Internet Explorer) and has a specific screen resolution and access to specific printers. This means that it makes sense on an intranet to be able to print directly to the printer without displaying the print dialog because the person writing the web page knows which printer it will be printed on.

内部网当然是完全不同的事情。 使用Intranet,您知道访问该页面的每个人都在使用特定的浏览器(通常是Internet Explorer的最新版本),并且具有特定的屏幕分辨率和对特定打印机的访问权限。 这意味着在Intranet上能够直接打印到打印机而不显示打印对话框是有意义的,因为编写网页的人知道将在哪台打印机上打印。

The Internet Explorer substitute for Javascript (called JScript) therefore has slightly more information about the browser and operating system that Javascript itself does. The individual computers on the network running the intranet may be able to be configured to allow the JScript window.print() command write directly to the printer without displaying the print dialog. This configuration would need to be set up individually on each client computer and is well beyond the scope of an article on Javascript.

因此,Internet Explorer可以代替Javascript(称为JScript)来获得有关Javascript本身所具有的浏览器和操作系统的更多信息。 运行Intranet的网络上的各个计算机可以配置为允许JScript window.print()命令直接写入打印机,而不显示打印对话框。 此配置将需要在每台客户端计算机上单独设置,并且已经超出了Javascript文章的范围。

When it comes to web pages on the internet there is absolutely no way that you can set up a Javascript command to send directly to the default printer. If your visitors wish to do that they will have to set up their own "fast print" button on their browser toolbar.

当涉及到Internet上的网页时,绝对不可能设置Javascript命令直接发送到默认打印机。 如果您的访客希望这样做,则必须在浏览器工具栏上设置自己的“快速打印”按钮。

翻译自: https://www.thoughtco.com/print-directly-to-printer-2037449