javascript 如何在 php 中使用打印机打印 HTML 内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13857758/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:59:06  来源:igfitidea点击:

How to print a HTML content using printer in php?

phpjavascript

提问by Amit Kumar

I have a HTML content in a variable like this.

我在这样的变量中有一个 HTML 内容。

$message ="<div>
    <table align='center'>
        <tr><td><h1>Reporte de Tipo de Documentos</h1></td></tr>
        <tr><td><h2>Farmaceutica MDC</h2></td></tr>
    </table>
</div>
<div>
    <table style='width:960px; margin:0 auto;'>
        <tr colspan='7'>
            <td><?php echo 'Fecha: '".$time = date('d/m/Y h:i:s A')."; ?></td>
        </tr>
        <tr bgcolor='#CCCCCC' height='30'>
            <td><b>Sr. No.</b></td>
            <td><b>Tipo Documento</b></td>
            <td><b>Cant. Pasos</b></td>
            <td><b>Costo</b></td>
            <td><b>Precio</b></td>
            <td><b>Balance</b></td>
            <td><b>Notifica Cliente</b></td>
        </tr>
    </table>";

I want to print a printout page of this HTML Content. Like for web page we use

我想打印此 HTML 内容的打印页面。就像我们使用的网页一样

<script type="text/javascript">
    function printpage()
    {

        window.print();

    }
</script>.

So what i use to print the above HTML content?

那么我用什么来打印上面的 HTML 内容呢?

回答by AlexStack

How about embedding the contents of this page in HTML and adding a script to print it out automatically on page load?

如何将这个页面的内容嵌入到 HTML 中并添加一个脚本以在页面加载时自动打印出来?

<?php
$message ="<div>
    <table align='center'>
        <tr><td><h1>Reporte de Tipo de Documentos</h1></td></tr>
        <tr><td><h2>Farmaceutica MDC</h2></td></tr>
    </table>
</div>
<div>
    <table style='width:960px; margin:0 auto;'>
        <tr colspan='7'>
            <td><?php echo 'Fecha: '".$time = date('d/m/Y h:i:s A')."; ?></td>
        </tr>
        <tr bgcolor='#CCCCCC' height='30'>
            <td><b>Sr. No.</b></td>
            <td><b>Tipo Documento</b></td>
            <td><b>Cant. Pasos</b></td>
            <td><b>Costo</b></td>
            <td><b>Precio</b></td>
            <td><b>Balance</b></td>
            <td><b>Notifica Cliente</b></td>
        </tr>
    </table>";

echo "<html><head></head><body>" . $message . "<script type='application/javascript'>window.onload=function(){window.print()}</script></body></html>";
?>

To literally print the HTML code, you can sanitizethe HTML like:

要真正打印 HTML 代码,您可以像这样清理HTML:

echo "<html><head></head><body>" . htmlspecialchars($message, ENT_QUOTES) . "<script type='application/javascript'>window.onload=function(){window.print()}</script></body></html>";
?>

More information about this:

有关此的更多信息:

回答by Vainglory07

   <input type="submit" value="Print" onclick="printIframe(report);"/>
   <script>
       function printIframe(objFrame) {
           objFrame.focus();
           objFrame.print();
           bjFrame.save();
       }
   </script>
   <iframe name="report" id="report" src="report.php"></iframe>

try calling the form inside an iframe then a link or button that will run the js to print the html/php inside the iframe..

尝试在 iframe 中调用表单,然后调用一个链接或按钮,该链接或按钮将运行 js 以在 iframe 中打印 html/php。