javascript 如何使用 PHP 从 html 表格中打印出来
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11597814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to take a print out of a html table using PHP
提问by Yasitha
I have a table in a PHP page. It is a invoice. So I want to print it after adding items to it, and I had already done the coding part and all the things are working properly.
我在 PHP 页面中有一个表。是发票。所以我想在添加项目后打印它,我已经完成了编码部分并且所有事情都可以正常工作。
Now I just want to print that invoice table to a paper by pressing a button. I searched on Google but no clue is found to do that.
现在我只想通过按一个按钮将该发票表打印到纸上。我在谷歌上搜索,但没有找到任何线索。
Can any one help me?
谁能帮我?
This is my table on right hand side. It is populated by the form in left hand side so I just want to print that right hand side table:
这是我右手边的桌子。它由左侧的表格填充,所以我只想打印右侧的表格:
回答by hsuk
Try this one:
试试这个:
Add an attribute id
to your table your_content
id
向表中添加属性your_content
Place an anchor tag :
放置一个锚标签:
<a href="javascript:void(0);" id="printPage">Print</a>
And add script:
并添加脚本:
This script will initially show print preview of the content that is inside your table.
此脚本最初将显示表格内内容的打印预览。
<script lang='javascript'>
$(document).ready(function(){
$('#printPage').click(function(){
var data = '<input type="button" value="Print this page" onClick="window.print()">';
data += '<div id="div_print">';
data += $('#your_content').html();
data += '</div>';
myWindow=window.open('','','width=200,height=100');
myWindow.innerWidth = screen.width;
myWindow.innerHeight = screen.height;
myWindow.screenX = 0;
myWindow.screenY = 0;
myWindow.document.write(data);
myWindow.focus();
});
});
Copy the script part and put it on the top of your page. And put the print link, [above anchor tag] wherever you want. And make sure you have included the jquery script file.
复制脚本部分并将其放在页面顶部。并将打印链接 [above anchor tag] 放在任何你想要的地方。并确保您已包含 jquery 脚本文件。
回答by pat34515
This would be too trivial of a situation for something like jQuery so you should use plain Javascript to do it. Place this somewhere on your page and see if it works:
这对于像 jQuery 这样的东西来说太微不足道了,所以你应该使用普通的 Javascript 来做到这一点。把它放在你页面上的某个地方,看看它是否有效:
<a href="javascript:void(0);" onclick="printPage();">Print</a>
<script type="text/javascript">
function printPage(){
var tableData = '<table border="1">'+document.getElementsByTagName('table')[0].innerHTML+'</table>';
var data = '<button onclick="window.print()">Print this page</button>'+tableData;
myWindow=window.open('','','width=800,height=600');
myWindow.innerWidth = screen.width;
myWindow.innerHeight = screen.height;
myWindow.screenX = 0;
myWindow.screenY = 0;
myWindow.document.write(data);
myWindow.focus();
};
</script>??????
You can see the demo here: http://jsfiddle.net/Pxqtb/
你可以在这里看到演示:http: //jsfiddle.net/Pxqtb/
回答by Yasitha
i used this code and it solved my problem. thank you all for helping me.....
我使用了这段代码,它解决了我的问题。谢谢大家帮我.....
function printlayout() {
var data = '<input type="button" value="Print this page" onClick="window.print()">';
var DocumentContainer = document.getElementById('printlayout');
//data += $('printlayoutable').html();
//data += '</div>';
var WindowObject = window.open('', "TrackHistoryData",
"width=740,height=325,top=200,left=250,toolbars=no,scrollbars=yes,status=no,resizable=no");
WindowObject.document.writeln(DocumentContainer.innerHTML);
//WindowObject.document.writeln(DocumentContainer.innerHTML);
WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
WindowObject.close();
回答by Nick
You have a few options. (1) If you create a new HTML page with just the invoice, the user can print that page using the browser's print capacity. (2) Alternatively, you can allow the user to download an invoice document which you create and print that. (3) You can use the JavaScript command window.print();
in conjunction with #1. Have a look at this.
你有几个选择。(1) 如果您仅使用发票创建一个新的 HTML 页面,则用户可以使用浏览器的打印功能打印该页面。(2) 或者,您可以允许用户下载您创建并打印的发票文档。(3) 您可以将 JavaScript 命令window.print();
与 #1 结合使用。看看这个。