Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a page in PHP to print using printer same as window.print() works

Actually i want to print the content with below code sample.

$html having my all HTML which i want to print without render a View in Browser and without print/show in browser.

I am trying to find a Same method as window.print(); works. But need in PHP. I don't want to show all the HTML in Browser.

Is there any method or Trick ? Any suggestion can help me lot. Thank you.

My Sample Code:

$arr = array('one','two','three','four','five');
$html = "<div style='background:red;color:black;'>";
foreach($arr as $value){
    $html .= $value.'<br />';
}
$html .= "</div>"; 

// print code to print $html content as same as JS window.print() works.
like image 614
Naresh Avatar asked Sep 30 '14 06:09

Naresh


1 Answers

 <script type="text/javascript">     
    function PrintDiv() {    
       var divToPrint = document.getElementById('divToPrint');
       var popupWin = window.open('', '_blank', 'width=300,height=300');
       popupWin.document.open();
       popupWin.document.write('<html><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
        popupWin.document.close();
            }
 </script>




<div id="divToPrint" style="display:none;">
  <div style="width:200px;height:300px;background-color:teal;">
           <?php echo $html; ?>      
  </div>
</div>
<div>
  <input type="button" value="print" onclick="PrintDiv();" />
</div>
like image 155
Edwin Thomas Avatar answered Nov 15 '22 20:11

Edwin Thomas