总结JS打印方法
来源:互联网 发布:淘宝旺季是什么时候 编辑:程序博客网 时间:2024/05/21 13:55
一.用JS自带函数打印
3.ScriptX控件功能也比较强大,目前在使用这种方式。
直接调用
Java代码
- <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代码
- <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代码
- <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代码
- 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代码
- <%@ 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没有权限的错误。
0 0
- 总结JS打印方法
- 总结JS打印方法
- JS打印方法总结
- 总结JS打印方法
- 总结JS打印方法
- 总结JS打印方法
- 对JS的打印方法总结
- js 打印总结
- 堆栈打印方法总结
- HTML打印方法总结
- JS调用finereport打印方法
- JS实现打印两方法
- js打印对象的方法
- js 打印出对象方法
- JS/Jquery_实现打印页面指定div的方法_个人总结
- 常用js方法总结
- 常用js方法总结
- js 常用方法总结
- How to collect the full path name of the files in ASM diskgroups (文档 ID 888943.1)
- select2使用demo
- 黑马程序员__异常
- js获取DropDownList的选中项的value值
- 如何隐藏input标签的样式
- 总结JS打印方法
- 推荐5款超实用的.NET性能分析工具
- 2015/04/30CSS学习记录
- Lua导入第三方库或目录
- 使用Rails实现文件上传
- CSS小笔记
- 欢迎使用CSDN-markdown编辑器
- 数据库主键选取策略
- Ubuntu环境SSH、PuTTY、WinSCP搭建配置