150

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?

Debiprasad
  • 5,895
  • 16
  • 67
  • 95

6 Answers6

212

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

I came across another elegant solution for this:

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

<div id="printableArea">
      <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

asprin
  • 9,579
  • 12
  • 66
  • 119
126

Here is a pure css version

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

<div class="example-print">You only see me in the print</div>
Jonathan
  • 2,700
  • 4
  • 23
  • 41
2ne
  • 5,766
  • 9
  • 30
  • 51
  • 1
    I like that you don't need to open a new window and all that crap. – Robusto Apr 16 '17 at 22:01
  • 1
    @Robusto agree in one hand, but I don't like how it's not so good for saving the page (without needing to print), not to mention when there's paging to be handled. preparing a page to "print" is ambiguous in that sense. – cregox Apr 23 '17 at 13:17
86

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();
Community
  • 1
  • 1
Jaay
  • 2,103
  • 1
  • 16
  • 34
  • 9
    This solution requires JQuery which is not mentioned (or tagged) in the question. You should specify this dependency in your answer. – musefan Oct 18 '13 at 11:08
  • 9
    replacing `$('.report_left_inner').html()` by vanilla js would make this the best answer on the page for simplicity and robustness – zeachco Aug 21 '15 at 21:24
  • 2
    `w.document.write(document.getElementsByClassName('report_left_inner')[0].innerHTML);` – karaxuna Jan 13 '16 at 14:45
  • 6
    This is excellent. One point of caution though: you'll need to check and inject your style rules separately, or it'll fall back to bare HTML. For that I personally did `document.write('');` before the `document.write()` given here. – cst1992 Aug 19 '17 at 08:14
  • This is interesting but when I tried it from an Angular component I didn't get the CSS applied to the new window. Also, the images don't have time to load between the write call and the print call so they don't appear until after the print dialog is done. – AlanObject Jan 25 '20 at 23:15
  • This is not working in current versions of chrome, also, popups may be blocked by the users browser. – Felix Hagspiel Jun 05 '20 at 12:43
  • This creates a visible tab in the browser under the print dialog, which is pretty ugly. – Jaromír Adamec Feb 16 '23 at 17:49
11

I Want See This

Example http://jsfiddle.net/35vAN/

    <html>
<head>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </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 id="mydiv">
    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 id="anotherdiv">
    Nor will this.
</div>

<input type="button" value="Print Div" onclick="PrintElem('#mydiv')" />

</body>

</html>
Panchal Deep
  • 247
  • 3
  • 13
10

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 id="block1">
<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 id="block2">
    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>
Butani Vijay
  • 4,181
  • 2
  • 29
  • 61
2

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');
Alejo JM
  • 921
  • 7
  • 12
  • Hi Alejo. Clever solution, but it doesn't work in IE. Could you please help me with this question: https://stackoverflow.com/questions/49410902 THaNKS! – codeispoetry Mar 21 '18 at 15:56