iframe.contentWindow.print()打印局部页面时,去掉页眉页脚
<style media="print">
@page {
size: auto; /* auto is the initial value */
margin: 0mm 10mm; /* this affects the margin in the printer settings */
}
</style>
页面做打印的时候,用了网上的方法不好使,后来才发现加错位置了,iframe嵌套的页面要把这段样式加到子页面(reportOverviewPrint.html)里面,也就是iframe页面里面,我一开始加在了父页面里面。
@page:设置页面容器的版式,方向,边空等。
margin(上 右 下 左) :打印时的边缘留白,上下为0时,才可以没有页眉和页脚;同时左右的边距是可以调节的,因为如果是0,左右留白太少,10mm比较合适。
相关代码如下:
<button type="button" class="btn btn-primary" id="btn-print" style="padding: 4px 30px;">打印</button>
<!-- start打印的外部盒子 -->
<div class="col-md-12">
<iframe src="reportOverviewPrint.html" style="width:100%; min-width: 900px;height:700px;" runat="server" frame scrolling="no" allowtransparency="yes" id="printId"></iframe>
</div>
<!-- end打印的外部盒子 -->
<script language="javascript" type="text/javascript">
$(document).ready(function() {
function isIE() {
if (!!window.ActiveXObject || "ActiveXObject" in window) {
return true;
} else {
return false;
}
}
function Cprint() {
var iframe = document.getElementById('printId');
iframe.contentWindow.focus();
iframe.style.fontFamily = '宋体';
if (isIE()) {
document.body.className += ' ext-ie';
document.execCommand('print', true, null);
} else {
iframe.contentWindow.print();
}
}
$('#btn-print').click(function() {
Cprint();
});
});
</script>