js打印

本文详细介绍三种网页打印方式:使用JS自带函数实现简单打印;利用IEWebBrowser组件提供更多打印选项;通过ScriptX.cab控件实现高级打印功能,如页眉页脚设置、页面方向调整等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.用JS自带函数打印

直接调用
Java代码 复制代码

   1. <a href="javascript:window.print();">打印</a>

<a href="javascript:window.print();">打印</a>



二.IEWebBrowser组件

介绍

http://support.microsoft.com/default.aspx?scid=kb%3BEN-US%3BQ267240#top
http://support.microsoft.com/kb/q247671/#appliesto

Java代码 复制代码

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

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



三.使用ScriptX.cab控件

1.下载ScriptX.cab控件

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

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

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

   1. <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>

<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. }

function setPrintBase(headerText,footerText,rootUrl) {

    // -- 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"

    var header = (headerText==null||headerText=="")?'默认页眉':headerText;

    var footer = (footerText==null||footerText=="")?'默认页角':footerText;

factory.printing.header = "&b"+header+"&b" ;

factory.printing.footer = "&b"+footer;

factory.printing.portrait = true;

factory.printing.leftMargin =10.00;

factory.printing.topMargin =10.00;

factory.printing.rightMargin =10.00;

factory.printing.bottomMargin =10.00;

}




例子
Java代码 复制代码

   1. <%@ page contentType="text/html;charset=GBK"%>
   2.  
   3. <html>
   4. <head>
   5. <meta http-equiv="imagetoolbar" content="no">
   6. <script language="javascript" src="print.js"></script>
   7. <style media="print">
   8. .Noprint   {DISPLAY:   none;}
   9. </style>
10. <title>打印测试</title>
11. </head>
12. <OBJECT id="factory" style="DISPLAY: none" codeBase="smsx.cab#VVersion=6,3,435,20" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext></OBJECT>
13.  
14. <script defer>
15. function window.onload() {    
16. setPrintBase('页眉','页脚');
17. }
18. </script>
19. <body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0">
20. <center class="Noprint">
21. <input type=button value="打印" onclick="factory.printing.Print(true)">  
22. <input type=button value="页面设置" onclick="factory.printing.PageSetup()">  
23. <input type=button value="打印预览" onclick="factory.printing.Preview()">            
24. <input type="button" value="关闭" onclick="window.close();">
25. </center>
26.    <center>
27.       <table width="100%" border="0" cellpadding="0" cellspacing="0">
28.           <tr><td align="center"><b>内容</b></td></tr>
29.        </table>
30.     </center>
31. </body>
32. </html>

<%@ 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="打印" onclick="factory.printing.Print(true)">
<input type=button value="页面设置" onclick="factory.printing.PageSetup()">
<input type=button value="打印预览" onclick="factory.printing.Preview()">          
<input type="button" value="关闭" onclick="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>



四.对比

1.Window.print调用方便,但功能简单

2.功能更强大,但使用IEWebBrowser有时会报JS没有权限的错误。

3.ScriptX控件功能也比较强大,目前在使用这种方式。

这里的也不错啊,有打印到 word的和excel

http://hi.baidu.com/afei0211/blog/item/20523955ca8bebc2b745ae98.html

http://hi.baidu.com/hongz1125/blog/item/0152bcfd84ce1e1008244d5c.html

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值