Print a div contents in Php

If You want to print the section or Div contents, then please do the following…

By using the following code you can print the contents in a div or section.

 

Html  Section:

// The Printing button. On click this button the print preview opens


<div class="row">
      <div class="col-md-12"> 
              <button class="xshr_print_section_btn" onclick="javascript:printDivContents('xshr_print_section')">Print Details</button></div>
</div>

<!-- html Ends -->

Script Section:

<!-- For Print the Contents Starts -->

<script language="javascript" type="text/javascript">

function printDivContents(divID) {

//Get the HTML of div
var divElements = document.getElementById(divID).innerHTML;

//Get the HTML of whole page
var oldPage = document.body.innerHTML;

//Reset the page's HTML with div's HTML only
document.body.innerHTML = 
"<html><head><title></title></head><body>" + 
divElements + "</body>";

//Print Page
window.print();

//Restore orignal HTML
document.body.innerHTML = oldPage;


}
</script>

<!-- For Print the Contents / Ends-->

 

 

also solution available at: how to print a Div content in javascript

2 thoughts on “Print a div contents in Php

  1. #We can use this alse

    var base_url="request->webroot; ?>";
    
    function printDivContents(divID="xshr_print_section") {
    
        //Get the HTML of div
        var divElements = document.getElementById(divID).innerHTML;
        
        Popup(divElements);
    }
    
    function Popup(data) {
        var mywindow = window.open('', 'new div', 'height=400,width=600');
        mywindow.document.write('');
        mywindow.document.write('');
        mywindow.document.write('');
        mywindow.document.write(data);
        mywindow.document.write('');
    
        mywindow.print();
    
        return true;
    }
    
    

Leave a Comment