Monday, October 21, 2019
Print Directly to Printer With JavaScript
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.