Javascript 如何在 PHP 中打印页面以使用与 window.print() 相同的打印机进行打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26114277/
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 print a page in PHP to print using printer same as window.print() works
提问by Puzzled Boy
Actually i want to print the content with below code sample.
其实我想用下面的代码示例打印内容。
$htmlhaving my all HTMLwhich i want to print without render a View in Browser and without print/show in browser.
$html拥有我HTML想要打印的所有内容,而无需在浏览器中呈现视图,也无需在浏览器中打印/显示。
I am trying to find a Same method as window.print();works. But need in PHP. I don't want to show all the HTMLin Browser.
我正在尝试找到与window.print();作品相同的方法。但需要在PHP. 我不想HTML在浏览器中显示所有内容。
Is there any method or Trick ? Any suggestion can help me lot. Thank you.
有什么方法或技巧吗?任何建议都可以帮助我很多。谢谢你。
My Sample Code:
我的示例代码:
$arr = array('one','two','three','four','five');
$html = "<div style='background:red;color:black;'>";
foreach($arr as $value){
$html .= $value.'<br />';
}
$html .= "</div>";
// print code to print $html content as same as JS window.print() works.
回答by edCoder
<script type="text/javascript">
function PrintDiv() {
var divToPrint = document.getElementById('divToPrint');
var popupWin = window.open('', '_blank', 'width=300,height=300');
popupWin.document.open();
popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
popupWin.document.close();
}
</script>
<div id="divToPrint" style="display:none;">
<div style="width:200px;height:300px;background-color:teal;">
<?php echo $html; ?>
</div>
</div>
<div>
<input type="button" value="print" onclick="PrintDiv();" />
</div>

