JS实现打印的方式

来源:互联网 发布:毕业设计的要求及数据 编辑:程序博客网 时间:2024/06/06 23:59

目前正在做浏览器端采用js方式实现打印这么一个功能,JS打印实现的方法很多,但是兼容各个浏览器实现打印预览的功能有些棘手,现将实现的内容及遇到的问题记录下来,希望有大牛看到所提的问题后可以给予解答,在此感谢啦。

1.JS实现打印的方式

方式一:window.print()

window.print();会弹出打印对话框,打印的是window.document.body.innerHTML中的内容,下面是从网上摘到的一个局部打印的例子,该例子的不足是打印会修改页面的内容

[javascript] view plain copy
print?
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>局部打印案例</title>  
  6. <script type="text/javascript">     
  7.     function doPrint() {      
  8.         bdhtml=window.document.body.innerHTML;      
  9.         sprnstr="<!--startprint-->";      
  10.         eprnstr="<!--endprint-->";      
  11.         prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);      
  12.         prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));      
  13.         window.document.body.innerHTML=prnhtml;   
  14.         window.print();      
  15. }      
  16. </script>  
  17. </head>  
  18.   
  19. <body>  
  20. <p>1不需要打印的地方</p>  
  21. <p>2这里不要打印啊</p>  
  22. <!--startprint--><!--注意要加上html里star和end的这两个标记-->  
  23. <h1>打印标题</h1>  
  24. <p>打印内容~~</p>  
  25. <!--endprint-->  
  26. <button type="button" onclick="doPrint()">打印</button>  
  27. <p>不打印的地方啊哈哈哈哈</p>  
  28. <p>2</p>  
  29. </body>  
  30. </html>  

方式二:使用html 标签<object>引入Webbrowser控件

这种方式是其只兼容IE,其他浏览器不可使用,同时IE10以下的浏览器才可以使用,调用方式如下:

[javascript] view plain copy
print?
  1. <body>  
  2.     <object id="WebBrowser" classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0" width="0"> </object>  
  3. </body>  
  4. <script>  
  5.     WebBrowser.ExecWB(1,1)  //打开   
  6.     WebBrowser.ExecWB(2,1)  //关闭现在所有的IE窗口,并打开一个新窗口   
  7.     WebBrowser.ExecWB(4,1)  //保存网页  
  8.     //--------------- 常用 ---------------      
  9.     WebBrowser.ExecWB(6,1)  //打印   
  10.     WebBrowser.ExecWB(7,1)  //打印预览   
  11.     WebBrowser.ExecWB(8,1)  //打印页面设置   
  12.     //-------------------------------------   
  13.     WebBrowser.ExecWB(10,1) //查看页面属性   
  14.     WebBrowser.ExecWB(15,1) //撤销   
  15.     WebBrowser.ExecWB(17,1) //全选   
  16.     WebBrowser.ExecWB(22,1) //刷新   
  17.     WebBrowser.ExecWB(45,1) //关闭窗体无提示  
  18. </script>  

这种方式优势是在IE下可以弹出打印预览,这是打印很人性化的功能,但是遗憾的是高版本的IE浏览器不支持WebBrowser了

方式三:采用document.execCommand(”print”)

该方式也兼容各个版本的浏览器,同window.print()一样,其启动的是打印对话框,chrome的打印对话框自带预览功能,但是IE、火狐仅仅只弹出打印设置对话框,没有预览功能。

方式四:采用JQuery插件

使用jQuery浏览插件可以很方便的进行局部打印,常用的插件有:

1)jquery.print.js 下载地址:https://github.com/DoersGuild/jQuery.print

2)jquery.print-preview.js 下载地址:https://github.com/etimbo/jquery-print-preview-plugin

这两种方式使用都很简单,1)通过$("#id").print(/*options*/);调用;2)通过$('#id').printArea(/*options*/); 其中的option可选项可以在下载地址下载下来后看示例代码,一般options不用传即可,示例代码如下:

