How to print a page in PHP for printing using a printer, similar to working with the .print () window

Actually, I want to print the contents using sample code.

$html having all the HTML that I want to print, without rendering the view in the browser and without printing / displaying in the browser.

I am trying to find the same method as window.print(); . But you need it in PHP . I do not want to display all the HTML in the browser.

Is there any method or trick? Any suggestion can help me. Thanks.

My sample code is:

 $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. 
+2
source share
1 answer
  <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> 
+19
source

Source: https://habr.com/ru/post/950698/


All Articles