总结JS打印方法

简介: 最近做项目用到订单打印,看到一个不错的文章,转来收藏一下,呵呵 《转》http://ltc603.javaeye.com/blog/123191 对JS的打印方法总结一下,方便日后查阅。 一.用JS自带函数打印 直接调用 Java代码 复制代码    1.
最近做项目用到订单打印,看到一个不错的文章,转来收藏一下,呵呵

《转》http://ltc603.javaeye.com/blog/123191

对JS的打印方法总结一下,方便日后查阅。

一.用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

.NET环境下的
http://blog.csdn.net/flygoldfish/archive/2004/08/17/77208.aspx
目录
相关文章
|
2月前
|
JavaScript 前端开发 程序员
前端原生Js批量修改页面元素属性的2个方法
原生 Js 的 getElementsByClassName 和 querySelectorAll 都能获取批量的页面元素,但是它们之间有些细微的差别,稍不注意,就很容易弄错!
|
2月前
|
Web App开发 JavaScript 前端开发
如何确保 Math 对象的方法在不同的 JavaScript 环境中具有一致的精度?
【10月更文挑战第29天】通过遵循标准和最佳实践、采用固定精度计算、进行全面的测试与验证、避免隐式类型转换以及持续关注和更新等方法,可以在很大程度上确保Math对象的方法在不同的JavaScript环境中具有一致的精度,从而提高代码的可靠性和可移植性。
|
3月前
|
缓存 监控 前端开发
JavaScript 实现大文件上传的方法
【10月更文挑战第17天】通过以上步骤和方法,我们可以实现较为可靠和高效的大文件上传功能。当然,具体的实现方式还需要根据实际的应用场景和服务器要求进行调整和优化。
|
11天前
|
JavaScript 前端开发 开发者
JavaScript字符串的常用方法
在JavaScript中,字符串处理是一个非常常见的任务。JavaScript提供了丰富的字符串操作方法,使开发者能够高效地处理和操作字符串。本文将详细介绍JavaScript字符串的常用方法,并提供示例代码以便更好地理解和应用这些方法。
37 13
|
2月前
|
监控 JavaScript Java
Node.js中内存泄漏的检测方法
检测内存泄漏需要综合运用多种方法,并结合实际的应用场景和代码特点进行分析。及时发现和解决内存泄漏问题,可以提高应用的稳定性和性能,避免潜在的风险和故障。同时,不断学习和掌握内存管理的知识,也是有效预防内存泄漏的重要途径。
185 52
|
2月前
|
JavaScript 前端开发 索引
js中DOM的基础方法
【10月更文挑战第31天】这些DOM基础方法是操作网页文档结构和实现交互效果的重要工具,通过它们可以动态地改变页面的内容、样式和行为,为用户提供丰富的交互体验。
|
2月前
|
缓存 JavaScript UED
js中BOM中的方法
【10月更文挑战第31天】
|
2月前
|
缓存 JavaScript 前端开发
JavaScript 与 DOM 交互的基础及进阶技巧,涵盖 DOM 获取、修改、创建、删除元素的方法,事件处理,性能优化及与其他前端技术的结合,助你构建动态交互的网页应用
本文深入讲解了 JavaScript 与 DOM 交互的基础及进阶技巧,涵盖 DOM 获取、修改、创建、删除元素的方法,事件处理,性能优化及与其他前端技术的结合,助你构建动态交互的网页应用。
68 5
|
2月前
|
JavaScript 前端开发
js中的bind,call,apply方法的区别以及用法
JavaScript中,`bind`、`call`和`apply`均可改变函数的`this`指向并传递参数。其中,`bind`返回一个新函数,不立即执行;`call`和`apply`则立即执行,且`apply`的参数以数组形式传递。三者在改变`this`指向及传参上功能相似,但在执行时机和参数传递方式上有所区别。
35 1
|
2月前
|
JavaScript 前端开发
.js方法参数argument
【10月更文挑战第26天】`arguments` 对象为JavaScript函数提供了一种灵活处理参数的方式,能够满足各种不同的参数传递和处理需求,在实际开发中具有广泛的应用价值。
56 7