web 頁(yè)面分頁(yè)打印的實(shí)現(xiàn)
更新時(shí)間:2009年06月22日 12:39:09 作者:
網(wǎng)上找的,經(jīng)我整理添加demo如下
1.首先引入一個(gè)WebBrowser在需要打印的頁(yè)面,可以直接添加:
<object id="WebBrowser" classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0" width="0">
</object>
到頁(yè)面,或者使用JavaScript在需要的時(shí)候臨時(shí)添加也可以:
document.body.insertAdjacentHTML("beforeEnd",
"<object id=\"WebBrowser\" width=0 height=0 \
classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\">");
2 .頁(yè)面設(shè)置和打印預(yù)覽
如下所示,直接調(diào)用即可
document.all.WebBrowser.ExecWB(6,6) 直接打印
document.all.WebBrowser.ExecWB(8,1) 頁(yè)面設(shè)置
document.all.WebBrowser.ExecWB(7,1) 打印預(yù)覽
或者:
execScript("document.all.WebBrowser.ExecWB 7, 1","VBScript");
3 隱藏不打印的頁(yè)面元素和分頁(yè)
CSS 有個(gè)Media 屬性,可以分開設(shè)置打印和顯示的格式。
如 <style media="print" type="text/css"> …</style> 中間的格式將只在打印時(shí)起作用,不會(huì)影響顯示界面。
所以可以設(shè)定
<style media="print" type="text/css">
.Noprint{display:none;}
.PageNext{page-break-after: always;}
</style>
然后給不想打印的頁(yè)面元素添加: class="Noprint" ,那就不會(huì)出現(xiàn)在打印和打印預(yù)覽中了。
想分頁(yè)的地方添加: <div class="PageNext"></div> 就可以了。
4.打印頁(yè)面的特定部分
通過將需要打印的特定部分另建一個(gè)頁(yè)面,然后裝入主頁(yè)面的一個(gè)IFrame中,再調(diào)用IFrame的打印方法,只打印IFrame中的內(nèi)容實(shí)現(xiàn)的。
如:
<iframe style="visibility: visible" name="FrameId" width="100%" height="30%" src="NeedPrintedPage.asp"></iframe>
下面的pringFrame js函數(shù)將只打印Iframe中的內(nèi)容,可以直接引用使用,如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中所裝載頁(yè)面的打印效果在所裝載頁(yè)面設(shè)置就可以了,如分頁(yè)等。
5.后臺(tái)打印
通過建一個(gè)隱藏Iframe實(shí)現(xiàn)的,當(dāng)然仍然會(huì)有頁(yè)面裝載的過程。
下面的函數(shù)創(chuàng)建Iframe裝載頁(yè)面并打印。如 printHidden(url) //url為頁(yè)面地址
function printHidden(url) {
document.body.insertAdjacentHTML("beforeEnd",
"<iframe name=printHiddenFrame width=0 height=0></iframe>");
var doc = printHiddenFrame.document;
doc.open();
doc.write("<body onload=\"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,所以別忘了引用前面的函數(shù)。
以下為demo:
<html>
<head>
<title>報(bào)表</title>
<object id="WebBrowser"
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0"
width="0">
</object>
<style media="print" type="text/css">
.Noprint{display:none;}
.PageNext{page-break-after: always;}
</style>
</head>
<body>
<div class="Noprint">
<input type="button" value="print"
onclick="document.all.WebBrowser.ExecWB(6,6)">
<input type="button" value="printset"
onclick="document.all.WebBrowser.ExecWB(8,1)">
<input type="button" value="view"
onclick="document.all.WebBrowser.ExecWB(7,1)">
<input type="button" value="frame"
onclick="printHidden(FrameId)">
</div>
<div class="PageNext">
1
</div>
<div class="PageNext">
2
</div>
<iframe style="visibility: visible" diplay="none"name="FrameId" width="100%"
height="30%" src="2.html"></iframe>
</body>
</html>
<script type="text/javascript">
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;
}
function printHidden(url) {
document.body.insertAdjacentHTML("beforeEnd",
"<iframe name=printHiddenFrame width=0 height=0></iframe>");
var doc = printHiddenFrame.document;
doc.open();
doc.write("<body onload=\"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);
}
</script>
復(fù)制代碼 代碼如下:
<object id="WebBrowser" classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0" width="0">
</object>
到頁(yè)面,或者使用JavaScript在需要的時(shí)候臨時(shí)添加也可以:
復(fù)制代碼 代碼如下:
document.body.insertAdjacentHTML("beforeEnd",
"<object id=\"WebBrowser\" width=0 height=0 \
classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\">");
2 .頁(yè)面設(shè)置和打印預(yù)覽
如下所示,直接調(diào)用即可
復(fù)制代碼 代碼如下:
document.all.WebBrowser.ExecWB(6,6) 直接打印
document.all.WebBrowser.ExecWB(8,1) 頁(yè)面設(shè)置
document.all.WebBrowser.ExecWB(7,1) 打印預(yù)覽
或者:
復(fù)制代碼 代碼如下:
execScript("document.all.WebBrowser.ExecWB 7, 1","VBScript");
3 隱藏不打印的頁(yè)面元素和分頁(yè)
CSS 有個(gè)Media 屬性,可以分開設(shè)置打印和顯示的格式。
如 <style media="print" type="text/css"> …</style> 中間的格式將只在打印時(shí)起作用,不會(huì)影響顯示界面。
所以可以設(shè)定
<style media="print" type="text/css">
.Noprint{display:none;}
.PageNext{page-break-after: always;}
</style>
然后給不想打印的頁(yè)面元素添加: class="Noprint" ,那就不會(huì)出現(xiàn)在打印和打印預(yù)覽中了。
想分頁(yè)的地方添加: <div class="PageNext"></div> 就可以了。
4.打印頁(yè)面的特定部分
通過將需要打印的特定部分另建一個(gè)頁(yè)面,然后裝入主頁(yè)面的一個(gè)IFrame中,再調(diào)用IFrame的打印方法,只打印IFrame中的內(nèi)容實(shí)現(xiàn)的。
如:
<iframe style="visibility: visible" name="FrameId" width="100%" height="30%" src="NeedPrintedPage.asp"></iframe>
下面的pringFrame js函數(shù)將只打印Iframe中的內(nèi)容,可以直接引用使用,如printFrame(FrameId);
復(fù)制代碼 代碼如下:
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中所裝載頁(yè)面的打印效果在所裝載頁(yè)面設(shè)置就可以了,如分頁(yè)等。
5.后臺(tái)打印
通過建一個(gè)隱藏Iframe實(shí)現(xiàn)的,當(dāng)然仍然會(huì)有頁(yè)面裝載的過程。
下面的函數(shù)創(chuàng)建Iframe裝載頁(yè)面并打印。如 printHidden(url) //url為頁(yè)面地址
function printHidden(url) {
document.body.insertAdjacentHTML("beforeEnd",
"<iframe name=printHiddenFrame width=0 height=0></iframe>");
var doc = printHiddenFrame.document;
doc.open();
doc.write("<body onload=\"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,所以別忘了引用前面的函數(shù)。
以下為demo:
<html>
<head>
<title>報(bào)表</title>
<object id="WebBrowser"
classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0"
width="0">
</object>
<style media="print" type="text/css">
.Noprint{display:none;}
.PageNext{page-break-after: always;}
</style>
</head>
<body>
<div class="Noprint">
<input type="button" value="print"
onclick="document.all.WebBrowser.ExecWB(6,6)">
<input type="button" value="printset"
onclick="document.all.WebBrowser.ExecWB(8,1)">
<input type="button" value="view"
onclick="document.all.WebBrowser.ExecWB(7,1)">
<input type="button" value="frame"
onclick="printHidden(FrameId)">
</div>
<div class="PageNext">
1
</div>
<div class="PageNext">
2
</div>
<iframe style="visibility: visible" diplay="none"name="FrameId" width="100%"
height="30%" src="2.html"></iframe>
</body>
</html>
<script type="text/javascript">
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;
}
function printHidden(url) {
document.body.insertAdjacentHTML("beforeEnd",
"<iframe name=printHiddenFrame width=0 height=0></iframe>");
var doc = printHiddenFrame.document;
doc.open();
doc.write("<body onload=\"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);
}
</script>
您可能感興趣的文章:
- Web打印解決方案之普通報(bào)表打印功能
- Web打印解決方案之證件套打的實(shí)現(xiàn)思路
- 網(wǎng)頁(yè)WEB打印控件制作
- JavaWeb實(shí)現(xiàn)打印功能
- 利用javascript實(shí)現(xiàn)web頁(yè)面中指定區(qū)域打印
- js控制web打印(局部打印)方法整理
- web打印 window.print()介紹
- web的各種前端打印方法之jquery打印插件jqprint實(shí)現(xiàn)網(wǎng)頁(yè)打印
- web的各種前端打印方法之jquery打印插件PrintArea實(shí)現(xiàn)網(wǎng)頁(yè)打印
- web打印小結(jié)
相關(guān)文章
phantomjs導(dǎo)出html到pdf的方法總結(jié)
這篇文章主要介紹了phantomjs導(dǎo)出html到pdf的方法總結(jié),需要的朋友可以參考下2017-10-10
uniapp 仿微信的右邊下拉選擇彈出框的實(shí)現(xiàn)代碼
這篇文章主要介紹了uniapp 仿微信的右邊下拉選擇彈出框的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
javascript中call,apply,bind的區(qū)別詳解
這篇文章主要介紹了javascript中call,apply,bind的區(qū)別詳解,幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下2020-12-12
JavaScript如何判斷一個(gè)對(duì)象是空對(duì)象(附5種常見方法)
在JavaScript中判斷對(duì)象的類型是開發(fā)過程中的一個(gè)常見需求,尤其是在處理不確定類型的數(shù)據(jù)時(shí),這篇文章主要介紹了JavaScript如何判斷一個(gè)對(duì)象是空對(duì)象的相關(guān)資料,需要的朋友可以參考下2025-04-04
javascript實(shí)現(xiàn)的淘寶旅行通用日歷組件用法實(shí)例
這篇文章主要介紹了javascript實(shí)現(xiàn)的淘寶旅行通用日歷組件,以實(shí)例形式分析了該日歷組件的相關(guān)設(shè)置及使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
JavaScript 用Node.js寫Shell腳本[譯]
你懂JavaScript嗎?你需要寫一個(gè)Shell腳本嗎?那么你應(yīng)該試一下Node.js,它很容易安裝,而且很適合通過寫Shell腳本來學(xué)習(xí)它2012-09-09

