瀏覽器控制打印機在線打印


一、直接使用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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM