JS调用IE的打印功能



 下载:  相关资源下载(免积分)



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=7"/>
    <title>print1</title>
    <script type="text/javascript">
    window.onload = function() {
        pagePrintSetup();
    }
    // 打印
    function printPage() {  
        beforePrint();
        callPrint();
        afterPrint();
    }   
    // 调用打印
    function callPrint() {
        document.all.WebBrowser.ExecWB(6,1); // 打印
        // document.all.WebBrowser.ExecWB(8,1); // 设置
        // document.all.WebBrowser.ExecWB(7,1); // 预览
    }
    function beforePrint() {
        document.getElementById("printBtn").style.display = "none";
        document.getElementById("currentTime").innerHTML = formatDatetime( new Date() );
    }
    function afterPrint() {
        document.getElementById("printBtn").style.display = "inline-block";
        document.getElementById("currentTime").innerHTML = "";
    }
    // 格式化日期为字符串: 2013-12-12 12:12
    function formatDatetime(v){
        if( !v ) {
            return "";
        }
        var d = new Date(v);
        return d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes();
    }
    </script>
    
<style>
.printArea {width: 606px; margin: 0 auto;}
.commonTable { border-collapse: collapse; font-size: 12px; font-family: "宋体"; color: #323232; margin-top: -1px; }
#firstTable th { font-size: 18px; font-family: "黑体"; padding: 12px 0; color: #323232; text-align: center;}
.commonTable td { border: solid 1px #c0c0c0; line-height: 18px; height: 26px; vertical-align: middle; }
.labelTd { width: 86px; background-color: #eeeeee; text-align: right; padding-right: 6px; }
.contentTd { width: 128px; padding-left: 6px; }
.widerContentTd { width: 200px; }
.colspan300Px { width: auto; max-width: 300px; }
.colspan500Px { width: auto; max-width: 500px; }
.imgTd { text-align: center; vertical-align: middle; width: 141px; }
.imgTd img{ text-align: center; vertical-align: middle; width: 120px; border:none 0;}
.thirdTable td{ text-align: center; padding: 0; }
#currentTime { font-size: 10px; font-weight: normal; font-family: arial; text-align: right; }
#printBtn { display: inline-block; font-weight: normal; font-size: 14px; width: 90px; height: 37px; cursor: pointer; background: url(./105_2.gif) no-repeat; }

</style>
</head>
<body>

    <object id="WebBrowser" height="0" width="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"
        viewastext></object>

<div class="printArea">
        
    <table id="firstTable" class="commonTable">
        <tr>
            <th></th>
            <th colspan="3">常住人口信息表</th>
            <th id="functionGroup">
                <a id="printBtn" οnclick="printPage()"></a>
                <span id="currentTime"></span>
            </th>
        </tr>
        <tr>
            <td class="labelTd" id="firstCell">人员状态</td>
            <td class="contentTd">在册</td>
            <td class="labelTd">人户一致性</td>
            <td class="contentTd">人户一致</td>
            <td class="imgTd" rowspan="5"><img src="./nophoto.jpg"></td>
        </tr>
        <tr>
            <td class="labelTd">姓名</td>
            <td class="contentTd colspan300Px" colspan="3">张三</td>
        </tr>
        <tr>
            <td class="labelTd">曾用名</td>
            <td class="contentTd colspan300Px" colspan="3">张四</td>
        </tr>
        <tr>
            <td class="labelTd">性别</td>
            <td class="contentTd">男</td>
            <td class="labelTd">名族</td>
            <td class="contentTd">汉族</td>
        </tr>
        <tr>
            <td class="labelTd">出生日期时间</td>
            <td class="contentTd colspan300Px" colspan="3">2014 年 06 月 02 日 0 时30 分</td>
        </tr>
    </table>

</div>      
</body>
</html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=7"/>
    <title>print2</title>
    <script type="text/javascript">
    window.onload = function() {
        pagePrintSetup();
    }
    // 打印
    function printPage() {  
        beforePrint();
        callPrint();
        afterPrint();
    }   
    // 调用打印
    function callPrint() {
        var srcBody = '', startPrintMark = '', endPrintMark = '', printContent = '';
        srcBody = window.document.body.innerHTML;
        startPrintMark = "<!--startprint-->";
        endPrintMark = "<!--endprint-->";  
        printContent = srcBody.substring(srcBody.indexOf(startPrintMark) + startPrintMark.length); 
        printContent = printContent.substring(0, printContent.indexOf(endPrintMark));
        window.document.body.innerHTML = printContent;
        window.print();
        window.document.body.innerHTML = srcBody;
        window.location.reload();
    }
    function beforePrint() {
        document.getElementById("printBtn").style.display = "none";
        document.getElementById("currentTime").innerHTML = formatDatetime( new Date() );
    }
    function afterPrint() {
        document.getElementById("printBtn").style.display = "inline-block";
    }

    // 设置网页打印的页眉页脚为空
    function pagePrintSetup(){
        try{
            var RegWsh = new ActiveXObject("WScript.Shell");
            var hkey_root = "HKEY_CURRENT_USER";
            var hkey_path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
            var hkey_key="header";
            RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
            hkey_key="footer";
            RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
            hkey_key="Print_Background";
            RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"yes");
        }catch(e){ 
            alert("请降低IE安全级别,自动设置打印!"); 
        }
    }

    // 格式化日期为字符串: 2013-12-12 12:12
    function formatDatetime(v){
        if( !v ) {
            return "";
        }
        var d = new Date(v);
        return d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes();
    }
    </script>
    
<style>
.printArea {width: 606px; margin: 0 auto;}
.commonTable { border-collapse: collapse; font-size: 12px; font-family: "宋体"; color: #323232; margin-top: -1px; }
#firstTable th { font-size: 18px; font-family: "黑体"; padding: 12px 0; color: #323232; text-align: center;}
.commonTable td { border: solid 1px #c0c0c0; line-height: 18px; height: 26px; vertical-align: middle; }
.labelTd { width: 86px; background-color: #eeeeee; text-align: right; padding-right: 6px; }
.contentTd { width: 128px; padding-left: 6px; }
.widerContentTd { width: 200px; }
.colspan300Px { width: auto; max-width: 300px; }
.colspan500Px { width: auto; max-width: 500px; }
.imgTd { text-align: center; vertical-align: middle; width: 141px; }
.imgTd img{ text-align: center; vertical-align: middle; width: 120px; border:none 0;}
.thirdTable td{ text-align: center; padding: 0; }
#currentTime { font-size: 10px; font-weight: normal; font-family: arial; text-align: right; }
#printBtn { display: inline-block; font-weight: normal; font-size: 14px; width: 90px; height: 37px; cursor: pointer; background: url(./105_2.gif) no-repeat; }

</style>
</head>
<body>

<div class="printArea">
    
<!--startprint-->
<table id="firstTable" class="commonTable">
    <tr>
        <th></th>
        <th colspan="3">常住人口信息表</th>
        <th id="functionGroup">
            <a id="printBtn" οnclick="printPage()"></a>
            <span id="currentTime"></span>
        </th>
    </tr>
    <tr>
        <td class="labelTd" id="firstCell">人员状态</td>
        <td class="contentTd">在册</td>
        <td class="labelTd">人户一致性</td>
        <td class="contentTd">人户一致</td>
        <td class="imgTd" rowspan="5"><img src="./nophoto.jpg"></td>
    </tr>
    <tr>
        <td class="labelTd">姓名</td>
        <td class="contentTd colspan300Px" colspan="3">张三</td>
    </tr>
    <tr>
        <td class="labelTd">曾用名</td>
        <td class="contentTd colspan300Px" colspan="3">张四</td>
    </tr>
    <tr>
        <td class="labelTd">性别</td>
        <td class="contentTd">男</td>
        <td class="labelTd">名族</td>
        <td class="contentTd">汉族</td>
    </tr>
    <tr>
        <td class="labelTd">出生日期时间</td>
        <td class="contentTd colspan300Px" colspan="3">2014 年 06 月 02 日 0 时30 分</td>
    </tr>
</table>
<!--endprint-->

</div>
</body>
</html>



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值