[html] view plain copy
print?
  1. <html>  
  2. <head>  
  3. <meta http-equiv=Content-Type content="text/html; charset=utf-8">  
  4. <title>JQuery打印</title>  
  5. <script type="text/javascript" src="./js/jquery-3.1.1.min.js"></script>  
  6. <script language="javascript" src="./js/jquery.print.js"></script>  
  7. </head>  
  8.   
  9. <body style='margin:0 auto;text-align:center;'>  
  10. <button id='button_print' name='button_print' onclick="javascript:printit()>打印</button>  
  11. <div id='ganburenmianbaio' class=WordSection1 style='width:750px;margin:0 auto;text-align:center;vertical-align: middle;'>  
  12. </div>  
  13. </body>  
  14. <script language="javascript">  
  15. function printit(){  
  16.     $("#ganburenmianbaio").print({iframe:true,prepend:'<br/>'});  
  17. }  
  18. </script>  
  19. </html>  

方式五:采用浏览器打印第三方插件

该方式需要用户浏览器安装相关的第三方插件,用户体验不好,故在此不细述了。

2.打印预览

chrome浏览器、win10自带的IE浏览器 调用打印弹出的打印设置界面中包含打印预览部分,故其通过上面的打印函数的调用即可实现。

IE9以后的版本、火狐不支持webbrowser控件了,JS调用不了浏览器的打印预览的功能,我们只能用iframe模拟打印预览的对话框,将需要打印的内容显示在该对话框中,然后在调用打印的功能实现打印。

1)jquery打印预览插件

jquery.print-preview.js 下载地址:https://github.com/etimbo/jquery-print-preview-plugin

其实现的效果如下图(其自动的示例代码)


2)webbrowser控件打印预览

IE8及以下版本可以调用WebBrowser.ExecWB(7,1) 函数弹出浏览器的打印预览对话框,采用该函数的好处是 用户可以在打印预览对话框中 调整页边距、页眉、页脚;

下面贴出的是设置页边距、页眉、页脚的JS代码

[javascript] view plain copy
print?
  1. //取得页面打印设置的原参数数据     
  2. function PageSetup_temp(){     
  3.     try     
  4.     {     
  5.        var Wsh=new ActiveXObject("WScript.Shell");     
  6.        HKEY_Key="header";     
  7.         //取得页眉默认值     
  8.        head = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key);     
  9.        HKEY_Key="footer";     
  10.         //取得页脚默认值     
  11.        foot = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key);     
  12.        HKEY_Key="margin_bottom";     
  13.         //取得下页边距     
  14.        bottom = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key);     
  15.        HKEY_Key="margin_left";     
  16.         //取得左页边距     
  17.        left = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key);     
  18.        HKEY_Key="margin_right";     
  19.         //取得右页边距     
  20.        right = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key);     
  21.        HKEY_Key="margin_top";     
  22.         //取得上页边距     
  23.        top = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key);     
  24.     }     
  25.     catch(e){     
  26.          //alert("不允许ActiveX控件");     
  27.     }     
  28. }     
  29.     
  30. //设置网页打印的页眉页脚和页边距,注册表里的单位是英寸,打印设置中是毫米,1英寸=25.4毫米     
  31. function PageSetup_Null(){     
  32.     try     
  33.     {     
  34.        var Wsh=new ActiveXObject("WScript.Shell");     
  35.        HKEY_Key="header";     
  36.         //设置页眉(为空)     
  37.        Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");     
  38.        HKEY_Key="footer";     
  39.         //设置页脚(为空)     
  40.        Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");     
  41.        HKEY_Key="margin_bottom";     
  42.         //设置下页边距(0)     
  43.        Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0.6");     
  44.        HKEY_Key="margin_left";     
  45.         //设置左页边距(0)     
  46.        Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0.3");     
  47.        HKEY_Key="margin_right";     
  48.         //设置右页边距(0)     
  49.        Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0.3");     
  50.        HKEY_Key="margin_top";     
  51.         //设置上页边距(8)     
  52.        Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0.6");     
  53.     }     
  54.     catch(e){     
  55.          //alert("不允许ActiveX控件");     
  56.     }     
  57. }     
  58.   
  59. //设置网页打印的页眉页脚和页边距为默认值     
  60. function PageSetup_Default(){        
  61.     try     
  62.     {     
  63.        var Wsh=new ActiveXObject("WScript.Shell");     
  64.        HKEY_Key="header";     
  65.        HKEY_Key="header";     
  66.         //还原页眉     
  67.        Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,head);     
  68.        HKEY_Key="footer";     
  69.         //还原页脚     
  70.        Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,foot);     
  71.        HKEY_Key="margin_bottom";     
  72.         //还原下页边距     
  73.        Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,bottom);     
  74.        HKEY_Key="margin_left";     
  75.         //还原左页边距     
  76.        Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,left);     
  77.        HKEY_Key="margin_right";     
  78.         //还原右页边距     
  79.        Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,right);     
  80.        HKEY_Key="margin_top";     
  81.         //还原上页边距     
  82.        Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,top);     
  83.     }     
  84.     catch(e){     
  85.          //alert("不允许ActiveX控件");     
  86.     }     
  87. }  
