views:

20097

answers:

17

How do I print the indicated div (without manually disabling all other content on the page)?

I want to avoid a new preview dialog, so crating a new window with this content is not useful...

The page contains a couple of tables, one of them contains the div I want to print - the table is styled with visual styles for the web, that should not be shown in print....

+14  A: 

Could you use a print stylesheet, and use CSS to arrange the content you wanted printed? Read this article for more pointers.

Paul Dixon
I do have a print style sheet - trying to avoid putting a style rule in all other content...
noesgard
I do have a table with styling on, containg the div in question, If I set the table to display:none - can I still display the div?
noesgard
I think so yes, and you could try marking the rule as !important to boost it!
Paul Dixon
if the table isn't displayed it won't matter even if the div is set to 'display: awesome;'. The parent is hidden and so are the children. Is this a situation where you need a table or is it just for page layout?
sanchothefat
Table is layout only...
noesgard
"Boom, headshot!" by table layouts. =|
ANeves
+2  A: 

hm ... use the type of a stylsheet for printing ... eg:

<link rel="stylesheet" type="text/css" href="print.css" media="print" />

print.css:

div { display: none; }
#yourdiv { display: block; }
Andreas Niedermair
This will not work if the document is not DIV-based or #yourdiv also includes further DIVs.
Gumbo
#yourdiv * { display: ... }if you place this on top of the page and have an up-to-date browser you can then work with more selectors to hide away non divs
Andreas Niedermair
"#yourdiv * { display: ... }" - what should the ... be without breaking the layout?
Greg
This would display inline elements also as block elements. But “#yourdiv, #yourdiv div { display:block }” will do it.
Gumbo
@Gumbo that's my point - it can't be done
Greg
wow "..." is: "fill in what you want"
Andreas Niedermair
@dittodhole - think about it for a minute. What could you put in there that won't break the layout within #yourdiv?
Greg
run-in - but not supported with common browsers. only working values are: inline, block and none (list-item with ie > 5). so i would say: it depends on the layout! but i would prefer Gumbo's solution with my not-knowing-what's-on-the-page view
Andreas Niedermair
The trouble is, with almost any layout you have a mix of block and inline. Even something really simple like this: <p>This <b>is</b> broken.</p> <p>If you choose inline <b>or</b> block</p>
Greg
therefore i would tend to build up 2 different mother-divs like here http://stackoverflow.com/questions/468881/print-div-idprintarea-div-only#468922
Andreas Niedermair
A: 

Use a special Stylesheet for printing

<link rel="stylesheet" href="print.css" type="text/css" media="print" />

and then add a class i.e. "noprint" to every tag which's content you don't want to print.

In the CSS use

.noprint {
  display: none;
}
Peter
A: 

You could use a separate CSS style which disables every other content except the one with the id "printarea".

See CSS Design: Going to Print for further explanation and examples.

Kosi2801
A: 

With CSS 3 you could use the following:

body *:not(#printarea) {
    display: none;
}
Gumbo
Can't use that I'm afraid... (correct me if wrong) the page I'm working on is asp.net 1.1 / DHTML
noesgard
does not work with our beloved browser: ie 6.0
Andreas Niedermair
It depends on the browser whether the :not() selector is already available or not.
Gumbo
IE 6+ must be supported
noesgard
Well then is my proposal just a look into the future … No, seriously. I just mentioned it for the sake of completeness. I know that it’s not practicable right now. Unfortunately.
Gumbo
You could use jQuery and this selector which (I think) would work in IE6 - $('body *:not(#printarea)').style('display', 'none');
David Heggie
+6  A: 

All the answers so far are pretty flawed - they either involve adding class="noprint" to everything or will mess up display within #printable.

I think the best solution would be to create a wrapper around the non-printable stuff:

<head>
    <style type="text/css">

    #printable { display: none; }

    @media print
    {
     #non-printable { display: none; }
     #printable { display: block; }
    }
    </style>
