roc41793818 2007-4-17 12:05
WEB打印设置解决方案
WEB 打印设置解决方案一(通过修改注册表改变IE打印设置)
以下是IE打印设置脚本
〈script language=“VBScript“〉
Dim hkey_root,hkey_path,hkey_key
hkey_root = “HKEY_CURRENT_USER“
hkey_path = “\Software\Microsoft\Internet Explorer\PageSetup“ //IE打印设置的注册表地址
//设置修改函数
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,““ //页脚
hkey_key=“\margin_left“
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,“1“ //键值设定--左边边界
hkey_key=“\margin_top“
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,“1“ //键值设定--上边边界
hkey_key=“\margin_right“
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,“1“ //键值设定--右边边界
hkey_key=“\margin_bottom“
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,“1“ //键值设定--下边边界
end function
//恢复默认设置
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页,&p/&P“ //页数-
hkey_key=“\footer“
RegWsh.RegWrite hkey_root+hkey_path+hkey_key,“&u&b&d“ //网址,日期等信息
end function
〈/script〉
除了以上键值以外还有以下几项,其值有待确认。
duplex
orientation
paper_size
paper_source
printer
注意:上面对页边距的设置为:1,实际设置值为25.4。
===================================================================
WEB打印设置解决方案二(利用ScriptX.cab控件改变IE打印设置)
出处
下载ScriptX.cab控件,修改codebase的值,默认下面的设置也可以。
页面加载控件:
〈object id=“factory“ style=“display:none“ viewastext classid=“clsid:1663ed61-23eb-11d2-b92f-008048fdd814“ codebase=“[url]http://www.meadroid.com/scriptx/ScriptX.cab#Version=5[/url],60,0,360“〉〈/object〉
控件使用教本:
〈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 //方向,true
factory.printing.leftMargin = 1.0
factory.printing.topMargin = 1.0
factory.printing.rightMargin = 1.0
factory.printing.bottomMargin = 1.0
factory.printing.Print(false) //直接打印,true ,好像不起作用,总是弹出选择打印机窗口
factory.printing.PageSetup() //打印设置
factory.printing.Preview() //打印预览
}
〈/script〉
=========================================================
WEB打印设置解决方案三(FileSystem组件实现WEB打印)
程序代码:
〈script Language=VBScript〉
Function print_onclick //打印函数
Dim label
label=document.printinfo.label.value //获得HTML页面的数据
set objfs=CreateObject(“Scripting.FileSystemObject“) //创建FileSystem组件对象的实例
set objprinter=objfs.CreateTextFile (“LPT1:“,true) //建立与打印机的连接
objprinter.Writeline(“__________________________________“) //输出打印的内容
objprinter.Writeline(“| |“)
objprinter.Writeline(“| 您打印的数据是:“&label& “ |”)
objprinter.Writeline(“| |“)
objprinter.Writeline(“|_________________________________|“)
objprinter.close //断开与打印机的连接
Set objprinter = nothing
Set objfs = nothing // 关闭FileSystem组件对象
End Function
〈/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
%〉 //与数据库进行交互
HTML页面编码:
〈HTML〉
………
〈FORM ID=printinfo NAME=“printinfo“ 〉
〈INPUT type=“button“ value=“打印〉〉“ id=print name=print 〉 //调用打印函数
〈INPUT type=“hidden“ id=“text1“ name=“label“ value=〈%=………%〉〉 //保存服务器端传来的数据
………
〈/HTML〉
===============================================
WEB打印设置解决方案四(在ASP中实现网络打印功能)
出处
使用到的技术:
ASP,WSH,VBScript
文件ASPPrint.asp代码如下:
〈%@ Language=VBScript %〉
〈%
Option Explicit
Dim strSubmit ’ Form中用来保存提交按钮的值
Dim strPrinterPath ’ Form中保存网络打印机路径的值
Dim strUsername ’ Form中用户名的值
Dim strPassword ’ Form中密码的值
Dim strMessage ’ Form打印内容的值
Dim objFS ’ VBScript中的文件系统对象
Dim objWSHNet ’ WSH中的网络对象
Dim objPrinter ’ 打印对象
strSubmit = Request.Form(“Submit“)
%〉
〈HTML〉
〈HEAD〉
〈META NAME=“GENERATOR“ Content=“Microsoft Visual Studio 6.0“〉
〈/HEAD〉
〈BODY〉
〈%
If strSubmit = ““ Then
%〉
注意的是:
由于我是演示起见,其中有关NT的帐号和密码都是使用了不加密的手段在ASP中传递的真正的运用中应该对该登录过程进行安全处理。
〈FORM action=“ASPPrint.asp“ method=POST id=form name=form〉
〈TABLE WIDTH=100% ALIGN=center BORDER=0 CELLSPACING=1 CELLPADDING=1〉
〈TR〉
〈TD ALIGN=right NOWRAP〉网络打印机路径:〈/TD〉
〈TD ALIGN=left NOWRAP〉〈INPUT type=“text“ id=printerpath name=printerpath value=“\\〈 Domain 〉\〈 Printer 〉“〉〈/TD〉
〈/TR〉
〈TR〉
〈TD ALIGN=right NOWRAP〉登录帐号:〈/TD〉
〈TD ALIGN=left NOWRAP〉〈INPUT type=“text“ id=username name=username
value=“〈% = strUsername %〉“〉〈/TD〉
〈/TR〉
〈TR〉
〈TD ALIGN=right NOWRAP〉登录口令:〈/TD〉
〈TD ALIGN=left NOWRAP〉〈INPUT type=“password“ id=password name=password〉〈/TD〉
〈/TR〉
〈TR〉
〈TD ALIGN=right NOWRAP〉请输入你想打印的文字:〈/TD〉
〈TD ALIGN=left NOWRAP〉〈TEXTAREA rows=2 cols=20 id=message name=message〉〈/TEXTAREA〉〈/TD〉
〈/TR〉
〈TR〉
〈TD ALIGN=right NOWRAP〉 〈/TD〉
〈TD ALIGN=left NOWRAP〉〈INPUT type=“submit“ value=“Submit“ id=submit name=submit〉〈/TD〉
〈/TR〉
〈/TABLE〉
〈/FORM〉
当以上信息被提交后,就可以按照下面的代码进行打印了。
〈%
Else
’ 从form中取得响应信息。
strPrinterPath = Request.Form(“printerpath“)
strUsername = Request.Form(“username“)
strPassword = Request.Form(“password“)
strMessage = Request.Form(“message“)
We will now use the VBScript FileSystemObject object and the WSH Net work object. The Network object will
give us the methods we need to open a printer connection, and the FileSystemObject will allow us to stream our
output to the printer. We create these objects in the following code
example:
’ 使用WSH连接网络打印机
Set objWSHNet = CreateObject(“WScript.Network“)
objWSHNet.AddPrinterConnection “LPT1“, strPrinterPath, False, strUsername, strPassword
’使用文件系统对象将打印设备作为一个文件使用
Set objFS = CreateObject(“Scripting.FileSystemObject“)
Set objPrinter = objFS.CreateTextFile(“LPT1:“, True)
’ 给打印设备送出文本
objPrinter.Write(strMessage)
’关闭打印设备对象并进行错误陷阱处理
On Error Resume Next
objPrinter.Close
’ 如果发生错误,关闭打印连接,并输出错误信息
If Err Then
Response.Write (“Error # “ & CStr(Err.Number) & “ “ & Err.Description)
Err.Clear
Else
’ 操作成功,输出确认信息
Response.Write(“〈CENTER〉“)
Response.Write(“〈TABLE WIDTH=100% ALIGN=center BORDER=0 CELLSPACING=1 CELLPADDING=1〉“)
Response.Write(“〈TR〉〈TD ALIGN=RIGHT〉〈B〉打印消息送出:〈/B〉〈/TD〉“)
Response.Write(“〈TD ALIGN=LEFT〉“ & strMessage & “〈/TD〉〈/TR〉“)
Response.Write(“〈TR〉〈TD ALIGN=RIGHT〉〈B〉网络打印机路径:〈/B〉〈/TD〉“)
Response.Write(“〈TD ALIGN=LEFT〉“ & strPrinterPath & “〈/TD〉〈/TR〉“)
Response.Write(“〈TR〉〈TD ALIGN=RIGHT〉〈B〉登录帐号:〈/B〉〈/TD〉“)
Response.Write(“〈TD ALIGN=LEFT〉“ & strUsername & “〈/TD〉〈/TR〉“)
Response.Write(“〈/TABLE〉“)
Response.Write(“〈/CENTER〉“)
End If
’ 取消打印连接
objWSHNet.RemovePrinterConnection “LPT1:“
Set objWSHNet = Nothing
Set objFS = Nothing
Set objPrinter = Nothing
End If
%〉
〈/BODY〉
〈/HTML〉
=============================================================
每页都有的表头和打印分页
出处
〈!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN“〉
〈html〉
〈head〉
〈meta http-equiv=“Content-Type“ content=“text/html; charset=gb2312“〉
〈title〉无标题文档〈/title〉
〈style〉
@media print{
INPUT {display:none}
}
〈/style〉
〈/head〉
〈body〉
〈TABLE border=“0“ style=“font-size:9pt;“ width=“300px“ align=“center“〉
〈THEAD style=“display:table-header-group;font-weight:bold“〉
〈TR〉〈TD colspan=“2“ align=“center“ style=“font-weight:bold;border:3px double red“〉每页都有的表头〈/TD〉〈/TR〉
〈/THEAD〉
〈TBODY style=“text-align:center““〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表衲谌?lt;/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR style=“page-break-after:always;“〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR style=“page-break-after:always;“〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈TR style=“page-break-after:always;“〉〈TD〉表格内容〈/TD〉〈TD〉表格内容〈/TD〉〈/TR〉
〈/TBODY〉
〈TFOOT style=“display:table-footer-group;font-weight:bold“〉
〈TR〉
〈TD colspan=“2“ align=“center“ style=“font-weight:bold;border:3px double blue“〉每页都有的表尾〈/TD〉
〈/TR〉
〈/TFOOT〉
〈/TABLE〉
〈input type=button value=“ 打 印 “ onclick=javascript:window.print()〉
〈/body〉
〈/html〉