js网页打印
window.print();打印会出现图片不在打印预览显示,不打印的问题,需要用 jqprint 插件
jquery.jqprint-0.3.js https://www.jq22.com/jquery-info347 要在jq之后引用
2009年版本会有兼容性问题,之前加一段
预览的页面字号不起作用了,需要在jqprint中写入
如果页面有横向滚动而打印时不想显示滚动条,需要在jqprint中写入
jQuery.browser={}; (function(){ jQuery.browser.msie=false; jQuery.browser.version=0; if(navigator.userAgent.match(/MSIE ([0-9]+)./)){ jQuery.browser.msie=true; jQuery.browser.version=RegExp.$1; } })(); (function($) { var opt; $.fn.jqprint = function (options) { opt = $.extend({}, $.fn.jqprint.defaults, options); var $element = (this instanceof jQuery) ? this : $(this); if (opt.operaSupport && $.browser.opera) { var tab = window.open("","jqPrint-preview"); tab.document.open(); var doc = tab.document; } else { var $iframe = $("<iframe />"); if (!opt.debug) { $iframe.css({ position: "absolute", width: "0px", height: "0px", left: "-600px", top: "-600px" }); } $iframe.appendTo("body"); var doc = $iframe[0].contentWindow.document; } if (opt.importCSS) { if ($("link[media=print]").length > 0) { $("link[media=print]").each( function() { doc.write("<link rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />"); }); } else { //加载页面引用的样式表 $("link").each( function() { doc.write("<link href='" + $(this).attr("href") + "' rel='stylesheet' />"); }); doc.write("<style type='text/css'>td,th { font-size: 13px; }</style>"); } } //console.log(doc); if (opt.printContainer) { doc.write($element.outer()); } else { $element.each( function() { doc.write($(this).html()); }); } doc.close(); //培训详情弹开打印预览立即恢复学习照片滚动效果 if($("#tdwid")[0] != undefined){ var tdwid = $("#tdwid")[0].clientWidth - 10; $("#divwid").css({'width':tdwid+'px','overflow-x':'auto'}); }; if($("#tdwid1")[0] != undefined){ var tdwid1 = $("#tdwid1")[0].clientWidth - 10; $("#divwid1").css({'width':tdwid1+'px','overflow-x':'auto'}); }; (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).focus(); setTimeout( function() { (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).print(); if (tab) { tab.close(); } }, 1000); } $.fn.jqprint.defaults = { debug: false, importCSS: true, printContainer: true, operaSupport: true }; // Thanks to 9__, found at http://users.livejournal.com/9__/380664.html jQuery.fn.outer = function() { return $($('<div></div>').html(this.clone())).html(); } })(jQuery);
打印带有背景色效果的需要加样式
/*打印背景色*/ @media all { .dybg { background-color: #eeeeee !important; -webkit-print-color-adjust:exact; -moz-print-color-adjust:exact; -ms-print-color-adjust:exact; print-color-adjust:exact; } .trrbg { background-color: #ffe5e3 !important; -webkit-print-color-adjust:exact; -moz-print-color-adjust:exact; -ms-print-color-adjust:exact; print-color-adjust:exact; } }
另外,如果横向滚动容器宽度不确定,需要通过js计算赋值,在获取渲染时,填充完列表立即更改宽度,在点击打印预览时修改宽度,在jqprint实例创建完后再改回原来效果
//拼好字符串,立即计算容器宽度 var tdwid = $("#tdwid")[0].clientWidth - 10; $("#divwid").css('width',tdwid+'px'); function TPD_dodoPrint(){ //打印预览立即更改宽度并隐藏横向滚动条 $("#divwid").css({'width':'700px','overflow-x':'hidden'}); //$("#divwid1").css({'width':'700px','overflow-x':'hidden'}); $("#TPD_print-area").jqprint({ debug: false, //如果是true则可以显示iframe查看效果(iframe默认高和宽都很小,可以再源码中调大),默认是false importCSS: true, //true表示引进原来的页面的css,默认是true。(如果是true,先会找$("link[media=print]"),若没有会去找$("link")中的css文件) printContainer: true, //表示如果原来选择的对象必须被纳入打印(注意:设置为false可能会打破你的CSS规则)。 operaSupport: true//表示如果插件也必须支持歌opera浏览器,在这种情况下,它提供了建立一个临时的打印选项卡。默认是true }); }
__________________________________________________________________________________
打印表格的时候多页会出现头尾断线的情况,尤其有表头和标题时,
第一页和后面的表格页情况不一样,所以要计算单元格高度,表头高度,标题栏高度,
表格在循环时要做css判断取余行数来改变样式
<div class="ClaimFormEndorsement_contentdayin" style="width:100%;top:10px;left:0;"> <div class="ClaimFormEndorsement_content_fukuan" style="cursor:pointer;margin-right: 50px;" onclick="doPrint()">打印</div> <div style="width:100%;height:700px;overflow-y:auto;margin:0;padding:0;page-break-inside:avoid;" id="printdeliveryView"> <table class="ClaimFormEndorsement_listdy1" id="tableyue" style="width:96%;margin-left: 2%;margin-bottom: 30px;"> <tr> <td colspan="7" style="line-height: 22px;"> <div style="height: 26px;margin-top:17px;margin-bottom:8px;font-size:24px;"><b>表</b></div> <div style="height: 26px;margin-top:8px;margin-bottom:8px;font-size:14px;" id="curDate"></div> </td> </tr> <tbody id="ClaimFormEndorsement_Tab"> <tr class="fbo trrbg" style="height: 35px;line-height: 35px;"> <th style="width:12%;"> <div>日期</div> </th> <th style="width:22%;"> <div>单号</div> </th> <th style="width:7%;"> <div>页码</div> </th> <th style="width:10%;"> <div>责任类别</div> </th> <th style="width:25%;"> <div>责任人</div> </th> <th style="width:13%;"> <div>承担金额</div> </th> <th style="width:11%;"> <div>审批人</div> </th> </tr> <!-- ko foreach:rows --> <tr class="basic_table_trbg dayinTd" data-bind="css:{basic_table_trbg:($index()%2==0),dayinGD:($index()%30==29)}"> <td style="width:12%;" data-bind="text:FormatDate(Create_Date)"></td> <td style="width:22%;" data-bind="text:ClaimID"></td> <td style="width:7%;" data-bind="text:Pg"></td> <td style="width:10%;" data-bind="text:LiableType==1?'员工':'承运商'"></td> <td style="width:25%;" data-bind="text:LiableName"></td> <td style="width:13%;text-align: right; padding-right: 10px;" data-bind="text:LiableAmount.toFixed(2)"></td> <td style="width:11%;" data-bind="text:ExamineSubUserName"></td> </tr> <!-- /ko --> </tbody> </table> </div> </div>
function doPrint() { var headhtml = "<html><head><title></title></head><body>"; var foothtml = "</body>"; // 获取div中的html内容,jquery写法如下 $("#printdeliveryView").css({'height':'auto'})//进入网页打印设置时把高度去掉,否则只有一页还带有滚动条 var newhtml = $("#printdeliveryView").html(); // 获取原来的窗口界面body的html内容,并保存起来 var oldhtml = document.body.innerHTML; $(".ClaimFormEndorsement_content_fukuan").css({"display":"none"}); // 调用window.print方法打印新窗口 window.print(); $(".ClaimFormEndorsement_content_fukuan").css({"display":"block"}) $("#printdeliveryView").css({'height':'700px'})//关闭网页打印设置页面要把高度加回来,否则没有滚动条 }
/*打印*/ .ClaimFormEndorsement_contentdayin{ position:absolute; left:30px; top:30px; width:89%; height:680px; border:none; background-color:white; } .ClaimFormEndorsement_content_title{ text-align:center; } .ClaimFormEndorsement_content_fukuan{ position:absolute; right:30px; top:20px; width:100px; height:30px; line-height:30px; background-color:rgb(46,187,202); border-radius:3px; color:white; font-size:12px; text-align:center; z-index:10; } .ClaimFormEndorsement_listdy1{ width:98%; margin-left:0; border:1px solid gray; font-size:12px; } .ClaimFormEndorsement_listdy1 th{ height:30px; line-height:30px; text-align:center; border:1px solid #333333; /*background-color:rgb(236,236,236);*/ width:120px; } .ClaimFormEndorsement_listdy1 td{ /*height:30px;*/ padding: 2px; line-height:14px; text-align:center; border:1px solid #333333 }//每个单元格间距 .dayinTd td{ padding-top: 10px; padding-bottom: 10px; } //30行取余时的单元格间距
.dayinGD td{ padding-top: 8px; padding-bottom: 8px; } /*打印头部背景色*/ @media all { .trrbg { background-color: #cccccc !important; -webkit-print-color-adjust: exact; } }
打印默认横向
MVC: <style type="text/css" media="print"> @("@")page {size:landscape;} </style> 普通: <style type="text/css" media="print"> @page {size:landscape;} </style>
try{ print.portrait = false; } catch (e){ } window.print();
打印的dom节点 加个样式 page-break-after: always
在 <footer> 元素后始终插入分页符:
@media print { footer {page-break-after: always;} }
auto 默认。如果必要则在元素后插入分页符。
always 在元素后插入分页符。
avoid 避免在元素后插入分页符。
left 在元素之后足够的分页符,一直到一张空白的左页为止。
right 在元素之后足够的分页符,一直到一张空白的右页为止。
inherit 规定应该从父元素继承 page-break-after 属性的设置。