Use WebBrowser to completely solve Web printing problems (including background printing) _ javascript skills

Source: Internet
Author: User
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:


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

Document. body. insertAdjacentHTML ("beforeEnd ",
" 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 exampleThe format in the middle will only take effect during printing and will not affect the display interface.
So you can set

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 the following to the page:

You can.

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:

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 ",
"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 ",
"");
Var doc = printHiddenFrame.doc ument;
Doc. open ();
Doc. write ("");
Doc. write ("");
Doc. write ("");
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.

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.