网页打印【主要是转贴】

    技术2022-05-11  84

     WEB打印大全 

    1、控制"纵打"、 横打”和“页面的边距。 

    < html > < head > < script  language ="javascript" >    function printsetup(){     // 打印页面设置     try{                   document.all.WebBrowser.ExecWB(8,1);                 }               catch(e){                  alert("您的浏览器不支持此功能,需要5.5以上版本");               }    }   function printpreview(){     // 打印页面预览          WebBrowser.execwb(7,1);    }   function printit()   {     if (confirm(''确定打印吗?'')) {      WebBrowser.execwb(6,6)     }   }  </ script >   </ head >   < body >   < OBJECT  classid ="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"  height =0  id =WebBrowser name =WebBrowser width =0 ></ OBJECT >   < input  type =button  name =button_print  value ="打印"  onclick ="javascript:printit()" >   < input  type =button name=button_setup  value ="打印页面设置"  onclick ="javascript:printsetup();" >   < input  type =button name=button_show  value ="打印预览"  onclick ="javascript:printpreview();" >   < input  type =button  name =button_fh  value ="关闭"  onclick ="javascript:window.close();" >   </ body > </ html >

            该Object 其实就是IE内置打印控件IEWebBrowser, 设定 Width 和 Height 为0,在界面上就显示不出控件的形状。关于这个组件还有其他的用法,列举如下: WebBrowser.ExecWB(1,1) 打开 Web.ExecWB(2,1) 关闭现在所有的IE窗口,并打开一个新窗口 Web.ExecWB(4,1) 保存网页 Web.ExecWB(6,1) 打印 Web.ExecWB(7,1) 打印预览 Web.ExecWB(8,1) 打印页面设置 Web.ExecWB(10,1) 查看页面属性 Web.ExecWB(15,1) 好像是撤销,有待确认 Web.ExecWB(17,1) 全选 Web.ExecWB(22,1) 刷新 Web.ExecWB(45,1) 关闭窗体无提示 

    采用这种方式打印时,会有安全限制。如打印IE提示"Internet Explorer已经阻止此站点用不安全方式使用ActiveX控件。因此,此页可能显示不正确"。这是IE安全级别较高引起的。解决方法是:1.把该站点加入可信任站点;2.修改安全站点的安全级别。具体方法:点击[工具]->[Internet选项]->[安全]->[受信任的站点],点击[站点],在[可信站点]对话框把该站点加入,点击[确定];然后点击[自定义级别],在[安全设置]对话框中找到ActiveX控件和插件,启用“对ActiveX控件或组件”。

     2. 打印时如何去掉页面底部的路径和顶端的页码编号 

      < script language = " javascript " > var  hkey_root,hkey_path,hkey_keyhkey_root = " HKEY_CURRENT_USER " hkey_path = " /Software/Microsoft/Internet Explorer/PageSetup/ " // 设置网页打印的页眉页脚为空 function  pagesetup_null() {try{var RegWsh = new ActiveXObject("WScript.Shell")hkey_key="header" RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"")hkey_key="footer"RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"")}catch(e){}} // 设置网页打印的页眉页脚为默认值 function  pagesetup_default() {try{var RegWsh = new ActiveXObject("WScript.Shell")hkey_key="header" RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&w&b页码,&p/&P")hkey_key="footer"RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&u&b&d")}catch(e){}} </ script >

    3. 浮动帧打印 

    < script LANGUAGE = javascript >   function  button1_onclick()  {   var odoc=window.iframe1.document;   var r=odoc.body.createTextRange();   var stxt=r.htmlText;   alert(stxt)   var pwin=window.open("","print");   pwin.document.write(stxt);   pwin.print(); }   </ script >  

    4. CSS控制打印方式

    <style>@media print.displayBlock  {display:block} //.displayHidden  {display:hidden} //取消打印.displayNone  {display:none} //取消打印        .PageNext{page-break-after: always;}//分页        .AutoNewline { word-break: break-all;/*自动换行*/ }        .NoNewline { word-break: keep-all;/*不换行*/} </style>

    5. 打印当前页面部分内容的解决方案

    即如何方便、美观地实现报表打印。如果使用浏览器的打印菜单打印的话,将把网页上的一些无用的东西打到报表上,比如应用菜单、【打印】按钮等。      1) .使用专业的打印工具,如Crystal Reports(水晶报表)。最简单的方法是先用水晶报表制作好模板,然后使用ASP/JSP等带参数调用制作好的模板即可。水晶报表在网页上生成报表后,可以直接打印,也可以转存为其它文件如Excel等.

           2) 购买第三方的网上打印控件。

           3) .利用样式表及JavaScript自定义函数实现。示例如下:

    <style>@media print  {.noprint {display:none} }</style>

    在当前页面不需要打印的地方添加 class = 'noprint'  .

    < script language = " JavaScript "  type = " text/JavaScript " > function  divPrint()  {      if (window.print)      {         var div = document.all.div4print.innerHTML;                                            //重点在于定义div的ID,根据你要打印的哪些内容,从原显示页面中用                                            // <div id=div4print>Div1....</div><div id=Div2>Div2...</div>        var css = '';    //定义打印用的CSS        var body ='<table width="640" border="0" cellspacing="0" cellpadding="5">' +         ' <tr> ' + ' <td> ' + div +          ' </td>' + '</tr>' + '</table>';                // ******************************************************        // 在此处重新设置的打印格式,根据你的打印要求,将原显示的        // 网页的DIV内容重新组合,可以根据你原来的表格内容,去掉        // 不要打印的,你也可以能下面定义的noprint忽略掉你不想打        // 印的东西,只调用你要打印的内容,但这样被忽略掉的地方将        // 打印出空,不是很美观。表格宽度要同打印的纸张宽度匹配。        // ******************************************************                                                     document.body.innerHTML = '<center>' + body + '</center>'//// 重设document.body,打印文档准备就绪                                                 window.print();         window.history.go(0);         // ******************************************************        // 调用打印命令,打印当前窗口内容。当你打印时其实是一张新        // 的网页了,但网页文件还是原先的。紧接着调用        // window.history.go(0),再回到打印前的页面,效果相当不差        // ******************************************************    } }   </ script >

      如果你将实现调用的按钮设计在同一张网页上,则直接调用即可;如果你用了分帧方法,实现调用的按钮是在另一张网页上,则在DP函数的第一行加入window.focus()命令,否则打印的只是有按钮的网页。

    6. Tips

    1) . 在设置标格宽度的时候,对于A4纸张,横打用920,竖打用640,效果最好。

    2).


    最新回复(0)