Several solutions of web printing Control Technology

Source: Internet
Author: User
Tags print format
Several solutions of web printing Control Technology

Web developers will certainly face a common problem, that is, printing. Indeed, compared with Windows desktop applications, web application printing has various restrictions, and technicians often encounter user needs in the project development process. anyone who has worked in desktop application development will be very familiar with report controls such as Crystal Reports and active reports. They not only have simple and flexible design interfaces, but also have very powerful report functions, it can meet the printing requirements of various reports. Because of its special rendering method, web applications can only seek other solutions. Now let's analyze the web printing solutions that have been formed:

The existing web printing control technology is divided into several solutions:

I. Custom Controls are printed

Print using the webbrowser control provided by IE

Printing using third-party controls

1. Custom Controls

Custom Controls use tools such as VB or VC to generate COM components and use the defined print format to analyze and print the source files. Only by downloading and registering the generated components to the client can

Print.

The difficulty is to define the print format and how to analyze the print source file. A better method is to use XML technology to completely solve the problem. XML can be used to easily define the format of the printed target text, table, and other content.

However, it is difficult to develop programmers.

2. Use webbrowser for Web Printing

Webbrowser is a browser control built in IE, which does not need to be downloaded by users. This document discusses the technical content of the webbrowser control of ie6.0. Related Technical requirements include generation of printed documents, page settings, and implementation of printing operations.

(1) generate printed documents

1. client script

Client scripts include VBScript, Java Script, and JScript. The syntax used for application development in IE is JScript. Because it is almost identical to Java Script, it can also be called Java Script (hereinafter abbreviated as JS ). Generally, JavaScript is used to analyze Dom documents. Dom is a web document model proposed by Microsoft and is mainly used to implement web script programming.

Javascript can be used to analyze the content of the Source Page and extract the page elements to be printed for printing. By analyzing the content of the source document, you can generate a printed target document.

Advantage: the client generates the target document independently to reduce the server load;

Disadvantages: The analysis operations of the source document are complex, and the printed content in the source document must be agreed;

2. server-side program

The server-side program mainly uses the background code to read the print source from the database and generate the print target document. When the page is generated, you should also consider using CSS to implement forced paging control.

Advantage: You can generate a rich set of printed target documents, which is highly controllable. Since the printed content is obtained from the database, the generation operation is relatively simple;

Disadvantage: the server load is relatively large;

(2) page settings

Page settings mainly refer to setting the margins, headers, footers, and paper of printed documents. Page settings directly affect the generation of the printed document layout, so it is closely related to the generation of printed documents. For example

The number of rows, size, position, and font size.

The existing technology is to use the built-in print template of ie6.0 to control page settings, which can have a great impact on printing the target document. The print template can control the page margins, headers, footers, and even pages, get user settings, and send settings to the server.

The print template technology allows you to customize the preview window and print format to maximize the impact on the target document and print effect.

(3) Implementation of print operations

This function is mainly implemented by using the webbrowser control function interface to print, print preview (default ),

Page settings (default ).

<Object ID = 'webbrowser1' width = 0 Height = 0

Classid = 'clsid: 8856f961-340a-11d0-a96b-00c04fd705a2 '>

// Print

Webbrowser1.execwb (6, 1 );

// Print settings

Webbrowser1.execwb (8, 1 );

// Print and preview

Webbrowser1.execwb (7,1 );

3. The printing scheme adopted by an instance Project

The method of the server program and the print preview interface call are as follows:

Pageerrorprint. aspx. VB File

Main call page

Function printpage (ipageindex, strquery)

{

VaR strurl;

Strurl = "pageerrorprint. aspx? Pageindex = "+ ipageindex +" & querystring = "+

Strquery;

Winprint = Window. Open (strurl, "", "Left = 2000, Top = 2000, fullscreen = 3 ");

}

Preview Control in HTML source of printed pages

<Script language = "Java Script">

