[整理] Windows下打印网页

单纯的打印页面的命令非常简单,只要调用window.print()就可以了

比如下面这段代码中,第一行设定打印区域,第二行将其打印出来

document.body.innerHTML=document.getElementById('print_content').innerHTML 
window.print();

另外我们也可以给不打印区域设定一下css,这样它也不会被打印出来

.noprint{visibility:hidden}   

正常打印的内容,它自动给你增加了如下的页面和页脚,包含了网址等信息

但有的时候我们并不需要,怎么样去掉呢?

页面中添加样式:

<style media="print">
    @page {
      size: auto; /* auto is the initial value */
      margin: 0mm; /* this affects the margin in the printer settings */
    }
</style>

然后再调用

  window.print();

问题 :IE不支持

解决防范:判断出IE浏览器之后调用IE的特别处理

 if(getExplorer() == "IE"){
    pagesetup_null();
 }
 window.print();

function pagesetup_null(){ 
    var hkey_root,hkey_path,hkey_key;
    hkey_root="HKEY_CURRENT_USER";
    hkey_path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
    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 getExplorer() {
    var explorer = window.navigator.userAgent ;
    //ie 
    if (explorer.indexOf("MSIE") >= 0) {
        return "IE";
    }
    //firefox 
    else if (explorer.indexOf("Firefox") >= 0) {
        return "Firefox";
    }
    //Chrome
    else if(explorer.indexOf("Chrome") >= 0){
        return "Chrome";
    }
    //Opera
    else if(explorer.indexOf("Opera") >= 0){
        return "Opera";
    }
    //Safari
    else if(explorer.indexOf("Safari") >= 0){
        return "Safari";
    }
}

但是最新的IE userAgent并不包含MSIE,如下

我实现时做了特别处理,针对上面的userAgent,返回为IE

另外,按照上面方法javascript在执行new ActiveXObject时会出错

关于这个问题网上找了一些方案

Javascript中使用new ActiveXObject(WScript.Shell)创建对象,出现:Automation服务器不能创建对象的错误,其原因有
一、组件未注册,可以采用以下方法:
开始-》运行-》regsvr32 c:\WINDOWS\system32\shell32.dll
开始-》运行-》regsvr32 c:\WINDOWS\system32\WSHom.Ocx
开始-》运行-》regsvr32 c:\WINDOWS\system32\scrrun.dll
如果提示缺少哪个dll或ocx,那么就去网上下载吧或去其他电脑上拷贝
二、浏览器设置(本方法在IE6上通过),可以采用以下方法:
工具-》Internet选项-》安全-》受信任的站点-》站点-》取消下方要求安全验证的复选框,并将你的网址添进去,确定,再选上要求验证的复选框-。
通过以上设置,此组件已能创建,但受限较多,不推荐网站使用。 参考 如何应对new ActiveXObject("WScript.Shell")创建失败的问题

我的电脑当前版本为IE11,以上方案验证通过

打印之后恢复原来设置

有的时候,需要在打印之后恢复原来的设置,实现方法也很简单,只要修改前保存原来设置,完成之后再恢复回去就可以了

贴一下网上的方案(逻辑上看下来没问题,我没有这个需求,没有再去实际验证)

<SCRIPT language=javascript>  

var HKEY_Root,HKEY_Path,HKEY_Key;  
HKEY_Root="HKEY_CURRENT_USER";  
HKEY_Path="//Software//Microsoft//Internet Explorer//PageSetup/";  
var head,foot,top,bottom,left,right;  


//取得页面打印设置的原参数数据  
function PageSetup_temp() {  
try  
{  
   var Wsh=new ActiveXObject("WScript.Shell");  
   HKEY_Key="header";  

   //取得页眉默认值  
   head = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key);  
   HKEY_Key="footer";  

   //取得页脚默认值  
   foot = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key);  
   HKEY_Key="margin_bottom";  

   //取得下页边距  
   bottom = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key);  
   HKEY_Key="margin_left";  

   //取得左页边距  
   left = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key);  
   HKEY_Key="margin_right";  

   //取得右页边距  
   right = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key); 
   HKEY_Key="margin_top";  

   //取得上页边距  
   top = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key);  
  }  

  catch(e){
     alert("不允许ActiveX控件");  
  }  
}  

//设置网页打印的页眉页脚和页边距  
function PageSetup_Null()  
{  
  try  
  {  
   var Wsh=new ActiveXObject("WScript.Shell");  
   HKEY_Key="header";  

   //设置页眉(为空)  
   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");  
   HKEY_Key="footer";  

   //设置页脚(为空)  
   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");  
   HKEY_Key="margin_bottom";  

   //设置下页边距(0)  
   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0");  
   HKEY_Key="margin_left";  

   //设置左页边距(0)  
   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0");  
   HKEY_Key="margin_right";  

   //设置右页边距(0)  
   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0");  
   HKEY_Key="margin_top";  

   //设置上页边距(8)  
   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"8");  
  }  

  catch(e){
     alert("不允许ActiveX控件");  
  }  
}  

//设置网页打印的页眉页脚和页边距为默认值  
function PageSetup_Default()  
{  
  try  
  {  
   var Wsh=new ActiveXObject("WScript.Shell"); 
   HKEY_Key="header";  
   HKEY_Key="header";  

   //还原页眉  
   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,head);  
   HKEY_Key="footer";  

   //还原页脚  
   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,foot); 
   HKEY_Key="margin_bottom";  

   //还原下页边距  
   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,bottom);  
   HKEY_Key="margin_left";  

   //还原左页边距  
   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,left);  
   HKEY_Key="margin_right";  

   //还原右页边距  
   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,right);  
   HKEY_Key="margin_top";  

   //还原上页边距  
   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,top);  
  }  

  catch(e){
     alert("不允许ActiveX控件");  
  }  
}

function printorder()  
{
  PageSetup_temp();//取得默认值
  PageSetup_Null();//设置页面
  factory.execwb(6,6);//打印页面
  PageSetup_Default();//还原页面设置
  //factory.execwb(6,6);
  window.close();  
}  
</script>   

参考文档

评论

banana: 5 years, 6 months ago
谢谢分享,很好的文章

留言请先登录注册! 并在激活账号后留言!