IE下使用js与自带ActiveX控制web打印

来源:互联网 发布:手机淘宝直通车怎么做 编辑:程序博客网 时间:2024/05/17 09:34
一.打印需求1.控制页眉页脚和上下左右的页边距
2.可选横向打印
3.跳过配置对话直接打印


二.环境与背景
1.浏览器版本:IE8
2.不能使用ScriptX
3.不能开发新ActiveX


三.实现
1.控制页眉页脚和页边距

使用ActiveX组件"WScript.Shell"对注册表进行访问修改,相当于修改了IE中的打印设置。

web_printer.js:

function JSActiveXPrinter(){this.header = null;this.footer = null;this.marginTop = null;this.marginBottom = null;this.marginLeft = null;this.marginRight = null;this.localHeader = null;this.localFooter = null;this.localMarginTop = null;this.localMarginBottom = null;this.localMarginLeft = null;this.localMarginRight = null;this.portrain = false;this.wsShell = new ActiveXObject("WScript.Shell");this.regPath = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";}JSActiveXPrinter.prototype.doPrint = function(){this.getLocalPageSetup();this.setPageSetup();window.print();this.recoverPageSetup();}JSActiveXPrinter.prototype.getLocalPageSetup = function(){//Get local page setup setting for recoverythis.localHeader = this.wsShell.RegRead(this.regPath + "header");this.localFooter = this.wsShell.RegRead(this.regPath + "footer");this.localMarginTop = this.wsShell.RegRead(this.regPath + "margin_top");this.localMarginBottom = this.wsShell.RegRead(this.regPath + "margin_bottom");this.localMarginLeft = this.wsShell.RegRead(this.regPath + "margin_left");this.localMarginRight = this.wsShell.RegRead(this.regPath + "margin_right");//Set defualt valuethis.header = this.localHeader;this.footer = this.localFooter;this.marginTop = this.localMarginTop;this.marginBottom = this.localMarginBottom;this.marginLeft = this.localMarginLeft;this.marginRight = this.localMarginRight;}JSActiveXPrinter.prototype.setPageSetup = function(){this.wsShell.RegWrite(this.regPath + "header", this.header);this.wsShell.RegWrite(this.regPath + "footer", this.footer);this.wsShell.RegWrite(this.regPath + "margin_top", this.marginTop);this.wsShell.RegWrite(this.regPath + "margin_bottom", this.marginBottom);this.wsShell.RegWrite(this.regPath + "margin_left", this.marginLeft);this.wsShell.RegWrite(this.regPath + "margin_right", this.marginRight);this.setOrientation(this.portrain);}JSActiveXPrinter.prototype.recoverPageSetup = function(){this.wsShell.RegWrite(this.regPath + "header", this.localHeader);this.wsShell.RegWrite(this.regPath + "footer", this.localFooter);this.wsShell.RegWrite(this.regPath + "margin_top", this.localMarginTop);this.wsShell.RegWrite(this.regPath + "margin_bottom", this.localMarginBottom);this.wsShell.RegWrite(this.regPath + "margin_left", this.localMarginLeft);this.wsShell.RegWrite(this.regPath + "margin_right", this.localMarginRight);}


2.横向打印

系统和IE自带控件并不支持打印设置API,网上也有几种方法:

(1) 模拟法:成效好,实际就是通过JS控制键盘快捷键进行IE的页面设置

var ws = new ActiveXObject("WScript.Shell");ws.sendKeys("%fu");ws.sendKeys("%a");ws.sendKeys("{ENTER}");
上面的代码表示:a. "Alt + f + u"呼出“页面设置” --> b. "Alt + a"选择横向打印 --> "确定“

PS:此法有很大的不可控性,首先代码执行期间不可受干扰,各个快捷键的执行有时需要使用setTimeout进行延迟操作才能起效

(2) CSS滤镜法:

  filter: progid:DXImageTransform.Microsoft.BasicImage(Rotation=1);

PS:

a.  注意翻转后的尺寸,超出了会遮掩内容以及出现空白页。

b.  关于分页——使用空白DIV和"page-break-before:aways"之类的样式可以使各个翻转框可有效分页,防止“粘黏”现象。


附--个人实验所得:

1、CSS滤镜效果作用于页面加载后,所以被翻转元素的宽度、高度会一起“翻转”。

2、IE打印效果为水平内容会被压缩或者超出部分被忽略,垂直内容超出部分会翻页,用打印预览在不同比例下看一下就知道了。

3、最好使用DIV等元素包裹待翻转内容,并设置初始宽度、高度,不然可能会出现内容字体变大、超出可打印区域部分内容被忽略等情况。


3. 屏蔽打印设置对话窗

使用ActiveX控件WebBrowser可以控制IE部分功能。

当使用window.print()函数进行打印功能调用的时候,可以使用VBScript重写print()函数(虽然Javascript也能重写,但是调用WebBrowser的时候却不能成功屏蔽打印设这对话)。

<Script language="VBScript">    Sub Print()        call WB.EXECWB(6,2,1)    End Sub</Script>




原创粉丝点击