Alternative web printing methods

Source: Internet
Author: User
Tags print format

The alternative method for web printing is from wfmazhenhai's Blog
Alternative methods for keyword web Printing
Source
I believe that people who have used the B/S method may encounter such a problem. How to conveniently and visually print reports. If you use the print menu of the browser to print, some useless items on the web page will be sent to the report, such as the application menu. Because when you select the print menu to print the webpage, all content in the webpage will be printed out. If your application has a split frame, the printed content will include the content in each frame, the content you actually want is only part of it. Therefore, many applications can only put the printing function in the background. In this case, what should we do? In fact, there are many ways to implement the web page printing function.
One way is to use professional printing tools, such as Crystal Reports ). Those who have used Visual Studio 5.0 certainly remember this tool, but at that time the version was only 4. A few. The latest version has reached 9, and the latest versions of Crystal Reports Support WEB printing. The simplest method is to create a template with a crystal report, and then use ASP to call the created template with parameters. After a crystal report is generated on a web page, it can be printed directly or stored as another Excel file. You can use Crystal Reports to create beautiful styles. The key lies in your ability to develop Crystal Reports. However, due to the high price of Crystal Reports, it is only affordable when the project is very profitable.
The second method is to purchase a third-party online printing control, which is cheaper than the cost of the crystal report, but the performance is truly benevolent.
The third method is to use style sheets and JavaScript udfs. Using Style Sheets and JavaScript, you can print web pages. There is an instance here for you to see. Below is a detailed description of the print function implementation:
<Script language = "JavaScript" type = "text/JavaScript">
<! --
Function DP (){
If (window. print)
{
Var Div1 = document. all. Div1.innerHTML;
Var Div2 = document. all. Div2.innerHTML;
//************************************** ***************
// Div1 and Div2 are in the printed area.
// Here, based on what you want to print, it is used in the original display page
// <Div id = Div1> Div1.... </div> <div id = Div2> div2... </div>
// Indicates the number of items to be printed.
//************************************** * ************** Var css = '<style type = "text/css" media = all>' +
'P {line-height: 120%} '+
'. Ftitle {line-height: 120%; font-size: 18px; color: #000000}' +
'Td {font-size: 10px; color: #000000} '+
'</Style> ';
//************************************** ***************
// Define the CSS used for printing. The specific format you want to print depends on yourself.
//, But note: if there is anything inconsistent with the web page, it may be printed.
// The displayed page may vary with the webpage format and font.
//************************************** ***************
Var body = '<table width = "640" border = "0" cellspacing = "0" cellpadding = "5">' +
'<Tr>' +
'<Td class = "fbody">' +
'<Div align = "center" class = ftitle>' + Div1 + '</div>' + Div2 +
'</Td>' +
'</Tr>' +
'</Table> ';
//************************************** ****************
// Reset the print format here.
// Re-combine the DIV content of the webpage. you can remove it based on your original table content.
// Do not print it. You can also ignore the noprint defined below.
// The printed content only calls the content you want to print.
// Print out blank, not very beautiful. The table width must match the printed paper width.
//************************************** ****************
Document. body. innerHTML = '<center>' + css + body + '</center> ';
//************************************** ****************
// Reset document. body. The printed document is ready.
//************************************** ****************
Window. print ();
Window. history. go (0 );
//************************************** ****************
// Call the print command to print the content of the current window. When you print it, it's actually a new one.
// Webpage, but the webpage file is still original. Call
// Window. history. go (0) and return to the page before printing.
//************************************** ****************
}
}
-->
</Script>
<Style>
@ Media print {
. Noprint {display: none}
}
</Style>
<! -- //. Noprint defines noprint. When class = "noprint" is added to the following areas that do not need to be printed, window. print () will be ignored. -->
All right, everything is ready. Now you have to call the DP function. If you design the button to be called on the same webpage, you can call it directly; if you use the frame sharding method, the call button is on another webpage, and the window is added to the first line of the DP function. the focus () command. Otherwise, only webpages with buttons are printed.
The fourth method is a clever method. Similarly, you can call window. print () to generate a webpage with the print button on another frame. If the report webpage is on mainFrame and the button is on topFrame, the button calls the PrintReports () function. The PrintReports () function can be printed as follows.
Function PrintReports () // function in the topFrame webpage
{
Try
//************************************** *****************
// Handle the error. If the webpage in mainFrame does not have the DP function, it is not printed.
//************************************** *****************
{
Window. parent. frames ("mainFrame"). DP ();
}
Catch (e)
{
Alert ("no object to print! ");
}
}
Function DP () // functions in the mainFrame webpage
{
Window. focus ();
If (window. print)
{
Window. print ();
}
}
I personally think this is the most convenient method.
Finally, if you remove the settings of "header and page angle" in "file-page settings" of IE browser, the effect will be better. (Reprinted)

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.