Posts Tagged ‘打印’

PHP生成便于打印的网页

星期一, 06月 2nd, 2008

很多新闻和信息站点都提供了一种生成便于打印de网页de方法,所产生de页面de排版布局更有利于打印机de打印输出,这种方法方便了我从网页上直接打印我所需de内容,而不必为格式不规整伤脑筋,或者粘贴到文本编辑器中重新排版.然而,我却没看到有多少网站详细解释这些是如何实现de,在这里我提供一小段相关代码——用PHP来实现生成便于打印de网页并不是像想象de那么难,希望对大家有帮助.
要生成便于打印de网页,需要我做哪些工作呢?这主要取决于您de网站特点,和您想要生成de版式特征,不过有一些基本处理需要完成:
1、 页宽——生成页面de宽度必须限制,要打印A4de纸,大约网页要在630像素宽.
2、 页面背景色——为了美观,很多网页使用了不同de背景色和背景图片,但是作为要打印de网页,最合适效果de还是白底黑字为好.
3、 广告条——移除页面上de广告
4、 表格de背景色——我经常在表格中用颜色来强调信息和标题,这些也必须移除.
5、 链接——页面中de超链接也必须改变以使URL可见,例如:<a href=http://www.gbdirect.co.uk/ >GBDirect</a>应显示为GBDirect (http://www.gbdirect.co.uk/)
6、 菜单——菜单是最难被禁止de,然而如果您de页面是使用模板来构建de话,那么最简单de方法是换用便于打印de没有菜单de模板.
这些生成便于打印页面de所有方法,都是非常简单de,需要实现de时候您可以被下面de相关代码放到网页中:
<?
//从环境变量中得到文件de相对路径
$page=substr($SCRIPT_NAME,1);
// 显示一个图标并连接到Printer Friendly Pages
// 便于打印页面de生成程序pfp.php
?>
<a href="pfp.php?page=<?=$page?>">;
<img src="printer.gif" width="36" height="36" border="0"
alt="Click here to produce a printer friendly page">
<font face="arial, helvetica" size="2">
Printer Friendly Version
</font>
</a>
把当前页面de名称传递到pfp.php程序中,这个程序使用PHPde“file”函数把页面作为一个字符串来处理.当这个页面被载入de时候,程序就可以增加、改写或删除HTML片段.
<?
ereg(’^.*/’,$SCRIPT_FILENAME,$tmp);
$page_path = substr($tmp[0],0,-1);
?>
<html>
<head>
<base href="http://<? echo $HTTP_HOST ?>/">
<meta name="robots" content="no index, no follow">
<title>Printer Friendly Page</title>
</head>
<body bgcolor="white">
<font face="Arial,Helvetica">
<table border="0" cellpadding="5" cellspacing="0" width="630" >
<tr>
<td valign="top">
<?
// check if the filename for the page exists
if (!file_exists("$page.inc"))
{
echo "<strong>Error - The page <?=$page?>".
"does not exist on this site.</strong>";
}
else
{
// 得到页面de内容并把它放到一个字符串中
$fcontents = join(”, file("$page.inc"));
// 忽略颜色属性,转换以’ignore’替代’color’
$fcontents = ereg_replace(’color’,'ignore’,$fcontents);
// 去除超链接中de “_blank”
$fcontents = ereg_replace(’target=\"_blank\"’,”,$fcontents);
// 替换</a>标记
$fcontents = ereg_replace(’</a>’,”,$fcontents);
// 显示URLde绝对地址
$fcontents = ereg_replace(’<a[^h]*href="(http://[^"]*)"[^>]*>;([^]*)’,
‘<strong>\\2</strong><em>(\\1)</em>’,$fcontents);
// 把相对链接转为绝对链接
$fcontents = ereg_replace(
‘<a[^h]*href="([^"]*)"[^>]*>([^]*)’,
"<strong>\\2</strong><em>(http://$HTTP_HOST/\\1)</em>";,
$fcontents);
// 背景颜色改回白色
$fcontents = ereg_replace(’<body bgignore’,'<body bgcolor’, $fcontents);
// if any markers left restore link end element
$fcontents = ereg_replace(”,’</a>’,$fcontents);
// 输出页面
echo $fcontents;
}
?>
</td>
</tr>
<tr>
<td align="center"><hr width="90%"></td>
</tr>
<tr>
<td align="center">
<? include("$page_path/footer.inc"); ?>
</td>
</tr>
</table>
</font>
</body>
</html>
这样便于打印de页面就生成了,希望对大家能有帮助.
(译自PHPBulider/Mark Spink)

