jqprint 分页打印_web 页面分页打印的实现

1.首先引入一个WebBrowser在需要打印的页面,可以直接添加:

到页面,或者使用JavaScript在需要的时候临时添加也可以:

document.body.insertAdjacentHTML("beforeEnd",

"

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 属性,可以分开设置打印和显示的格式。

如 中间的格式将只在打印时起作用,不会影响显示界面。

所以可以设定

.Noprint{display:none;}

.PageNext{page-break-after: always;}

然后给不想打印的页面元素添加: class="Noprint" ,那就不会出现在打印和打印预览中了。

想分页的地方添加:

4.打印页面的特定部分

通过将需要打印的特定部分另建一个页面,然后装入主页面的一个IFrame中,再调用IFrame的打印方法,只打印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",

"

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",

"");

var doc = printHiddenFrame.document;

doc.open();

doc.write("

");

doc.write("

url + "\">

");

doc.write("");

doc.close();

}

function onprintHiddenFrame() {

function onfinish() {

printHiddenFrame.outerHTML = "";

if ( window.onprintcomplete ) window.onprintcomplete();

}

printFrame(printHiddenFrame.printMe, onfinish);

}

它用到了printFrame,所以别忘了引用前面的函数。

以下为demo:

报表

classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0"

width="0">

.Noprint{display:none;}

.PageNext{page-break-after: always;}

οnclick="document.all.WebBrowser.ExecWB(6,6)">

οnclick="document.all.WebBrowser.ExecWB(8,1)">

οnclick="document.all.WebBrowser.ExecWB(7,1)">

οnclick="printHidden(FrameId)">

1

2

height="30%" src="2.html">

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",

"

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",

"");

var doc = printHiddenFrame.document;

doc.open();

doc.write("

");

doc.write("

url + "\">

");

doc.write("");

doc.close();

}

function onprintHiddenFrame() {

function onfinish() {

printHiddenFrame.outerHTML = "";

if ( window.onprintcomplete ) window.onprintcomplete();

}

printFrame(printHiddenFrame.printMe, onfinish);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值