闲来无事,发个最近在用的Web打印方案.
本方法其实也是用模板来实现,只不过这个模板是一个Xml的配置文件.
基本思路:
先定义一个Xml配置文件,文件包括单据的一些基本信息。然后在需要打印
的时候将要打印的填入这个Xml文件,并生成一个新的Xml文件。最后在客户端使用
JavaScript脚本读取生成的Xml文件,并打印。
优点:
减轻Web服务器的负担。
代码重用。
缺点:
不支持打印预览、分页。
需要为每个单据写一个Xml配置文件,配置文件格式必须一致。
详细代码:
Xml文格式:
<?xml version="1.0" encoding="utf-8" ?>
<!--
文件属性说明:
<Bills>
<Page Width=页面宽 Height=页面高 />
<style name=样式名称 caption=值 top=距页面顶端的距离 left=距页面左边的距离 font=字体 size=字体大小 color = 字体颜色 bold = 是否粗体 />
</Bills>
-->
<Bills>
<Page Width ="200" Height="300" />
<style name="标题" caption="" top="20" left="50" font="宋体" size="9" color="Black" bold="false"/>
</Bills>
JavaScript代码:
<script language=javascript>
function Print()
{
rootNodes = xmlFile.documentElement; //xmlFile为<xml></xml>控件的ID
//改变窗体大小
ResizePage(rootNodes);
//输出打印内容
DrawContents(rootNodes);
//打印
Window.print();
}
function ResizePage(obj)
{
var node = obj.selectNodes(“//Page”);
width = node.item.getAttributes[“Width”]; //注意大小写
height = node.item.getAttributes[“Height”];
window.resizeTo(width, height);
//改变窗体位置
window.moveTo(window.screen.width/2 – width, window.screen.height/2-height);
}
function DrawContents(obj)
{
var nodes = obj.selectNodes(“//style”);
for(i=0;i<nodes.item.length;i )
{
caption = nodes.item(i).getAttribute[“caption”];
top = nodes.item(i).getAttribute[“top”];
left = nodes.item(i).getAttribute[“left”];
size = nodes.item(i).getAttribute[“size”];
color = nodes.item(i).getAttribute[“color”];
font = nodes.item(i).getAttribute[“font”];
bold = nodes.item(i).getAttribute[“bold”];
document.body.appendChild(CreateLabel(top,left,caption,font,size,color,bold));
}
}
function CreateLabel(top,left,caption,font,size,color,bold)
{
var oLab = document.createElementByTagName("Label");
oLab.style.position = "absolute";
oLab.style.top = top;
oLab.style.left = left;
oLab.innerText = caption;
oLab.style.fontFamily = font;
oLab.style.fontSize = size;
oLab.style.color = color;
oLab.style.fontWeight = bold;
return oLab;
}
</script>
问题点数:20、回复次数:8Top
1 楼cccclb(磊冰Ben)回复于 2005-08-02 23:26:34 得分 4
ls_return=ls_column+" = '"+String(gs_use_id)+"'"Top
2 楼ztchen(感谢csdn,回馈csdn,每周答几贴)回复于 2005-08-02 23:44:10 得分 4
不错,关注Top
3 楼gaofeng2000(高老师)回复于 2005-08-03 00:01:59 得分 3
upTop
4 楼color2002(网络资源库)回复于 2005-08-03 09:41:37 得分 3
不支持打印预览、分页
这两点要命Top
5 楼zhangA()回复于 2005-08-03 09:49:54 得分 3
markTop
6 楼yangkunjie(杨七郎)(dephi--c#)回复于 2005-08-03 10:12:48 得分 3
应该继续努力,支持打印分页Top
7 楼canghaiyisujsg(沧海一粟)回复于 2005-09-15 11:31:48 得分 0
还可以啊!!Top
8 楼anqiant(许留心)回复于 2005-09-15 15:01:45 得分 0
(1)Top