用在PHP里的JS打印函数

星期一, 06月 2nd, 2008

auto=1立即PRINT,否则timeOut毫秒后PRINT,如printPage(0,5000);

function printPage($auto=1,$timeOut=10000) {
if ($auto == 1) {
echo ”
< SCRIPT LANGUAGE=”JavaScript” >
< !– Begin
if (window.print) {
window.print();
}
else {
alert(’No printer driver in your PC’);
}
// End — >
< /script >
n”;
}
else {
echo ”
< SCRIPT LANGUAGE=”JavaScript” >
< !– Begin
if (window.print) {
setTimeout(’printCheck()’,'$timeOut’);
}
else {
alert(’No printer driver in your PC’);
}
function printCheck() {
agree = confirm(’OK to print now?’);
if (agree) window.print();
}
// End — >
< /script >
n”;
}
}

JS应用之禁止抓屏、复制、打印

星期一, 06月 2nd, 2008

项目需要禁止抓屏、复制、打印de要求,复制、打印做起来可能顺手一点网上各种各样de脚本俯首皆是.但抓屏怎么禁止?PrintScreen是一个特殊de键,它是没有keyCodede键,所以onkeydown变得毫无用处.不过换一种思路de话可会更好,我从粘贴板着手采取曲线救国策略.相关代码如下:
<script language=”javascript”>
window.setInterval(”clipboardData.setData(’text’,”)”,100);
</script>
以上相关代码是每100毫秒清空一次粘贴板操作.当页面加载时脚本程序就开始自动执行.但这样有个弊端,不管网页被最小化还是怎么de只要这个窗口开着我电脑所有de复制操作都无法进行(脚本一直在清空粘贴板),从某种意义上讲是达到预期效果了,但有些欠佳:(.
我知道所有de控件都有onfocus和onblur事件,window窗口也不例外.我通过利用这两个事件,只有在当前窗口处于活动状态时才执行清空操作,否则停止执行.相关代码如下:
<script language=”javascript”>
var interval
window.onfocus=function(){interval=window.setInterval(”clipboardData.setData(’text’,”)”,100);}
window.onblur=function(){window.clearInterval(interval);}
</script>
这样就可以完美de解决这个问题.但对于不将截屏内容放入粘贴板de截屏程序来说此方法还是心有余而力不足de.
最新测试实践发现使用onfocus和onblur事件de方法也不尽如人意,当焦点指向程序页面内其他控件(即使Table)时,window将失去焦点从而触发onblur事件停止执行清空粘贴板命令,难道需要遍历所有控件为其onfocus和onblur绑定事件?有些迷茫与失望.
另window.onfocus只是documentdeonfocus,如果焦点在地址栏或菜单之类de地方onfocus也将失效.
仅以此文字记录自己半天时间研究JSde心得.
禁止打印只需将如果下样式相关代码放入程序即可(打印出de页面内容将为空白):
<style>@media print{body{display:none}}</style>
禁止复制、选择、右键菜单:
<script language=javascript>
function click() {
return false;}
function click1(){if (event.button==2) {return false; }}
function CtrlKeyDown(){
if (event.keyCode==67&&event.ctrlKey)
{
clipboardData.setData(’text’,”);
return false;
}
}
document.onkeydown=CtrlKeyDown;
document.onselectstart=click;
document.onmousedown=click1;
</script>
<noscript><iframe src=*.html></iframe></noscript>
<script language=javascript>
<!–
if (window.Event)
document.captureEvents(Event.MOUSEUP);
function nocontextmenu(){
event.cancelBubble = true
event.returnValue = false;
return false;
}
function norightclick(e){
if (window.Event){
if (e.which == 2 || e.which == 3)
return false;
}
else if (event.button == 2 || event.button == 3)
{
event.cancelBubble = true;
event.returnValue = false;return false;}
}
document.oncontextmenu = nocontextmenu; // for IE5
document.onmousedown = norightclick; // for all others
//–></script>

以上相关代码在IE6.0环境运行正常.

ASP控制每页打印行数

星期一, 06月 2nd, 2008

<%
pagenum=55′指定打印行数
%>
<HTML>
<HEAD>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
<TITLE>销售利润明细报表打印</TITLE>
<style type=”text/css”>
td {font-size:9pt; color:#000000}
A{text-decoration:none}
A:hover{color:#FF0000;text-decoration:derline}
.break{page-break-before:always}
</style>
</HEAD>
<script language=”javascript”>
window.print()
</script>
<BODY style=”border:none” topmargin=”0″ leftmargin=”6″ onload=”javascrpt:pagesetup_default();”>
<script language=”VbScript”>
dim hkey_root,hkey_path,hkey_key
hkey_root=”HKEY_CURRENT_USER”
hkey_path=”\Software\Microsoft\Internet Explorer\PageSetup”
function pagesetup_default()
on error resume next
Set RegWsh = CreateObject(”WScript.Shell”)
hkey_key=”\header”
RegWsh.RegWrite hkey_root hkey_path hkey_key,”&b页&p/&P”
hkey_key=”\footer”
RegWsh.RegWrite hkey_root hkey_path hkey_key,”"
end function
</script>

<%
kdname1=trim(request(”kdname1″))
kdname2=trim(request(”kdname2″))
keyword1=trim(request(”keyword1″))
keyword2=trim(request(”keyword2″))

if keyword1<>”" then
today=keyword1
else
if kdname1=”" then
today=year(date())&”-”&month(date())
else
today=kdname1&”至”&kdname2
end if
end if
%>
<table border=”0″ cellspacing=”0″ cellpadding=”0″ align=”center” width=”740″ height=”30″>
<tr>
<td align=”center”>销售利润汇总报表</td>
</tr>
</table>

<%
strSQL=”select autoid,sellautoid,productxili,productname,productsize,productnum,productdan,productjia,chaoshi,tiaoma,youhui,fukuan,moncount1,gongshang,lirun1,username,indate,fudate from sell where officename=’”&trim(request.cookies(”Myoffice”))&”‘ and monthjie=’0′ and (year(indate)=year(getdate()) and month(indate)=month(getdate())) and zhuofei is null order by autoid desc”
set rs1=server.createobject(”adodb.recordset”)
rs1.open strSQL,conn,1,1
%>
<table border=”1″ cellspacing=”0″ cellpadding=”0″ align=”center” style=”border-collapse: collapse” bordercolor=”#000000″ width=”740″>
<tr>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”70″ >销售单号</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”168″ >商品名称(规格)</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”121″ >客户</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”30″ >数量</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”24″ >单位</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”50″ >销售价</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”23″ >折%</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”52″ >进货价</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”55″ >小计</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”45″ >利润</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”25″ >付款</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”61″ >销售日期</td>
</tr>
</table>
<%
moncount2=0
moncount5=0
Do while not rs1.eof
%>
<table border=”1″ cellpadding=”0″ cellspacing=”0″ width=”740″ align=”center” style=”border-collapse:collapse; font-size:10pt;color:#000000″ bordercolor=”#000000″>
<%
for i=1 to pagenum
if not rs1.eof then
if trim(rs1(”fukuan”))=”欠款” then
moncount6=Csng(rs1(”lirun1″))
moncount5=moncount5 moncount6
else
moncount3=Csng(rs1(”lirun1″))
moncount2=moncount2 moncount3
end if
%>
<tr>
<td height=”18″ width=”70″> <%=rs1(”sellautoid”)%></td>
<td height=”18″ width=”168″><%=Decode(rs1(”productname”))%> <%=rs1(”productsize”)%></td>
<td height=”18″ width=”121″><%=left(rs1(”gongshang”),9)%></td>
<td height=”18″ width=”30″ align=”center”><%=rs1(”productnum”)%></td>
<td height=”18″ width=”24″ align=”center”><%=rs1(”productdan”)%></td>
<td height=”18″ width=”50″ align=”right”><%=formatNumber(rs1(”chaoshi”),varnum,-1)%></td>
<td height=”18″ width=”23″ align=”center”><%=rs1(”youhui”)%></td>
<td height=”18″ width=”52″ align=”right”><%=formatNumber(rs1(”productjia”),varnum,-1)%></td>
<td height=”18″ width=”55″ align=”right”><%=formatNumber(rs1(”moncount1″),varnum,-1)%></td>
<td height=”18″ width=”45″ align=”right”><%=formatNumber(rs1(”lirun1″),varnum,-1)%></td>
<td align=”center” height=”18″ width=”25″><%if trim(rs1(”fukuan”))=”欠款” then%><font color=blue><%=rs1(”fukuan”)%></font><%else%><%=rs1(”fukuan”)%><%end if%></td>
<td height=”18″ width=”61″><%=rs1(”indate”)%></td>
</tr>
<%
rs1.movenext
end if
next
%>
</table>
<%
if not rs1.eof and i=pagenum 1 then ‘添加分页标记
%>
<div class=”break”> </div>
<table border=”0″ cellpadding=”0″ cellspacing=”0″ width=”740″ height=”12″ align=”center”><tr><td height=”12″></td></tr></table>
<table border=”1″ cellspacing=”0″ cellpadding=”0″ align=”center” width=”740″ style=”border-collapse: collapse” bordercolor=”#000000″>
<tr>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”70″ >销售单号</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”168″ >商品名称(规格)</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”121″ >客户</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”30″ >数量</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”24″ >单位</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”50″ >销售价</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”23″ >折%</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”52″ >进货价</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”55″ >小计</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”45″ >利润</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”25″ >付款</td>
<td align=”center” height=”20″ bgcolor=”#BDCBEE” width=”61″ >销售日期</td>
</tr>
<%
end if
loop
rs1.close
set rs1=nothing
%>
</table>
<table border=”1″ cellpadding=”0″ cellspacing=”0″ width=”740″ height=”20″ align=”center” style=”border-collapse: collapse” bordercolor=”#000000″>
<tr>
<td><font color=”#FF0000″><b>现金利润:</b></font><b><%=formatNumber(moncount2,varnum,-1)%></b> <%if moncount5<>”" then%><b><font color=”#FF0000″>欠款利润</font>:<%=formatNumber(moncount5,varnum,-1)%></b><%end if%> <%if moncount5<>”" then%><b><font color=”#FF0000″>毛利合计:</font><%=formatNumber(moncount5 moncount2,varnum,-1)%></b><%end if%></td>
</tr>
</table>
<%
end if
conn.close
set conn=nothing
%>

</BODY>
</HTML>

巧用FileSystem组件实现WEB应用中的本地特定打印

星期一, 06月 2nd, 2008

1、引言

随着Internetde飞速发展,许多企业都纷纷开发基于WEBde业务应用系统.一般情况下,基于WEBde业务应用都采用三层或三层以上de结构,前台即客户端是普通deWEB浏览器,中间业务逻辑应用层存放于WEB服务器上,由WEB服务器上de服务构件访问后台数据库.为了业务系统与Internetde互联,WEB服务器和数据库系统均托管在IDC(互联网数据中心),因而需要从托管在 IDCde服务器上提取业务数据在本地打印输出.在客户端浏览器只能打印简单deHTML页面de情况下,要求更为强大灵活de打印输出功能;通过采用相关de技术对浏览器de功能进行扩展,能够完成更复杂de数据打印de任务,如收条、回执等.笔者曾经为一网络教育公司开发教育管理软件时采用FileSystem组件实现在本地打印三联收款凭证和准考证等de功能.

2、FileSystem组件使用简介

FileSystem组件其实是一个ActiveX控件,其存在于WINDOWS平台中(不管是Win98、Win2000,还是NT环境都包含该组件.),CLSID:0D43FE01-11CF-8940-00A0C9054228.熟悉ASP编程人可能都使用过FileSystem组件,一般情况下该组件用在服务器端,用来对文本文件、文件夹及驱动器进行访问和控制.可以通过两种方法创建FileSystem组件de对象实例:

1、使用ASPde内置对象SERVERde方法Server.CreateObject来创建服务器端对象de实例.

2、使用Vbscript脚本函数CreateObject来创建客户端对象de实例(也可以在服务器端用该方法,但有时会出错,一般不使用;).

FileSystem组件对文本文件进行操作有如下方法:

CreateTextFile(Filename,[Overwrite if exists],[Unicode/ASCII]) //创建文本文件

OpenTextFile(Filename,[Input/output mode],[Create if not exists],[Format]) //打开文本文件

CopyFile(Filename1,Filename2,[Overwrite]) //拷贝文本文件

MoveFile (Filename1,Filename2) //移动文本文件

DeleteFile (Filename) //删除文本文件

GetFile (Filename) //获得文本文件

FileExists (Filename) //判断文本文件是否存在

FileSystem组件对文件夹和驱动器也有和文本文件相类似de方法,在此不一一赘述.

其实,FileSystem组件也可使用于客户端,采用创建FileSystem组件对象实例de第二种方法即可以使用该组件对客户端系统de文本文件、文件夹及驱动器进行访问和控制.因为微软平台带有FileSystem组件,所以对于微软平台de客户端则不需要从远程服务器下载,其在安装操作系统时会自己安装并注册;而对于其他平台de客户端则须安装插件并下载FileSystem组件.

3、本地端特定打印de实现机制

利用FileSystem组件实现本地端特定打印de过程如图所示:

<IMG SRC="http://www.computerworld.com.cn/htm/app/aprog/01_9_14_5.jpg" border=0>

1、客户端向WEB服务器发送数据请求;

2、WEB服务器根据业务处理逻辑与后台数据库进行交互,取得所需数据形成HTML页面,并附带有FileSystem组件对象deVBScript脚本,向客户端浏览器发回HTML页面;

3、在客户端运行脚本,脚本取得HTML页面构件de数据,建立与打印机de连接,向打印机输出打印数据.

前两步与一般deWEB应用没有什么区别,需要说明de是第三步:首先使用Vbscript脚本函数CreateObject来创建FileSystem组件对象de实例;然后调用该实例创建文本文件deCreateTextFile方法,把打印端口LPT1或LPT2作为文件名参数,并将可覆盖文件参数设为TRUE,建立与打印机de连接;调用获得de打印机文本文件流实例de写文本方法WriteLine向打印机输出打印de内容.

4、程序示例

客户端脚本:

<script Language=VBScript>

function print_onclick //打印函数

dim label

label=document.printinfo.label.value //获得HTML页面de数据

set objfs=CreateObject("Scripting.FileSystemObject") //创建FileSystem组件对象de实例

set objprinter=objfs.CreateTextFile ("LPT1:",true) //建立与打印机de连接

objprinter.Writeline("__________________________________") //输出打印de内容

objprinter.Writeline("| |")

objprinter.Writeline("| 您打印de数据是:"&label& " |”)

objprinter.Writeline("| |")

objprinter.Writeline("|_________________________________|")

objprinter.close //断开与打印机de连接

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=<%=………%>> //保存服务器端传来de数据

………

</HTML>

5、说明及结论

1、使用此方法实现打印任务之前,必须调节浏览器de安全设置;在Internet选项上安全级别降低,对自己de站点充分信任,可以通过浏览器执行脚本访问本机资源.

2、本方法可以实现在局域网de打印机上打印远程服务器de数据,只须用局域网上打印机注册在本地de端口代替本地打印机端口作为创建文本文件deCreateTextFile方法de参数即可.

3、本方法可以实现根据用户选择打印de宽度、高度和数量及内容,并可重复打印.

4、不需要编写ActiveX控件即可以完成特定de打印功能,实现较为简单.

5、不足之处是需要调节浏览器de安全设置,且不支持图片和特殊字体de打印.