Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a HTML content using printer in php?

Tags:

javascript

php

I have a HTML content in a variable like this.

$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

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

        window.print();

    }
</script>.

So what i use to print the above HTML content?

like image 880
Amit Kumar Avatar asked Dec 04 '25 17:12

Amit Kumar


2 Answers

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

<?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 sanitize the HTML like:

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:

  • How can I sanitize user input with PHP?
  • Sanitizing HTML input
  • PHP HTML sanitizer
  • https://www.php.net/manual/en/function.htmlspecialchars.php
like image 131
AlexStack Avatar answered Dec 06 '25 09:12

AlexStack


   <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..

like image 32
Vainglory07 Avatar answered Dec 06 '25 08:12

Vainglory07



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!