我正在使用AJAX将表单数据发送到服务器php文件,该文件构建并发送html电子邮件。我在服务器php文件上回应的这部分数据。回显的html构建了一个我希望用户打印的表(在纸上)。我想打开默认的浏览器打印对话框,以便用户可以打印他/她看不到的表格。我不在乎是否必须打开一个新标签显示回显的内容。这甚至可能吗?
答案 0 :(得分:10)
返回html表单的ajax请求,然后使用javascript打印
此代码未经过测试
<强> JQuery的/使用Javascript 强>
$.post("EmailFile.php", { "EmailParam": "EmailVal" },
function(data){
var HTML = data.EmailHTML;
var WindowObject = window.open("", "PrintWindow", "width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes");
WindowObject.document.writeln(HTML);
WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
WindowObject.close();
}, "json");
PHP文件(EmailFile.php)
$EmailData = $_POST['EmailParam'];
//...Send Email...
//..Build HTML...
$TableHTML = "<table></table>";
//Return HTML
$JSONArr['EmailHTML'] = $TableHTML;
echo json_encode($JSONArr);
答案 1 :(得分:3)
如果您正在尝试打印不可见的内容,可以使用两个不同的css文件用于不同的媒体(屏幕与打印),您可以通过 display:none; 隐藏/取消隐藏所需内容,然后通过 window.print()生成打印对话框。
你的问题有点令人困惑。
例如:
<link rel="stylesheet" type="text/css" href="theme1.css" media="screen" />
<link rel="stylesheet" type="text/css" href="theme2.css" media="print" />
<div class="hidden_on_page">YOU CAN'T SEE ME BUT YOU CAN PRINT ME!</div>
<div class="on_page">YOU CAN SEE ME BUT YOU CAN'T PRINT ME</div>
然后在theme1.css:
.hidden_on_page { display: none; }
然后在theme2.css中:
.on_page { display: none; }
当你需要时,你会触发打印对话框产生:
window.print();
答案 2 :(得分:0)
找到解决方案有完整的代码:
http://codexhelp.blogspot.in/2017/04/php-ajax-window-print-page-content.html
<script>
function printContent(id){
$.ajax({
type: "POST",
url: 'printContent.php',
data: {id: id},
type: 'get',
success: function( response ) {
var contents = response;
var idname = name;
var frame1 = document.createElement('iframe');
frame1.name = "frame1";
frame1.style.position = "absolute";
frame1.style.top = "-1000000px";
document.body.appendChild(frame1);
var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
frameDoc.document.open();
frameDoc.document.write('<html><head><title></title>');
frameDoc.document.write('<style>table { border-collapse: collapse; border-spacing: 0; width:100%; margin-top:20px;} .table td, .table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th{ padding:8px 18px; } .table-bordered, .table-bordered > tbody > tr > td, .table-bordered > tbody > tr > th, .table-bordered > tfoot > tr > td, .table-bordered > tfoot > tr > th, .table-bordered > thead > tr > td, .table-bordered > thead > tr > th { border: 1px solid #e2e2e2;} </style>');
// your title
frameDoc.document.title = "Print Content with ajax in php";
frameDoc.document.write('</head><body>');
frameDoc.document.write(contents);
frameDoc.document.write('</body></html>');
frameDoc.document.close();
setTimeout(function () {
window.frames["frame1"].focus();
window.frames["frame1"].print();
document.body.removeChild(frame1);
}, 500);
return false;
}
});
}
</script>