【问题标题】:How to print a HTML content using printer in php?如何在 php 中使用打印机打印 HTML 内容?
【发布时间】:2012-12-13 10:23:36
【问题描述】:

我在这样的变量中有一个 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>";

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

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

        window.print();

    }
</script>.

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

【问题讨论】:

  • 这和 JavaScript 有什么关系?你想用 PHP 打印,对吧?
  • @matejv print() 函数不接受字符串作为参数...
  • 为什么不将其写入隐藏的 iframe,然后打印该 iframe?

标签: php javascript


【解决方案1】:

如何将此页面的内容嵌入 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>";
?>

要从字面上打印 HTML 代码,您可以清理 HTML,如下所示:

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

更多信息:

【讨论】:

    【解决方案2】:
       <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>
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多