• HTML实现打印功能


    直接上代码:

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <meta charset="UTF-8">
     5         <title>打印</title>
     6         <style type="text/css">
     7             body{
     8                 background-color: #fff;
     9                 width: 100%;
    10                 height: 100%;
    11             }
    12             * {
    13                 padding: 0;
    14                 margin: 0;
    15             }
    16             .main{
    17                 padding: 2%;
    18                 font-family: Simsun;
    19             }
    20             @media print {
    21                 @page {
    22                     size: A4;
    23                 }
    24             }
    25             @media screen {
    26                 .main{
    27                       display: none;  
    28                 }
    29             }
    30         </style>
    31     </head>
    32     <body>
    33         <!--startprint-->
    34         <div class="main">
    35             <h1 align="center">要打印的文件标题</h1>
    36             <!-- 在这里画要打印的内容 你需要什么标签什么样式自己画就可以 -->
    37         </div>
    38         <!--endprint-->
    39     </body>
    40     <script type="text/javascript" src="/wro/jquery1.8.3.js"></script>
    41     <script type="text/javascript">
    42         $(function(){
    43             print_page();
    44         });
    45         
    46         function print_page() {
    47             if (!!window.ActiveXObject || "ActiveXObject" in window) {
    48                 remove_ie_header_and_footer();
    49             }
    50             bdhtml=window.document.body.innerHTML; //获取当前页的html代码
    51             sprnstr="<!--startprint-->"; //设置打印开始区域
    52             eprnstr="<!--endprint-->";//设置打印结束区域
    53             prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);//从开始代码向后取html
    54             prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//从结束代码向前取html
    55             window.document.body.innerHTML=prnhtml;
    56             window.print();
    57             // 这里延迟关闭是因为在加载的东西比较多的时候,可能dom需要的数据还没加载完就关闭了。这样会导致数据显示不全,所以采用延迟关闭的方法防止数据不全。具体延迟多长时间自己控制。
    58             setTimeout(cancle,5000);
    59         };
    60         
    61         function remove_ie_header_and_footer() {
    62             var hkey_root, hkey_path, hkey_key;
    63             hkey_path = "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup\";
    64             try {
    65                 var RegWsh = new ActiveXObject("WScript.Shell");
    66                 RegWsh.RegWrite(hkey_path + "header", "");
    67                 RegWsh.RegWrite(hkey_path + "footer", "");
    68             } catch (e) {}
    69         }
    70         
    71         function cancle(){
    72             window.close();
    73         }
    74     </script>
    75 </html>

     

    微信公众号:敲代码的小浪漫

    欢迎大家关注~

  • 相关阅读:
    Python安装
    solr集群solrCloud的搭建
    redis单机及其集群的搭建
    maven实现tomcat热部署
    maven发布时在不同的环境使用不同的配置文件
    nexus 的使用及maven的配置
    java 自定义注解以及获得注解的值
    Jenkins学习之——(4)Email Extension Plugin插件的配置与使用
    Jenkins学习之——(3)将项目发送到tomcat
    注意Tengine(Nginx) proxy_pass之后的"/"
  • 原文地址:https://www.cnblogs.com/ywy8/p/13602816.html
Copyright © 2020-2023  润新知