javascript and printer

1. 直接调用 window.print();

 

 

2 . 可以只打印部分页面 (个人最喜欢)

页面内容 :

<html>
...
<body>

    <!-- some content -->

    <!--startprint-->
        print context
    <!--endprint-->

    <!-- some content -->

</body>
</html>

 

javascript代码 :

<script type="text/javascript">
function printView(){
    bdhtml=window.document.body.innerHTML;
    sprnstr="<!--startprint-->"; // 这里要和页面标记完全统一
    eprnstr="<!--endprint-->"; // 同上...
    prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
    prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
    window.document.body.innerHTML=prnhtml;
    window.print();
    window.document.body.innerHTML=bdhtml;
}

</script>

 

 

 

3. IEWebBrowser组件 :

<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>  
   1. <input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=打开>
   2. <input name=Button onClick=document.all.WebBrowser.ExecWB(2,1) type=button value=关闭所有>
   3. <input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) type=button value=另存为>  
   4. <input name=Button onClick=document.all.WebBrowser.ExecWB(6,1) type=button value=打印>
   5. <input name=Button onClick=document.all.WebBrowser.ExecWB(6,6) type=button value=直接打印>
   6. <input name=Button onClick=document.all.WebBrowser.ExecWB(7,1) type=button value=打印预览>
   7. <input name=Button onClick=document.all.WebBrowser.ExecWB(8,1) type=button value=页面设置>
   8. <input name=Button onClick=document.all.WebBrowser.ExecWB(10,1) type=button value=属性>
9. <input name=Button onClick=document.all.WebBrowser.ExecWB(17,1) type=button value=全选>
10. <input name=Button onClick=document.all.WebBrowser.ExecWB(22,1) type=button value=刷新>
11. <input name=Button onClick=document.all.WebBrowser.ExecWB(45,1) type=button value=关闭>

 

 

4. 使用ScriptX.cab控件

1.下载ScriptX.cab控件

官网http://www.meadroid.com/scriptx/index.asp

2.使用object元素,修改codebase,classid的值

这里调用控件ScriptX.cab
Java代码 复制代码

<OBJECT id="factory" style="DISPLAY: none" codeBase="${rootUrl}js/smsx.cab#VVersion=6,3,435,20" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext></OBJECT>

这段代码用来加载cab文件,clsid和codebase必须要和你下载的cab中的信息对应,否则组件会加载错误,这两项其实不难找,只要你用winrar打开你下载的cab文件,然后找到扩展名是.inf的文件,然后打开之,就能看到了。

3.调用控件脚本

Print.js文件
Java代码 复制代码

  1. function setPrintBase(headerText,footerText,rootUrl) {
   2.  
   3.     // -- advanced features ,未曾使用过,有待确认。
   4.  
   5.         //factory.printing.SetMarginMeasure(2); // measure margins in inches
   6.  
   7.         //factory.SetPageRange(false, 1, 3);// need pages from 1 to 3
   8.  
   9.         //factory.printing.printer = "HP DeskJet 870C";
10.  
11.         //factory.printing.copies = 2;
12.  
13.         //factory.printing.collate = true;
14.  
15.         //factory.printing.paperSize = "A4";
16.  
17.         //factory.printing.paperSource = "Manual feed"
18.  
19.     var header = (headerText==null||headerText=="")?'默认页眉':headerText;
20.  
21.     var footer = (footerText==null||footerText=="")?'默认页角':footerText;
22.  
23.   factory.printing.header = "&b"+header+"&b" ;
24.  
25.   factory.printing.footer = "&b"+footer;
26.  
27.   factory.printing.portrait = true;
28.  
29.   factory.printing.leftMargin =10.00;
30.  
31.   factory.printing.topMargin =10.00;
32.  
33.   factory.printing.rightMargin =10.00;
34.  
35.   factory.printing.bottomMargin =10.00;
36.  
37. } 

 

 测试代码 :

 

<%@ page contentType="text/html;charset=GBK"%>

<html>
<head>
<meta http-equiv="imagetoolbar" content="no">
<script language="javascript" src="print.js"></script>
<style media="print">
.Noprint   {DISPLAY:   none;}
</style>
<title>打印测试</title>
</head>
<OBJECT id="factory" style="DISPLAY: none" codeBase="smsx.cab#VVersion=6,3,435,20" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext></OBJECT>

<script defer>
function window.onload() {  
setPrintBase('页眉','页脚');
}
</script>
<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0">
<center class="Noprint">
<input type=button value="打印" οnclick="factory.printing.Print(true)">
<input type=button value="页面设置" οnclick="factory.printing.PageSetup()">
<input type=button value="打印预览" οnclick="factory.printing.Preview()">          
<input type="button" value="关闭" οnclick="window.close();">
</center>
   <center>
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
          <tr><td align="center"><b>内容</b></td></tr>
       </table>
    </center>
</body>
</html>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值