</head>
<body>
    <div id="non-printable">
     Your normal page contents
    </div>

    <div id="printable">
     Printer version
    </div>
</body>

Of course this is not perfect as it involves moving things around in your HTML a bit...

Greg
I think you're right, but how do I kill the styling of the table surrounding the div (if thats possible to remove the styling - I can print the table and exclude the rest of the content quite easily
noesgard
I uses Javascript to pickup the needed content and the CSS like above
noesgard
I prefer using semantic HTML. Print formatting should ideally be handled by CSS. See my answer for how to do this: http://stackoverflow.com/questions/468881/print-div-idprintarea-div-only/2618980#2618980
Bennett McElwee
A: 

If you only want to print this div, you must use the instruction: @media print{ *{display:none;} #mydiv{display:block;} }

A: 

I picked up the content using JavaScript and created a window that I could print in stead...

noesgard
A: 
  1. Give whatever element u wanna print this id "printMe".

  2. Include this script in your head tag:

    <script language="javascript">

    var gAutoPrint = true;

    function processPrint(){

    if (document.getElementById != null){

    var html = '<HTML>\n<HEAD>\n';

    if (document.getElementsByTagName != null){

    var headTags = document.getElementsByTagName("head");

    if (headTags.length > 0) html += headTags[0].innerHTML;

    }

    html += '\n</HE' + 'AD>\n<BODY>\n';

    var printReadyElem = document.getElementById("printMe");

    if (printReadyElem != null) html += printReadyElem.innerHTML;

    else{

    alert("Error, no contents.");

    return;

    }

    html += '\n</BO' + 'DY>\n</HT' + 'ML>';

    var printWin = window.open("","processPrint");

    printWin.document.open();

    printWin.document.write(html);

    printWin.document.close();

    if (gAutoPrint) printWin.print();

    } else alert("Browser not supported.");

    }

    </script>

  3. Call the function

    <a href="javascript:void(processPrint());">Print</a>

A: 

Sandro's method works great.

I tweaked it to allow for allowing multiple printMe links, particularily to be used in tabbed pages and expanding text.

function processPrint(printMe){ <-- calling for a variable here

var printReadyElem = document.getElementById(printMe); <-- removed the quotes around printMe to ask for a variable

<a href="javascript:void(processPrint('divID'));">Print</a> <-- passing the div ID to be printed on to the function to turn the printMe variable into the div ID. single quotes are needed

+4  A: 

With jQuery it's as simple as this:

w=window.open();
w.document.write($('.report_left_inner').html());
w.print();
w.close();
romaninsh
+1  A: 

This works well:

<style type="text/css">
@media print
{
body * { visibility: hidden; }
#printcontent * { visibility: visible; }
#printcontent { position: absolute; top: 40px; left: 30px; }
}
</style>

Note that this only works with "visibility". "display" won't do it. Tested in FF3.

Christian M. M.
+2  A: 

Here is a general solution, using CSS only, which I have verified to work.

@media print {
  body * {
    visibility:hidden;
  }
  #section_to_print, #section_to_print * {
    visibility:visible;
  }
  #section_to_print {
    position:absolute;
    left:0;
    top:0;
  }
}

Alternative approaches aren't so good. Using display is tricky because if any element has display:none then none of its descendants will display either. To use it, you have to change the structure of you page.

Using visibility works better since you can turn on visibility for descendants. The invisible elements still affect the layout though, so I move the section_to_print to the top left so it prints properly.

Bennett McElwee
A: 

Dear Sandro, i thank you for your good code.

all the best gonen

gonen
A: 

HI!

great script Sandro.

But I have a doubt. How can i print a header with a image and a footer in every page printed?

TKS (Obrigado) Ocean

Ocean
A: 

Hi good code I have used in my project but it is opening in new window. I dont want to open in new window i.e wheneve we click it should show directly print option.

Thanks, Ganesh

Ganesh