我正在尝试使用此代码打印页面
<html>
<head>
<script type="text/javascript">
function Popup()
{
var mywindow = window.open('', 'Ticket info', 'height=400,width=600');
mywindow.document.write('<html><head><title>my div</title>');
mywindow.document.write('<style type="text/css"> *{margin: 0; padding: 0;} body{padding: 3px; padding-left:20px;font:6px bold Arial;}</style>');
mywindow.document.write('<script src="http://code.jquery.com/jquery-latest.min.js"><'+'/script>');
mywindow.document.write('<script src="jquery-barcode.min.js"><'+'/script>');
mywindow.document.write('</head><body>');
mywindow.document.write('<div id="demo"></div>');
mywindow.document.write('<script type="text/javascript">$("#demo").barcode("1234567890128", "code39");<'+'/script>');
mywindow.document.write('</body></html>');
mywindow.print();
return true;
}
</script>
</head>
<body>
<input type="button" value="Print Div" onclick="Popup();" />
</body>
</html>
基本上,它将弹出一个窗口并显示页面的打印预览。第一次尝试加载打印预览将不会加载条形码,并且当您取消第一个打印预览然后右键单击页面并再次打印时,第二次打印预览现在将显示要打印的条形码。
我认为问题出在此行:
mywindow.document.write('<script type="text/javascript">$("#demo").barcode("1234567890128", "code39");<'+'/script>');
当我对此行发表评论并在页面中添加虚拟文本时。第一次尝试时,它将自动显示在打印预览中。
在尝试从css文件加载样式之前,我遇到了同样的问题。我通过将样式直接转移到弹出窗口来解决此问题。
我的问题是为什么会这样?以及如何在第一次尝试打印预览时加载条形码?
您需要延迟打印。铬存在天然缺陷。
代码如下:
function PrintDiv(data) {
var mywindow = window.open();
var is_chrome = Boolean(mywindow.chrome);
mywindow.document.write(data);
if (is_chrome) {
setTimeout(function() { // wait until all resources loaded
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
mywindow.print(); // change window to winPrint
mywindow.close(); // change window to winPrint
}, 250);
} else {
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
mywindow.print();
mywindow.close();
}
return true;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句