整理:web打印
key words: IE打印 web打印 页面打印
最近又碰到了页面打印,顺便记录一下,本来下午写完了,可惜最近blogjava最近总是出现问题,提交居然失败,只好再来一次.
调用很简单,把要打印的部分直接放在 <div>里就可以了
把div传给 printit就可以了
由于本质上是js调用了ActiveX,所以会涉及到安全的问题,如果客户端对ActiveX禁用会出现问题,所以可以在异常处理信息里告诉用户,可以在安全设置里把你的站点的URL放进信任站点
关于打印,还有另一种类型,就是发票套打,可以用Applet实现,也可以用自己写的ActiveX来实现,上一次的一个phs项目里他们就使用vc写的ActiveX,用起来感觉不错,不过依然存在安全上的限制问题,不过如果用Applet的话实际上也是需要客户端安装jre的,从某种角度上来说差不多。话又说回来,做项目不同于做产品,可以对客户有所要求 :)
最近又碰到了页面打印,顺便记录一下,本来下午写完了,可惜最近blogjava最近总是出现问题,提交居然失败,只好再来一次.
function
SaveDataSourec(HeadName1, HeadName2, DivName) {
var Div1 = DivName.innerHTML;
var css = ' < style type = " text/css " media = all > ' +
'p { line - height: 120 % }' +
'.fhead { font - size: 9pt; text - decoration: none; color: 104A7B}' +
'.ftitle { line - height: 120 % ; font - size: 18px; color: # 000000 }' +
'td { font - size: 10px; color: # 000000 }' +
' </ style > ' ;
var body = ' < table width = " 640 " border = " 0 " cellspacing = " 0 " cellpadding = " 5 " > ' +
' < tr > ' +
' < td class = " fbody " > ' +
' < b >< div align = " center " > ' + ' < font size = " +1 " class = fhead > ' + HeadName1 + ' </ div > ' + ' </ font ></ b > ' +
' < b >< div align = " center " > ' + ' < font size = " +1 " class = fhead > ' + HeadName2 + ' </ div > ' + ' </ font ></ b > ' +
' </ td > ' +
' </ tr > ' +
' < tr > ' +
' < td class = " fbody " > ' +
' < div align = " center " class = ftitle > ' + Div1 + ' </ div > ' +
' </ td > ' +
' </ tr > ' +
' </ table > ';
document.body.innerHTML = ' < center > ' + css + body + ' < OBJECT classid = " CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 " height = 0 id = wb name = wb width = 0 ></ OBJECT > ' + ' </ center > ';
}
function PrintDataSourec(HeadName1, DivName) {
var oldBody = document.body.innerHTML;
var Div1 = DivName.innerHTML;
var css = ' < style type = " text/css " media = all > ' +
'p { line - height: 120 % }' +
'.fhead { font - size: 40pt; text - decoration: none; color: 104A7B}' +
'.ftitle { line - height: 120 % ; font - size: 25px; color: # 000000 }' +
'td { font - size: 25px; color: # 000000 }' +
' </ style > ' ;
var body =
' < table width = " 100% " border = " 0 " cellspacing = " 0 " cellpadding = " 0 " > ' +
' < tr > ' +
' < td height = " 40 " ></ td > ' +
' </ tr > ' +
' </ table > ' +
' < table width = " 640 " border = " 0 " cellspacing = " 0 " cellpadding = " 5 " > ' +
' < tr > ' +
' < td class = " fbody " > ' +
' < b >< div align = " center " > ' + ' < font size = " +3 " class = fhead > ' + HeadName1 + ' </ div > ' + ' </ font ></ b > ' +
' </ td > ' +
' </ tr > ' +
' < tr > ' +
' < td class = " fbody " > ' +
' < div align = " center " class = ftitle > ' + Div1 + ' </ div > ' +
' </ td > ' +
' </ tr > ' +
' </ table > ' +
' < table width = " 640 " border = " 0 " cellspacing = " 0 " cellpadding = " 5 " > ' +
' < tr > ' +
' < td class = " fbody " > ' +
' </ td > ' +
' </ tr > ' +
' </ table > ';
document.body.innerHTML = ' < center > ' + css + body + ' < OBJECT classid = " CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 " height = 0 id = wb name = wb width = 0 ></ OBJECT > ' + ' </ center > ';
try {
wb.execwb( 6 , 6 );
}
catch (ex) {
alert( " 请在IE里设置允许ActiveX " );
}
document.body.innerHTML = oldBody;
}
function PrintDataSourecWithSetup(HeadName1, HeadName2, DivName) {
var oldBody = document.body.innerHTML;
var Div1 = DivName.innerHTML;
var css = ' < style type = " text/css " media = all > ' +
'p { line - height: 120 % }' +
'.fhead { font - size: 9pt; text - decoration: none; color: 104A7B}' +
'.ftitle { line - height: 120 % ; font - size: 18px; color: # 000000 }' +
'td { font - size: 10px; color: # 000000 }' +
' </ style > ' ;
var body = ' < table width = " 640 " border = " 0 " cellspacing = " 0 " cellpadding = " 5 " > ' +
' < tr > ' +
' < td class = " fbody " > ' +
' < b >< div align = " center " > ' + ' < font size = " +1 " class = fhead > ' + HeadName1 + ' </ div > ' + ' </ font ></ b > ' +
' < b >< div align = " center " > ' + ' < font size = " +1 " class = fhead > ' + HeadName2 + ' </ div > ' + ' </ font ></ b > ' +
' </ td > ' +
' </ tr > ' +
' < tr > ' +
' < td class = " fbody " > ' +
' < div align = " center " class = ftitle > ' + Div1 + ' </ div > ' +
' </ td > ' +
' </ tr > ' +
' </ table > ';
document.body.innerHTML = ' < center > ' + css + body + ' < OBJECT classid = " CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 " height = 0 id = wb name = wb width = 0 ></ OBJECT > ' + ' </ center > ';
wb.execwb( 6 , 6 );
document.body.innerHTML = oldBody;
}
function SetPrintSettings() {
// -- advanced features
factory.printing.SetMarginMeasure( 2 ) // measure margins in inches
factory.SetPageRange( false , 1 , 3 ) // need pages from 1 to 3
factory.printing.printer = " HP DeskJet 870C "
factory.printing.copies = 2
factory.printing.collate = true
factory.printing.paperSize = " A4 "
factory.printing.paperSource = " Manual feed "
// -- basic features
factory.printing.header = " This is MeadCo "
factory.printing.footer = " Advanced Printing by ScriptX "
factory.printing.portrait = false
factory.printing.leftMargin = 1.0
factory.printing.topMargin = 1.0
factory.printing.rightMargin = 1.0
factory.printing.bottomMargin = 1.0
}
//调用接口
function printit(HeadName1, DivName) {
if (confirm('是否确认打印 ? ')) {
PrintDataSourec(HeadName1, DivName);
return ;
}
}
//带配置的接口
function printitWithSetup(HeadName1, HeadName2, DivName) {
if (confirm('是否确认')) {
PrintDataSourecWithSetup(HeadName1, HeadName2, DivName);
}
}
//另存
function saveas(HeadName1, DivName) {
SaveDataSourec(HeadName1, "" , DivName);
// wb.execwb(4,1);
top.frames( " mainFrame " ).document.execCommand( " saveAs " );
window.history.back( 0 );
}
var Div1 = DivName.innerHTML;
var css = ' < style type = " text/css " media = all > ' +
'p { line - height: 120 % }' +
'.fhead { font - size: 9pt; text - decoration: none; color: 104A7B}' +
'.ftitle { line - height: 120 % ; font - size: 18px; color: # 000000 }' +
'td { font - size: 10px; color: # 000000 }' +
' </ style > ' ;
var body = ' < table width = " 640 " border = " 0 " cellspacing = " 0 " cellpadding = " 5 " > ' +
' < tr > ' +
' < td class = " fbody " > ' +
' < b >< div align = " center " > ' + ' < font size = " +1 " class = fhead > ' + HeadName1 + ' </ div > ' + ' </ font ></ b > ' +
' < b >< div align = " center " > ' + ' < font size = " +1 " class = fhead > ' + HeadName2 + ' </ div > ' + ' </ font ></ b > ' +
' </ td > ' +
' </ tr > ' +
' < tr > ' +
' < td class = " fbody " > ' +
' < div align = " center " class = ftitle > ' + Div1 + ' </ div > ' +
' </ td > ' +
' </ tr > ' +
' </ table > ';
document.body.innerHTML = ' < center > ' + css + body + ' < OBJECT classid = " CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 " height = 0 id = wb name = wb width = 0 ></ OBJECT > ' + ' </ center > ';
}
function PrintDataSourec(HeadName1, DivName) {
var oldBody = document.body.innerHTML;
var Div1 = DivName.innerHTML;
var css = ' < style type = " text/css " media = all > ' +
'p { line - height: 120 % }' +
'.fhead { font - size: 40pt; text - decoration: none; color: 104A7B}' +
'.ftitle { line - height: 120 % ; font - size: 25px; color: # 000000 }' +
'td { font - size: 25px; color: # 000000 }' +
' </ style > ' ;
var body =
' < table width = " 100% " border = " 0 " cellspacing = " 0 " cellpadding = " 0 " > ' +
' < tr > ' +
' < td height = " 40 " ></ td > ' +
' </ tr > ' +
' </ table > ' +
' < table width = " 640 " border = " 0 " cellspacing = " 0 " cellpadding = " 5 " > ' +
' < tr > ' +
' < td class = " fbody " > ' +
' < b >< div align = " center " > ' + ' < font size = " +3 " class = fhead > ' + HeadName1 + ' </ div > ' + ' </ font ></ b > ' +
' </ td > ' +
' </ tr > ' +
' < tr > ' +
' < td class = " fbody " > ' +
' < div align = " center " class = ftitle > ' + Div1 + ' </ div > ' +
' </ td > ' +
' </ tr > ' +
' </ table > ' +
' < table width = " 640 " border = " 0 " cellspacing = " 0 " cellpadding = " 5 " > ' +
' < tr > ' +
' < td class = " fbody " > ' +
' </ td > ' +
' </ tr > ' +
' </ table > ';
document.body.innerHTML = ' < center > ' + css + body + ' < OBJECT classid = " CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 " height = 0 id = wb name = wb width = 0 ></ OBJECT > ' + ' </ center > ';
try {
wb.execwb( 6 , 6 );
}
catch (ex) {
alert( " 请在IE里设置允许ActiveX " );
}
document.body.innerHTML = oldBody;
}
function PrintDataSourecWithSetup(HeadName1, HeadName2, DivName) {
var oldBody = document.body.innerHTML;
var Div1 = DivName.innerHTML;
var css = ' < style type = " text/css " media = all > ' +
'p { line - height: 120 % }' +
'.fhead { font - size: 9pt; text - decoration: none; color: 104A7B}' +
'.ftitle { line - height: 120 % ; font - size: 18px; color: # 000000 }' +
'td { font - size: 10px; color: # 000000 }' +
' </ style > ' ;
var body = ' < table width = " 640 " border = " 0 " cellspacing = " 0 " cellpadding = " 5 " > ' +
' < tr > ' +
' < td class = " fbody " > ' +
' < b >< div align = " center " > ' + ' < font size = " +1 " class = fhead > ' + HeadName1 + ' </ div > ' + ' </ font ></ b > ' +
' < b >< div align = " center " > ' + ' < font size = " +1 " class = fhead > ' + HeadName2 + ' </ div > ' + ' </ font ></ b > ' +
' </ td > ' +
' </ tr > ' +
' < tr > ' +
' < td class = " fbody " > ' +
' < div align = " center " class = ftitle > ' + Div1 + ' </ div > ' +
' </ td > ' +
' </ tr > ' +
' </ table > ';
document.body.innerHTML = ' < center > ' + css + body + ' < OBJECT classid = " CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 " height = 0 id = wb name = wb width = 0 ></ OBJECT > ' + ' </ center > ';
wb.execwb( 6 , 6 );
document.body.innerHTML = oldBody;
}
function SetPrintSettings() {
// -- advanced features
factory.printing.SetMarginMeasure( 2 ) // measure margins in inches
factory.SetPageRange( false , 1 , 3 ) // need pages from 1 to 3
factory.printing.printer = " HP DeskJet 870C "
factory.printing.copies = 2
factory.printing.collate = true
factory.printing.paperSize = " A4 "
factory.printing.paperSource = " Manual feed "
// -- basic features
factory.printing.header = " This is MeadCo "
factory.printing.footer = " Advanced Printing by ScriptX "
factory.printing.portrait = false
factory.printing.leftMargin = 1.0
factory.printing.topMargin = 1.0
factory.printing.rightMargin = 1.0
factory.printing.bottomMargin = 1.0
}
//调用接口
function printit(HeadName1, DivName) {
if (confirm('是否确认打印 ? ')) {
PrintDataSourec(HeadName1, DivName);
return ;
}
}
//带配置的接口
function printitWithSetup(HeadName1, HeadName2, DivName) {
if (confirm('是否确认')) {
PrintDataSourecWithSetup(HeadName1, HeadName2, DivName);
}
}
//另存
function saveas(HeadName1, DivName) {
SaveDataSourec(HeadName1, "" , DivName);
// wb.execwb(4,1);
top.frames( " mainFrame " ).document.execCommand( " saveAs " );
window.history.back( 0 );
}
调用很简单,把要打印的部分直接放在 <div>里就可以了
<
div
id
="myPrint"
>
your content to print
</ div >
your content to print
</ div >
把div传给 printit就可以了
由于本质上是js调用了ActiveX,所以会涉及到安全的问题,如果客户端对ActiveX禁用会出现问题,所以可以在异常处理信息里告诉用户,可以在安全设置里把你的站点的URL放进信任站点
关于打印,还有另一种类型,就是发票套打,可以用Applet实现,也可以用自己写的ActiveX来实现,上一次的一个phs项目里他们就使用vc写的ActiveX,用起来感觉不错,不过依然存在安全上的限制问题,不过如果用Applet的话实际上也是需要客户端安装jre的,从某种角度上来说差不多。话又说回来,做项目不同于做产品,可以对客户有所要求 :)