0
votes

Is there any good workaround for the following code that can be used to print to different printers such as receipt printer, id card printer and inkjet/laser printer? I dont want to use the pdf for saving the data and printing instead directly print from a web form or database.

I tried this but didnt work.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<script type="text/javascript"> 
function Print(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> 
<input type="button" value="Print" onClick="javascript:Print('print')" /> 
<div id="print"> Content to be printed </div>
</body>
</html>
1
this prints an header but I foot want those to be printed. I know printing header/footer is a browser side issue but still I would like to know he there is any way by which the code prevents the printing automatically.Ayan

1 Answers

0
votes
<script>
function Print(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> 

<input type="button" value="Print" onclick="javascript:Print('print')" />

<div id="print">
 Content to be printed
</div>