I want to print some HTML content, when the user clicks on a button. Once the user clicks on that button, the print dialog of the browser will open, but it will not print the webpage. Instead, it will print the some other HTML content which is not displayed on the page.

While asking this question, there are few solutions coming into my mind. But I am not sure whether those are good ideas or something better can be done. One of those solutions are: I can keep this HTML content in a div and make it display: to print, but display: none to screen. All other elements on the webpage can be made to display: none for print and display: for the screen. And then call to print.

Any better idea?

3

6 Answers

@media print { .noPrint{ display:none; } } h1{ color:#f6f6; }
<h1> print me </h1> <h1> no print </h1> <button onclick="window.print();"> Print Me </button>

I came across another elegant solution for this:

Place your printable part inside a div with an id like this:

<div> <h1>Print me</h1> </div> <input type="button" onclick="printDiv('printableArea')" value="print a div!" /> 

Now let's create a really simple javascript:

function printDiv(divName) { var printContents = document.getElementById(divName).innerHTML; var originalContents = document.body.innerHTML; document.body.innerHTML = printContents; window.print(); document.body.innerHTML = originalContents; } 

SOURCE : SO Answer

11

Here is a pure css version

.example-print { display: none; } @media print { .example-screen { display: none; } .example-print { display: block; } }
<div>You only see me in the browser</div> <div>You only see me in the print</div>
3

According to this SO link you can print a specific div with

w=window.open(); w.document.write(document.getElementsByClassName('report_left_inner')[0].innerH‌​TML); w.print(); w.close(); 
6

I Want See This

Example

 <html> <head> <script type="text/javascript" src="" > </script> <script type="text/javascript"> function PrintElem(elem) { Popup($(elem).html()); } function Popup(data) { var mywindow = window.open('', 'my div', 'height=400,width=600'); mywindow.document.write('<html><head><title>my div</title>'); /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />'); mywindow.document.write('</head><body >'); mywindow.document.write(data); mywindow.document.write('</body></html>'); mywindow.print(); mywindow.close(); return true; } </script> </head> <body> <div> This will be printed. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a quam at nibh adipiscing interdum. Nulla vitae accumsan ante. </div> <div> This will not be printed. </div> <div> Nor will this. </div> <input type="button" value="Print Div" onclick="PrintElem('#mydiv')" /> </body> </html> 
1

Below Code May Be Help You :

<html> <head> <script> function printPage(id) { var html="<html>"; html+= document.getElementById(id).innerHTML; html+="</html>"; var printWin = window.open('','','left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status =0'); printWin.document.write(html); printWin.document.close(); printWin.focus(); printWin.print(); printWin.close(); } </script> </head> <body> <div> <table border="1" > </tr> <th colspan="3">Block 1</th> </tr> <tr> <th>1</th><th>XYZ</th><th>athock</th> </tr> </table> </div> <div> This is Block 2 content </div> <input type="button" value="Print Block 1" onclick="printPage('block1');"></input> <input type="button" value="Print Block 2" onclick="printPage('block2');"></input> </body> </html> 
1

If you add and remove the innerHTML, all javascript, css and more will be loaded twice, and the events will fire twice (happened to me), is better hide content, using jQuery and css like this:

function printDiv(selector) { $('body .site-container').css({display:'none'}); var content = $(selector).clone(); $('body .site-container').before(content); window.print(); $(selector).first().remove(); $('body .site-container').css({display:''}); } 

The div "site-container" contain all site, so you can call the function like:

printDiv('.someDiv'); 
1