Document. Write ("<Object ID = 'webbrowser 'width = 0 Height = 0

Classid = 'clsid: 8856f961-340a-11d0-a96b-00c04fd705a2 '> </Object> ");

Webbrowser. execwb (7,1 );

Window. Opener = NULL;

Window. Close ();

</SCRIPT>

Program Header

'Declare the table container first

Protected withevents phcontainer as system. Web. UI. webcontrols. placeholder

'Number of records in each table

Private const itempertable as integer = 20

Key Implementation

'Create a table that meets printing requirements

Tabpageprint = newprinttable ()

'Add the header to this table

Call addtabletitle (tabpageprint)

'Initialize the recorder

I = 0

Iitemindex = istartpoint

For each clsitem in clsalldata. errorcollection

If I> 0 and I mod itempertable = 0 then

'Add a table control to the page.

Phcontainer. Controls. Add (tabpageprint)

'Add a line break to the page

Call addpagebreak ()

'Create a new round of table

Tabpageprint = newprinttable ()

Call addtabletitle (tabpageprint)

End if

'Add the record to the table

Call additemtotable (iitemindex, tabpageprint, clsitem)

Iitemindex = iitemindex + 1

I = I + 1

Next

'Add a table control to the page.

Phcontainer. Controls. Add (tabpageprint)

Supported Functions

'Function: add a line break for the page

Private sub addpagebreak ()

Dim ltbreak as literalcontrol

Ltbreak = new literalcontrol ("<P style = 'page-break-before: Always '> ")

Phcontainer. Controls. Add (ltbreak)

End sub

Ii. Use IE to print

This method is simple and commonly used for printing. You only need to design the report page and print it using the print function in the IE menu. The advantage is that it is simple and easy to implement. The disadvantage is that it is not flexible. It cannot control pages, but cannot control headers and footers.

3. Export the report to Word, Excel, or PDF

In this way, you need to export the page to an office document or PDF file. The lowest requirement is that the client has installed the software used to open Word, Excel, or PDF documents. This method can be easily implemented through the crystal report component or some other third-party controls. After being exported as PDF, the quality and effect of printing are good. After being exported as word or Excel, you can customize the printed content and format.

In short, the existing printing scheme has its own advantages. In the development process, it should be selected based on users' needs, and IE printing is simple and easy to implement, this solution is suitable when users have simple requirements or few printed content. Custom Control printing can be used to achieve full customization, but requires high technical requirements and development cycle. You can use the export method to customize or print multiple pages.

-------------------------------------------------------------

1. Control the margin of "vertical", horizontal ", and" page.

(1) <SCRIPT defer>

Function setprintsettings (){

// -- Advanced features

Factory. Printing. setmarginmeasure (2) // measure margins in inches

Factory. setpagerange (false, 1, 3) // need pages from 1 to 3

Factory. Printing. Printer = "HP Deskjet 870c"

Factory. Printing. Copies = 2

Factory. Printing. collate = true

Factory. Printing. papersize = "A4"

Factory. Printing. papersource = "manual feed"

// -- Basic Features

Factory. Printing. header = "this is meadco"

Factory. Printing. footer = "advanced printing by scriptx"

Factory. Printing. Portrait = false

Factory. Printing. leftmargin = 1.0

Factory. Printing. topmargin = 1.0

Factory. Printing. rightmargin = 1.0

Factory. Printing. bottommargin = 1.0

}

</SCRIPT>

(2)

<Script language = "Java Script">

Function printsetup (){

// Print page settings

Wb.exe CWB (8, 1 );

}

Function printpreview (){

// Print the Page Preview

Wb.exe CWB (7,1 );

}

Function printit ()

{

If (confirm ('Are you sure you want to print? ')){

Wb.exe CWB (6, 6)

}

}

</SCRIPT>

</Head>

<Body>

<Object classid = "CLSID: 8856f961-340a-11d0-a96b-00c04fd705a2"

Height = 0 id = WB name = WB width = 0> </Object>

<Input type = button name = button_print value = "print"

Onclick = "Java Script: printit ()">

<Input type = button name = button_setup value = "print page settings"

Onclick = "Java Script: printsetup ();">

<Input type = button name = button_show value = "print preview"

Onclick = "Java Script: printpreview ();">

<Input type = button name = button_fh value = "disabled"

Onclick = "Java Script: window. Close ();">

------------------------------------------------

There are other usage examples of this component:

Webbrowser. execwb () Open

Web. execwb () Close all existing IE Windows and open a new window

Web. execwb () save Web Page

Web. execwb (6, 1) Printing

Web. execwb (7,1) print and preview

Web. execwb (8, 1) Printing page settings

Web. execwb () view page properties

Web. execwb () seems to be undo, to be confirmed

Select all web. execwb ()

Web. execwb (22, 1) Refresh

Web. execwb () Close form no prompt

2. Paging Printing

<HTML>

<Head>

<Style>

P {page-break-after: Always}

</Style>

</Head>

<Body>

<% While not Rs. EOF %>

<P> <% = RS (0) %> </P>

<% Rs. movenext %>

<% Wend %>

</Body>

</Html>

3. How to remove the path at the bottom of the page and the page number at the top of the page during ASP page printing?

(1) The IE File-> page settings-> the content in the header and footer is removed, and the print is not displayed.

(2) <HTML>

<Head>

<Title> new document </title>

<Meta name = "generator" content = "editplus">

<Meta name = "author" content = "YC">

<Script language = "VBScript">

Dim hkey_root, hkey_path, hkey_key

Hkey_root = "HKEY_CURRENT_USER"

Hkey_path = "/software/Microsoft/Internet Explorer/pagesetup"

'// Set the page header and footer to be blank

Function pagesetup_null ()

On Error resume next

Set regwsh = Createobject ("wscript. Shell ")

Hkey_key = "/Header"

Regwsh. regwrite hkey_root + hkey_path + hkey_key ,""

Hkey_key = "/footer"

Regwsh. regwrite hkey_root + hkey_path + hkey_key ,""

End Function

'// Set the page header and footer to the default value.

Function pagesetup_default ()

On Error resume next

Set regwsh = Createobject ("wscript. Shell ")

Hkey_key = "/Header"

Regwsh. regwrite hkey_root + hkey_path + hkey_key, "& W & B page number, & P/& P"

Hkey_key = "/footer"

Regwsh. regwrite hkey_root + hkey_path + hkey_key, "& U & B & D"

End Function

</SCRIPT>

</Head>

<Body>

<Br/>

<Br/>

<Br/>

<Br/>

<Br/>

<Br/> <p align = center>

<Input type = "button" value = "Clear page number" onclick = pagesetup_null ()> <input type = "button" value = "Restore page" onclick = pagesetup_default ()> <br/>

</P>

</Body>

</Html>

4. Floating frame Printing

<Script language = java script>

Function button1_onclick (){

VaR odoc=w.w.iframe1.doc ument;

VaR r = odoc. Body. createTextRange ();

VaR stxt=r.html text;

Alert (stxt)

VaR pwin = Window. Open ("", "print ");

Pwin.doc ument. Write (stxt );

Pwin. Print ();

}

</SCRIPT>

4. Use the filesystem component to implement specific local printing in Web Applications

<Script language = VBScript>

Function print_onclick // print the Function

Dim label

Label = Document. printinfo. Label. Value // obtain HTML page data

Set objfs = Createobject ("scripting. FileSystemObject") // create an instance of the filesystem Component Object

Set objprinter = objfs. createtextfile ("LPT1:", true) // establish a connection with the printer

Objprinter. writeline ("__________________________________") // output printed content

Objprinter. writeline ("| ")

Objprinter. writeline ("| the data you printed is:" & label & "| ")

Objprinter. writeline ("| ")

Objprinter. writeline ("| _________________________________ | ")

Objprinter. Close // disconnect from the printer

Set objprinter = nothing

Set objfs = nothing // disable the filesystem Component Object

End Function

</SCRIPT>

Server script:

<% .........

Set conn = server. Createobject ("ADODB. Connection ")

Conn. Open "DSN = Name; uid = xxxx; Pwd = xxxx ;"

Set rs = server. Createobject ("ADODB. recordset ")

Rs. Open ("select ......"), Conn, 1, 1

.......... %> // Interact with the database

HTML page encoding:

<HTML>

.........

<Form ID = printinfo name = "printinfo">

<Input type = "button" value = "Print>" id = Print name = print> // call the print function

<Input type = hidden id = text1 name = label value = <% = ......... %> // Save data sent from the server

.........

</Html>

 

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.