Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a specific HTML element and not the full page in Chrome or Firefox?

Let's say I'd like to print (or convert to PDF) a particular area (e.g., a div) of a complex HTML page. How to do that in Chrome or Firefox? I didn't find a specific option from the "inspect element" function.

like image 311
f_ficarola Avatar asked Mar 24 '16 15:03

f_ficarola


People also ask

How do I print a portion of a web page in Firefox?

Just select the desired text on the current page and press CTRL+P. This will bring up the Print dialog, where you can simply select the "Selection" option there. This will print out only the selected text. I just did this on my Firefox 30.0 and it worked perfectly.

How do I print a specific section in HTML?

You can use this function for print a specific area of web page or full web page content. Use printPageArea() function on onclick event of print button element and provide the content area div ID which you want to print.


1 Answers

I know it's a quite an old topic, but here is what worked for me:

<div id="parent_div">
    <p>NOT to be printed</p>
    <div id="print_div">
        Print this!
    </div>
    <button id="print_now" onclick="PrintDiv('print_div')">
</div>

And the JS function:

function PrintDiv(div_id) {
    $('body').css("visibility", "hidden");
    $("#"+div_id).css("visibility", "visible");
    window.print();
    $('body').css("visibility", "visible");    
}

If you are not using jQuery, you can still change the CSS with:

document.getElementById("body").style.visibility = "hidden";
document.getElementById(div_id).style.visibility = "visible";
like image 134
DevRenanGaspar Avatar answered Sep 29 '22 21:09

DevRenanGaspar