Several implementation methods of Web printing (reproduced)

Source: Internet
Author: User

Several implementation methods of Web printing (reproduced)

Web printing, three ways to implement web print controls

Do management system, printing has always been a tricky problem, to do b/s system This problem is more prominent! Here are three commonly used web printing methods

1. Use Word or Excel for Web printing (if you do not modify IE settings, you can generate the XLS file on the Web server side and then through xlbook = xls). Workbooks.Open (RemotePath) Get object print )

Implementation process: First import the data you want to print into Word or Excel, and then use Word or Excel's print function to achieve web printing.
The following is an example of how to print in Excel
There are many ways to import data from a Web page into Excel, and here's a way to take advantage of an ActiveX control, the Excel.Application, which is a programming interface provided by MS for Excel. In many programming languages, Excel tables can be manipulated through this interface.
The following is a simple example of a JavaScript script to implement.
< script language= "JavaScript" >

function Excelprint () {

var excelapp;//storing Excel objects

var excelbook;//storing Excel artifact book files

var excelsheet;//storing Excel Active worksheets

try{

Excelapp = new ActiveXObject ("Excel. Application ");//Create an Excel object}

catch (e) {

Alert ("Please enable ActiveX control settings!") ");

return;}

Excelbook = EXCELAPP.WORKBOOKS.ADD ();//Create an Excel workbook file

ExcelSheet = excelbook.activesheet;//Activate Excel worksheet

var Rowlen = number of rows of the Printtable.rows.length;//table object

for (var i=0;i< rowlen;i++) {

var Collen = printtable.rows (i). The number of columns for the Cells.length;//table object

for (var j=0;j< collen;j++)//Assign a value to an Excel table cell

Excelsheet.cells (i+1,j+1). Value = Printtable.rows (i). Cells (j). InnerText; Import the innertext of each cell in a table into a cell in Excel

excelapp.visible = true;//Set Excel object visible}

Excelsheet.printout (); Print a worksheet

Excelbook.close (TRUE); Close Document

Excelapp.quit (); End an Excel object

Excelapp=null; To release an Excel object

</script> 
   
   Note:
   Run this program if IE to allow initialization and scripting of ActiveX controls that are not marked as secure. The Setup method is as follows:
   open Control Panel →internet options → security → custom level → Initialize and script The ActiveX controls that are not marked as safe → enable, so our program is ready to run. If the ActiveX control setting is not enabled, the program throws an exception when it executes the Excel object, and the catch () statement can be used to catch the exception and handle it accordingly.
   running the program must have MS EXCEL installed on the client, or ActiveX will not drive it.
   
   
   
2, using the browser's own print control for Web printing
   implementation process: Directly call IE printing function or call Window.print () in the program to achieve Web printing, headers and footers will have page title, page number, URL, date and other information, these printing if not required, how can be removed. The practice is very simple, only in the IE File menu to open the page Number Settings dialog box, remove the header and footer in the settings of what information, you can. But this requires each client to go through the manual setup once. If you do not want each client to be set manually, you can do so by modifying the registry's key values.
   The following is a procedure for modifying the registry using VBScript:
   < script language= "VBScript";

Dim path, Reg

' Path stores the registry address for IE print settings, and Reg holds the object for the Wscript.Shell component

Path = "Hkey_current_user\software\micro-soft\internet Explorer\PageSetup"

' Modify print settings through the registry to modify only the headers, footers, and values of each boundary

' Parameter description: header--header, footer--footer, margin_left--left border

' margin_top--Upper border, margin_right--right border, margin_bottom--lower border

' The page margin is set in 1 for 25.4mm, which means that margin_left=1 represents the actual value of 25.4mm

function PageSetup (header, footer, Margin_left, Margin_top, Margin_right, Margin_bottom)

On Error Resume Next

Set reg = CreateObject ("Wscript.Shell")

If Err. Number>0 Then

MsgBox "Cannot create Wscript.Shell Object! "

Exit function

End If

Reg. RegWrite path+ "\header", header ' Set headers

Reg. RegWrite path+ "\footer", footer ' set footer

Reg. RegWrite path+ "\margin_left", Margin_left ' set left border

Reg. RegWrite path+ "\margin_top", margin_top ' set upper bounds

Reg. RegWrite path+ "\margin_right", Margin_right ' Set right border

Reg. RegWrite path+ "\margin_bottom", Margin_bottom ' set down boundary

End Function

</script>

It is also important to note that the use of Window.print () method to print, is directly pop-up print dialog box, rather than print preview of the window. In general, users want to print a preview before printing. or some format fixed, each time is the same format, I would like to not pop up the print dialog box, directly printed out.
There are users who want each print to be directly and a kind of paper binding good, when printing directly to call that type of paper to print, so Window.print () is clearly far from enough.


3, the use of third-party control or reporting software to achieve web printing
Implementation process: Third-party controls encapsulate printed parameters and methods into objects that can be conveniently called directly on the page, such as Scriptx.cab,eprint.cab, which is a control of this type. Can be directly implemented in the Code Web page header footer settings, Web printing paper binding, Web print margin settings, Web Print preview, direct Web printing.
Web Printing format settings, Web printing paging, Web printing page re-printing some information, some information can only be printed on the first page, some information can only be printed on the last page and so on, but also the solution. ScriptX does not deal with these scenarios, Webprint has WebGrid and eprint two solutions, the table of row and column rules can be easily webgrid to deal with, complex format can be used eprint to design the format.

In general, this type of print control is chargeable, and the user can consider it from an economic standpoint.
==========================================================================================================

First, the browser's Print function menu
The advantage of this approach is that there is no need to expand the browser, it is the simplest way, but the problem is the most, such as:

  1. cannot be precisely paged.   The
    Browser is usually based on the page size set by the user, the content of the Web page, to determine the paging location, the programmer is difficult to control. There will be a footer header interference.

  2. cannot resolve continuous printing.  
    For example, instead of printing just one ticket, you print several tickets in a row at a time.

Second, use WebBrowser control + JavaScript
This is actually a program call to the browser's Print function menu, which is no different from the Print function menu. The problem with paging is still there, but instead of having to go to a menu, a button in a webpage, or a link inside a page, can be called.

Third, using the print CSS
This is one of the most ideal ways to implement web sets. This approach enables the control of HTML document output printing by embedding print-related CSS styles in an HTML document, such as setting the paper size, paper orientation, print margins, paging, and so on. Obviously, this is a small cost, no need to download any plug-ins, and very good cross-platform. Print CSS has been going on for some time, but unfortunately, none of the vendors ' browsers have implemented these standards well so far, which makes it impossible for programmers to actually develop with print CSS. For printing CSS, see:
Http://css-discuss.incutio.com/?page=PrintStylesheets

Iv. Use of PDF files
In this way, a PDF file stream is downloaded from the server side, opened in IE with an Adobe plugin, and then printed with Adobe's print menu, although this option allows for precise nesting, but requires downloading of adobe plugins. This is a frequently recommended printing method for foreign reporting tools, but in China, where PDFs are less prevalent, this is not the best option.

v. Use of pure ActiveX
The solution is to download a control where the data of the ticket is no longer rendered in HTML, but is rendered in ActiveX. The advantage of this scheme is the high precision of printing, good paging controllability, but the disadvantage is also obvious, embedded ActiveX control to undermine the overall HTML style of Web applications, and such a large control (generally more than 1M, download time). This is generally the case with non-Java-class reporting products on the market.

Vi. Adoption of the applet approach
The use of applets, paging or precise printing, can be perfect, but the shortcomings are also obvious, manifested in:

  1. Installation applet costs a lot. Need to download a more than 10 m file.   The
    applet itself may not be large, but the JRE required to run the applet is typically at least 10 m (jre1.4.2, 15.45M). Users need a lot of patience to print.

  2. When printing a report, it is inefficient to retrieve data from the server again.  
    Because the applet scheme generally renders the data in HTML, the applet must retrieve the data from the same ticket to the server, appearing to be the ticket that prints the current page, and in fact, the applet does not print with the current HTML page's data. Instead, download the data into the applet to print to the server. That is, to print, it must be two requests, one HTML rendering, one for printing.  
    Report tools for Java classes on the market, generally recommend applets for printing.

Seven, the lightweight way of ActiveX printing
This is the company at the customer's request, the latest launch of a most innovative web-based set of solutions.

Jay as a pure Java reporting tools, previously provided Web package is based on applets, applets also do as small as possible (only 24kb), but the user is still complaining, because the JRE is too large, installation needs patience. In addition, using the Applet method, it is difficult to achieve the client batch printing function.

Several implementation methods of Web printing (reproduced)

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.