Use webbrowser to completely solve web printing problems

Source: Internet
Author: User
Use webbrowser to completely solve web printing problems (including background printing)

How do we solve the problem of printing under the BS architecture? I recently worked on a project to take charge of this part, not only requiring printing specific parts of the page, but also requesting batch printing in the background, I checked some information online and finally solved the problem. With the idea of "serving the public and serving the public", I summarized it and shared it with you online, hoping to help friends who encounter similar problems.

I mainly use the webbrowser control built in IE, which does not need to be downloaded or installed by users. Webbrowser has many functions, and other functions except printing will not be described in detail. Almost all of the printing functions you can use can be done by it. The following question is how to use it. First, display and print, and then back-end printing.

1. First introduce a webbrowser on the page to be printed, you can directly add:
 
<Object ID = "webbrowser" classid = CLSID: 8856f961-340a-11d0-a96b-00c04fd705a2 Height = "0" width = "0">
</Object>

To the page, or use JavaScript to temporarily add it as needed:

Document. Body. insertadjacenthtml ("beforeend ",

"<Object ID = \" webbrowser \ "width = 0 Height = 0 \

Classid = \ "CLSID: 8856f 961 -340a -11d0-a96b -00c 04fd 705a 2 \ "> ");

2. Page settings and print preview

Directly call

Document. All. webbrowser. execwb (6, 6) print directly
Document. All. webbrowser. execwb (8, 1) page settings
Document. All. webbrowser. execwb (7,1) print preview

Or:

ExecScript ("document. All. webbrowser. execwb 7, 1", "VBScript ");

3. Hide non-printable page elements and pages

CSS has a media attribute that allows you to set the print and display formats separately.

For example, <style media = "print" type = "text/CSS">... </Style> the format in the middle will only take effect during printing and will not affect the display interface.
So you can set
<Style media = "print" type = "text/CSS">
. Noprint {display: none ;}
. Pagenext {page-break-after: Always ;}
</Style>
Then, add "class =" noprint "to the page element that you do not want to print. This will not appear in print or print preview.
Add <Div class = "pagenext"> </div> to the page.

4. Print specific parts of the page

I create a page for a specific part to be printed, load it into an IFRAME on the home page, and then call the IFRAME printing method to print only the content in the IFRAME.
For example:
<IFRAME Style = "visibility: visible" name = "frameid" width = "100%" Height = "30%" src = "needprintedpage. asp"> </iframe>
The following pringframe JS function will print only the content in IFRAME and can be directly referenced for use, such as printframe (frameid );

Window. Print = printframe;

// Main stuff
Function printframe (frame, onfinish ){
If (! Frame) frame = window;

Function execonfinish (){
Switch (typeof (onfinish )){
Case "string": execScript (onfinish); break;
Case "function": onfinish ();
}
If (focused &&! Focused. Disabled) focused. Focus ();
}

If (frame.doc ument. readystate! = "Complete ")&&(! Frame.doc ument. Confirm ("the document to print is not downloaded yet! Continue with printing? ")))
{
Execonfinish ();
Return;
}

 
VaR eventscope = printgeteventscope (FRAME );
VaR focused = Document. activeelement;
 
Window. printhelper = function (){
ExecScript ("On Error resume next: printwb. execwb 6, 1", "VBScript ");
Printfireevent (frame, eventscope, "onafterprint ");
Printwb. outerhtml = "";
Execonfinish ();
Window. printhelper = NULL;
}
 
Document. Body. insertadjacenthtml ("beforeend ",
"<Object ID = \" printwb \ "width = 0 Height = 0 \
Classid = \ "CLSID: 8856f961-340a-11d0-a96b-00c04fd705a2 \"> ");
 
Printfireevent (frame, eventscope, "onbeforeprint ");
Frame. Focus ();
Window. printhelper = printhelper;
SetTimeout ("window. printhelper ()", 0 );
}

// Helpers
Function printisnativesupport (){
VaR agent = Window. Navigator. useragent;
VaR I = agent. indexof ("MSIE") + 5;
Return parseint (agent. substr (I)> = 5 & agent. indexof ("5.0b1") <0;
}

Function printfireevent (frame, OBJ, name ){
VaR handler = OBJ [name];
Switch (typeof (handler )){
Case "string": frame.exe cscript (handler); break;
Case "function": handler ();
}
}

Function printgeteventscope (FRAME ){
VaR frameset = frame.doc ument. All. Tags ("frameset ");
If (frameset. Length) return frameset [0];
Return frame.doc ument. Body;
}

You can set the print effect of the page loaded in IFRAME on the loaded page, such as paging.

5. Background Printing

I implemented this by creating a hidden IFRAME. Of course, there will still be page loading.
The following function creates an IFRAME loading page and prints it. For example, printhidden (URL) // URL is the page address

Function printhidden (URL ){
Document. Body. insertadjacenthtml ("beforeend ",
"<IFRAME name = printhiddenframe width = 0 Height = 0> </iframe> ");
VaR Doc = printhiddenframe.doc ument;
Doc. open ();
Doc. Write ("<body onload = \" parent. onprinthiddenframe () \ "> ");
Doc. Write ("<IFRAME name = printme width = 0 Height = 0 src = \" "+
URL + "\"> </iframe> ");
Doc. Write ("</body> ");
Doc. Close ();
}

Function onprinthiddenframe (){
Function onfinish (){
Printhiddenframe. outerhtml = "";
If (window. onprintcomplete) window. onprintcomplete ();
}
Printframe (printhiddenframe. printme, onfinish );
}

It uses printframe, so don't forget to reference the previous function.

In short, webbroswer has provided us with a solution. We only need to apply it as needed.

Post for the first time. If you have any questions, please contact us.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.