Javascript 单击打印按钮时获取jsp页面的精确布局
我的jsp页面中有一个必须打印的div。单击“打印”按钮后,除版面设计(无法打印确切样式)外,其他一切正常。 谷歌搜索后,我知道这是因为CSS链接,需要明确添加。当我添加相同的链接时,我的引导链接似乎不起作用。谁能为同样的问题提出最好的解决方案。 以下是我的代码: 需要打印的div预览: 单击打印按钮时执行的代码为:Javascript 单击打印按钮时获取jsp页面的精确布局,javascript,html,css,jsp,bootstrap-modal,Javascript,Html,Css,Jsp,Bootstrap Modal,我的jsp页面中有一个必须打印的div。单击“打印”按钮后,除版面设计(无法打印确切样式)外,其他一切正常。 谷歌搜索后,我知道这是因为CSS链接,需要明确添加。当我添加相同的链接时,我的引导链接似乎不起作用。谁能为同样的问题提出最好的解决方案。 以下是我的代码: 需要打印的div预览: 单击打印按钮时执行的代码为: function printReceipt(){ console.log("Printing the receipt"); var cont
function printReceipt(){
console.log("Printing the receipt");
var contents = $("#receipt").html();
var frame1 = $('<iframe />');
frame1[0].name = "frame1";
frame1.css({ "position": "absolute", "top": "-1000000px" });
$("body").append(frame1);
var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;
frameDoc.document.open();
//Create a new HTML document.
frameDoc.document.write('<html><head><title></title>');
frameDoc.document.write('<link type="text/css" rel="stylesheet" href="<spring:url value="/resources/css/bootstrap.min.css" />">');
frameDoc.document.write('<link type="text/css" rel="stylesheet" href="<spring:url value="/resources/css/hmsCss.css" />">');
frameDoc.document.write('</head><body>');
//Append the DIV contents.
frameDoc.document.write(contents);
frameDoc.document.write('<script type="text/javascript" src="<spring:url value="/resources/js/jquery.min.js"/>"><\/script>');
frameDoc.document.write('<script type="text/javascript" src="<spring:url value="/resources/js/bootstrap.min.js"/>"><\/script>');
frameDoc.document.write('</body></html>');
frameDoc.document.close();
setTimeout(function () {
window.frames["frame1"].focus();
window.frames["frame1"].print();
frame1.remove();
}, 500);
}
函数printReceivement(){
控制台日志(“打印收据”);
var contents=$(“#收据”).html();
变量frame1=$('');
frame1[0]。name=“frame1”;
frame1.css({“位置”:“绝对”,“顶部”:“-1000000px”});
$(“正文”)。追加(第1帧);
var frameDoc=frame1[0]。contentWindow?frame1[0]。contentWindow:frame1[0]。contentDocument.document?frame1[0]。contentDocument.document:frame1[0]。contentDocument;
frameDoc.document.open();
//创建一个新的HTML文档。
frameDoc.document.write(“”);
frameDoc.document.write(“”);
frameDoc.document.write(“”);
frameDoc.document.write(“”);
//附加DIV内容。
frameDoc.document.write(内容);
frameDoc.document.write(“”);
frameDoc.document.write(“”);
frameDoc.document.write(“”);
frameDoc.document.close();
setTimeout(函数(){
window.frames[“frame1”].focus();
window.frames[“frame1”].print();
框架1.移除();
}, 500);
}
我的打印收据格式:
打印收据标题的布局已更改。如何使其与预览格式完全相同。任何建议都是可以接受的。尝试以下方法:
<spring:url value="/resources/css/bootstrap.min.css" var="bsmin" />
...
<script>
function printReceipt(){
...
frameDoc.document.write('<link type="text/css" rel="stylesheet" href="${bsmin}">');
...
}
...
函数printReceive(){
...
frameDoc.document.write(“”);
...
}
我使用引导使用了一些样式属性。无论如何,非常感谢您的回答:-)