使用该函数,会弹出

通过网页修改activex安全设置,添加信任站点,禁用该弹出窗口提示,代码如下:

[javascript] view plain copy
print?
  1. function activeXControl(){  
  2.         try{  
  3.         var WshShell=new ActiveXObject("WScript.Shell");  
  4.           
  5.         //添加信任站点(http://127.0.0.1)  
  6.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Ranges\\Range100\\","");  
  7.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Ranges\\Range100\\:Range","127.0.0.1");  
  8.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Ranges\\Range100\\http","2","REG_DWORD");  
  9.   
  10.         //修改IE ActiveX安全设置: 1本地Intranet区域  
  11.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\1\\1001","0","REG_DWORD");  
  12.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\1\\1004","0","REG_DWORD");  
  13.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\1\\1200","0","REG_DWORD");  
  14.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\1\\1201","0","REG_DWORD");  
  15.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\1\\1405","0","REG_DWORD");  
  16.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\1\\2201","0","REG_DWORD");  
  17.   
  18.         //修改IE ActiveX安全设置:2受信任的站点区域  
  19.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\2\\1001","0","REG_DWORD");  
  20.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\2\\1004","0","REG_DWORD");  
  21.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\2\\1200","0","REG_DWORD");  
  22.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\2\\1201","0","REG_DWORD");  
  23.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\2\\1405","0","REG_DWORD");  
  24.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\2\\2201","0","REG_DWORD");  
  25.           
  26.         //修改IE ActiveX安全设置:3Internet区域  
  27.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3\\1001","0","REG_DWORD");  
  28.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3\\1004","0","REG_DWORD");  
  29.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3\\1200","0","REG_DWORD");  
  30.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3\\1201","0","REG_DWORD");  
  31.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3\\1405","0","REG_DWORD");  
  32.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3\\2201","0","REG_DWORD");  
  33.   
  34.         //禁用Winxp弹出窗口阻止程序  
  35.         WshShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\New Windows\\PopupMgr","no");  
  36.     }catch(e){     
  37.          //alert("不允许ActiveX控件");     
  38.     }     
  39. }  

3.问题

1)网页修改activex安全设置该段代码也是必须在启用ActiveX的条件下调用成功,是需要用户在Internet的配置项中设置的(如下图),如何才能自动启用该插件?


2) chrome、火狐如何通过JS设置页边距、页眉、页脚?

3) IE高版本浏览器、火狐如何通过JS弹出浏览器自己的打印预览?



原创粉丝点击