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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了如何在php中使用打印机打印HTML内容?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
在这样的变量中有 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

脚本宝典总结

以上是脚本宝典为你收集整理的如何在php中使用打印机打印HTML内容?全部内容,希望文章能够帮你解决如何在php中使用打印机打印HTML内容?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。