• [整理] Windows下打印网页


    [整理] 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:WINDOWSsystem32shell32.dll
    开始-》运行-》regsvr32 c:WINDOWSsystem32WSHom.Ocx
    开始-》运行-》regsvr32 c:WINDOWSsystem32scrrun.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>   
    

    参考文档

  • 相关阅读:
    Linux-nmap
    MongoDb注意事项
    HTML如何转XTML
    Centos 64位 Install certificate on apache 即走https协议
    CentOS 下搭建部署独立SVN服务器全程详解(5.5)
    LNMP安装与配置
    64位CentOS 6.0下搭建LAMP环境
    Apache遇到的问题:APR not found
    超详细LAMP环境搭建
    偏方治百病
  • 原文地址:https://www.cnblogs.com/2dogslife/p/9349016.html
Copyright © 2020-2023  润新知