Monday, December 30, 2019

Print Directly to Printer With JavaScript

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. Rather than just telling you that it cant 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. As most people run Windows on their computer, lets 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. 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. 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. 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. 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. 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 cant 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. 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%). 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 doesnt need to know those things to perform  those  things that Javascript is intended to do. Basic security says that if something like Javascript doesnt 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  Javascripts  one and only concern. 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. 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. 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.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.