zoukankan      html  css  js  c++  java
  • 浏览器控制打印机在线打印

    一、直接使用window.print()

    <script>
    function print() {
        bdhtml=window.document.body.innerHTML; 
        sprnstr="<!--startprint-->"; 
        eprnstr="<!--endprint-->"; 
        prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17); //17为sprnstr的length
        prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr)); 
        window.document.body.innerHTML=prnhtml; 
        window.print();
        location.reload();
    }
    </script>
    
    <!--startprint-->
    <div>要打印的内容...</div>
    <!--endprint-->
    <input type="button" value="打印" onclick="print();" />

    二、使用IE内置的浏览器控件WebBrowser(无需用户下载)

     注:要在IE的"Internet选项——安全——自定义级别——把对没有标记为安全的ActiveX控件进行初始化和脚本运行,设置为启用"

    <OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" id="WebBrowser1" width="0" height="0" VIEWASTEXT></OBJECT>
    
    <script>
    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);
    
            Wsh = null;
         }
        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,"0");
    
            Wsh = null;
         }
         catch(e){
            //alert("不允许使用ActiveX控件");
        }
    }
    
    //设置网页打印的页眉页脚和页边距为默认值
    function PageSetup_Default(){
        try{
            var Wsh=new ActiveXObject("WScript.Shell");
            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);
    
            Wsh = null;
        }
        catch(e){
            //alert("不允许使用ActiveX控件");
        }
    }
    
    //使用顺序
    function printorder(){
        PageSetup_temp();//取得默认值
            PageSetup_Null();//设置页面
            WebBrowser1.execwb(6,6);//打印页面
            PageSetup_Default();//还原页面设置
            window.close();
    }
    </script> 
    
    <script>
    /*
     * 打印
     */
    function print(){
        PageSetup_temp();//取得默认值
        PageSetup_Null();//设置页面
        WebBrowser1.ExecWB(6,1);//直接打印
        PageSetup_Default();//还原页面设置
        setTimeout("printClose()", 5000); //这里延时要长点,要先等打印后再关闭
    }
    
    /*
     * 打印预览
     */
    function printPreview(){
        PageSetup_temp();//取得默认值
        PageSetup_Null();//设置页面
        WebBrowser1.ExecWB(7, 1);//打印预览
         PageSetup_Default();//还原页面设置
        setTimeout("printClose()", 100); //没有延时不会自动关闭
    }
    
    /*
     * 打印设置
     */
    function printSetting(){
        WebBrowser1.ExecWB(8,1);
    }
    
    /*
     * 关闭打印页面
     */
    function printClose(){
        WebBrowser1.ExecWB(45,1); //关闭窗体无提示
    }
    </script>
    
    <!-- 我是将内容放在新页面中打印的 -->
    <body>
    <div>要打印的内容</div>
    <div>
        <input type="button" value="打印" onclick="print()" />
        <input type="button" value="打印预览" onclick="printPreview()" />
        <input type="button" value="打印设置" onclick="printSetting()" />
    </div>
    </body>

    关于这个组件还有其他的用法,列举如下:

    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); //关闭窗体无提示

     打印时会把整个页面都打印出来的,页面里面有什么东西就打印出来,我们有时候只需要打印数据表格,这时我们就要写一个样式了,把不想打印的部份隐藏起来:

    <style type="text/css" media="print">
    .noprint {
        display: none;
    }
    </style>
    
    <!-- 然后使用样式就可以 -->
    <p class="noprint">不需要打印的地方</p>

     

    转载:http://tiwson.iteye.com/blog/617976

            http://www.cnblogs.com/samlin/archive/2008/04/13/1151265.html

  • 相关阅读:
    GreenPlum 大数据平台--运维(二)
    GreenPlum 大数据平台--运维(一)
    GreenPlum 大数据平台--非并行备份(六)
    zabbix--高级篇-监控docker服务(一)
    GreenPlum 大数据平台--备份-邮件配置-gpcrondump & gpdbrestore(五)
    subprocess模块
    Python写随机发红包的原理流程
    粘包
    网络编程: 基于UDP协议的socket
    网络编程: 基于TCP协议的socket, 实现一对一, 一对多通信
  • 原文地址:https://www.cnblogs.com/adforce/p/2450357.html
Copyright © 2011-2022 走看看