Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: DHTML
Expertise: Beginner
Aug 20, 1998

Printing from Browser

Question:
Is there a way to print an HTML page (perhaps using VBScript or JavaScript) without the user clicking on the print button?

Answer:
Ah, printing! You would think that, given its roots in a text based mark-up language that HTML would be superb for printing, right? Not a chance. The issue of printing has bedeviled both browser makers and Web developers for quite some time, and the answers are simply not as acceptable as anyone would like.

Let's get the easy question out of the way first. From within a traditional browser, there is no way to print a Web page from within a document using simply JavaScript or DHTML. The reason for this should be fairly obvious. If I were a prankster, I could tell your printer to print a thousand-page document filled with my latest conspiracy theory. Technically, it's not hard to do, but from a security standpoint, it's an issue that most people would just as soon avoid.

However, there are just as obviously places where it would be useful to force a print. While you can't print from within DHTML, there's no reason why you can't do it from outside. There are two situations here where you may want to print--you have a browser window with all the menus and command bars turned off, or you have an embedded browser. The solution I'm going to discuss here for both cases is Internet Explorer-centric, although you can create an analogous solution using certain Java classes (specifically, the PrintJob class).

In the case of an embedded browser in another application (for example, if you're creating a custom browser with appropriate branding for your product), you'll be using the WebBrowser control (SHDOCVW.DLL). This control is essentially the browsing engine for Internet Explorer--everything else is simply a shell around it that handles administrative functions. One of the methods that the WebBrowser exposes is ExecWB() (WB for Web Browser, I would expect). This is an entry point for passing OLE commands into the browser object itself, allowing you to save, print, or otherwise manipulate the browser's contents in a surprisingly extensive number of ways. The command takes four arguments:

WebBrowser-->ExecWB(cmdID,cmdexecopt,[pvaIn],[pvaOut])
with the following meanings:

cmdID (Command ID): a constant that contains the action to perform (printing is OLECMDID_PRINT).

cmdexexopt (Command Execute Option): a constant that tells whether to prompt the user with a dialog box.

pvaIn (pointer to an Inbound variant variable): a variable that contains information for the command, such as file name for OLECMDID_OPEN.

pvaOut (pointer for an Outbound variant variable): a variable that will be populated with information when the ExecWB method is called.

With the ExecWB() method, printing becomes trivial. If you want to display the print dialog box (the recommended procedure) before printing, you'd call the following in C++ (assuming you've already instantiated the WebBrowser ActiveX object):

Webbrowser-->ExecWB(OLECMDID_PRINT,OLECMDEXECOPT_PROMPTUSER)
Webbrowser-->ExecWB(OLECMDID_PRINT,OLECMDEXECOPT_DONTPROMPTUSER)
The same thing can be done with Visual Basic by replacing the "-->" method identifiers with the dot notation.

These calls can be incorporated into an ActiveX component that resides in the page. A User Control's ParentControl array can be used to reference the Web browser object, or a Printer object could be created inside the component to print specialized data that may not mirror the Web page.

I have two final notes about printing, alluded to in the C programming question. At one time, when HTML was in its infancy, printing HTML was a fairly simple task because the language was a markup language. However, in today's Applet and DHTML enabled world, trying to manually create a Web page output to the printer becomes even more complex than writing a browser by hand that would handle all of the parsing. Let the browser objects handle it.

Also, the CSS2 specifications, recently approved but not yet implemented in browsers, contain considerably richer support for printing output to a wide number of devices, including standard printers, Braille printers, aural readers, and television. The next generation of Web browsers may also include Printer objects in the object model so that they can be invoked from code--as mentioned before, the hang-up here is providing a secure way of doing it, not performing the technical process.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap