利用WebBrowser徹底解決Web打印問題(包括後臺打印)

利用WebBrowser徹底解決Web打印問題(包括後臺打印)


BS架構下的打印大家是怎麼解決的呢,我最近作了一個項目正好負責這一塊,不僅要求打印頁面的特定部分,還要求有後臺的批量打印,在網上查了一些資料,最後終於解決了。抱着“取之於衆 服務於衆”的思想,我總結了一下,把它拿到網上來與大家分享,希望能幫助遇到類似問題的朋友。

我主要使用了IE內置的WebBrowser控件,無需用戶下載和安裝。WebBrowser有很多功能,除打印外的其他功能就不再贅述了,你所能用到的打印功能也幾乎全部可以靠它完成,下面的問題就是如何使用它了。先說顯示後打印,後面說後臺打印。

1.首先引入一個WebBrowser在需要打印的頁面,可以直接添加: 
 
<object id="WebBrowser" classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0" width="0">
</object>

到頁面,或者使用JavaScript在需要的時候臨時添加也可以:

document.body.insertAdjacentHTML("beforeEnd",

    "<object id=/"WebBrowser/" width=0 height=0 /

classid=/"clsid:8856F961-340A-11D0-A96B-00C04FD705A2/">");


2 .頁面設置和打印預覽

如下所示,直接調用即可

 

document.all.WebBrowser.ExecWB(6,6)   直接打印
document.all.WebBrowser.ExecWB(8,1)   頁面設置
document.all.WebBrowser.ExecWB(7,1)   打印預覽

或者:

execScript("document.all.WebBrowser.ExecWB 7, 1","VBScript");


3 隱藏不打印的頁面元素和分頁

 CSS 有個Media 屬性,可以分開設置打印和顯示的格式。

如 <style media="print" type="text/css"> …</style> 中間的格式將只在打印時起作用,不會影響顯示界面。
所以可以設定
<style media="print" type="text/css">
.Noprint{display:none;}
.PageNext{page-break-after: always;}
</style>

然後給不想打印的頁面元素添加: class="Noprint" ,那就不會出現在打印和打印預覽中了。
想分頁的地方添加:  <div class="PageNext"></div> 就可以了。


4.打印頁面的特定部分

我是通過將需要打印的特定部分另建一個頁面,然後裝入主頁面的一個IFrame中,再調用IFrame的打印方法,只打印IFrame中的內容實現的。
如:
<iframe style="visibility: visible" name="FrameId" width="100%" height="30%" src="NeedPrintedPage.asp"></iframe>
下面的pringFrame  js函數將只打印Iframe中的內容,可以直接引用使用,如printFrame(FrameId);


window.print = printFrame;

// main stuff
function printFrame(frame, onfinish) {
  if ( !frame ) frame = window;

  function execOnFinish() {
    switch ( typeof(onfinish) ) {
      case "string": execScript(onfinish); break;
      case "function": onfinish();
    }
    if ( focused && !focused.disabled ) focused.focus();
  }

  if (( frame.document.readyState !== "complete") &&( !frame.document.confirm("The document to print is not downloaded yet! Continue with printing?") ))
  {
    execOnFinish();
    return;
  }

 
  var eventScope = printGetEventScope(frame);
  var focused = document.activeElement;
 
  window.printHelper = function() {
    execScript("on error resume next: printWB.ExecWB 6, 1", "VBScript");
    printFireEvent(frame, eventScope, "onafterprint");
    printWB.outerHTML = "";
    execOnFinish();
    window.printHelper = null;
  }
 
  document.body.insertAdjacentHTML("beforeEnd",
    "<object id=/"printWB/" width=0 height=0 /
    classid=/"clsid:8856F961-340A-11D0-A96B-00C04FD705A2/">");
 
  printFireEvent(frame, eventScope, "onbeforeprint");
  frame.focus();
  window.printHelper = printHelper;
  setTimeout("window.printHelper()", 0);
}


// helpers
function printIsNativeSupport() {
  var agent = window.navigator.userAgent;
  var i = agent.indexOf("MSIE ")+5;
  return parseInt(agent.substr(i)) >= 5 && agent.indexOf("5.0b1") < 0;
}

function printFireEvent(frame, obj, name) {
  var handler = obj[name];
  switch ( typeof(handler) ) {
    case "string": frame.execScript(handler); break;
    case "function": handler();
  }
}

function printGetEventScope(frame) {
  var frameset = frame.document.all.tags("FRAMESET");
  if ( frameset.length ) return frameset[0];
  return frame.document.body;
}

Iframe中所裝載頁面的打印效果在所裝載頁面設置就可以了,如分頁等。

5.後臺打印

我是通過建一個隱藏Iframe實現的,當然仍然會有頁面裝載的過程。
下面的函數創建Iframe裝載頁面並打印。如 printHidden(url)  //url爲頁面地址

function printHidden(url) {
  document.body.insertAdjacentHTML("beforeEnd",
    "<iframe name=printHiddenFrame width=0 height=0></iframe>");
  var doc = printHiddenFrame.document;
  doc.open();
  doc.write("<body οnlοad=/"parent.onprintHiddenFrame()/">");
  doc.write("<iframe name=printMe width=0 height=0 src=/"" +
      url + "/"></iframe>");
  doc.write("</body>");
  doc.close();
}

function onprintHiddenFrame() {
  function onfinish() {
    printHiddenFrame.outerHTML = "";
    if ( window.onprintcomplete ) window.onprintcomplete();
  }
  printFrame(printHiddenFrame.printMe, onfinish);
}

它用到了printFrame,所以別忘了引用前面的函數。

發佈了19 篇原創文章 · 獲贊 6 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章