加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 21:46:04 所属栏目:PHP教程 来源:网络整理
导读:我在这样的变量中有 HTML内容. $message ="div table align='center' trtdh1Reporte de Tipo de Documentos/h1/td/tr trtdh2Farmaceutica MDC/h2/td/tr /table/divdiv table style='width:960px; margin:0 auto;' tr colspan='7' td?php echo 'Fecha: '".$ti
我在这样的变量中有 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内容呢?

解决方法

如何在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>";
?>

更多相关信息:

> What’s the best method for sanitizing user input with PHP?
> Sanitizing HTML input
> PHP HTML sanitizer
> http://se2.php.net/manual/en/function.htmlspecialchars.php

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读