views:

113

answers:

4

I have a page from which the user will be able to print. However, the page which will get printed is not the one the user is viewing, but rather a new one I'd like to generate on the background and (possibly) only show the print dialog for it.

Just to make things clear:

  1. User is on "View.aspx" and clicks my Print button (not the browser's one).
  2. The Print button loads the content of "Printable.aspx" and displays a print dialog for it while the user is still on "View.aspx".

FYI, what I'm trying to avoid is to have the "Printable.aspx" open in a new window and then show its print dialog.

Thanks in advance.

+12  A: 

Use a combination of MEDIA tags in CSS to show/hide objects for printing.

<STYLE type="text/css">
@media print {
   .PrintOnly {font-size: 10pt; line-height: 120%; background: white;}
}
@media screen {
   .PrintOnly {display: none}
}
</STYLE>

You can make controls that are style Display:none on media screen, so the user only sees them when printing.

<DIV class="PrintOnly>
This control will only show up during printing
</DIV>

Any of your controls can be classed as "PrintOnly" so you only see them when printing. You just need to have the css class defined once for "@media screen" and once for "@media print" to ensure they behave differently.

You can also bring in an entire stylesheet for print-only.

<LINK rel="stylesheet" type"text/css" href="screen.css" media="screen">
<LINK rel="stylesheet" type"text/css" href="print.css" media="print">
Carter
+1: This way you don't even need a second page, just a second stylesheet. The same page will simply render differently through the printer.
David
I would love a working example of this. I have several controls and containers I wouldn't want to be printed, but have absolutely no idea how I would go around effectively doing so. Thanks.
Rafael Belliard
@Rafael: I updated the example to be non-DIV specific.
Carter
What if the printable version containts elements not yet loaded on the screen version? Should I load it via something like Ajax or just load a separate page on a hidden panel, like user378806 suggested? Thanks.
Rafael Belliard
@Rafael: If you don't want to mess around with IFRAMEs you can just add "<body onload="window.print();history.go(-1);">" to your "Printable.aspx" page. It will print and then return to the previous screen. It will work better in browsers that don't support IFRAMEs.
Carter
Thanks. This worked beautifully.
Rafael Belliard
A: 

I don't think what you are trying for is possible. But I could be wrong. Though the better way to approach this is to use a different css stylesheet.

spinon
A: 

You could load the page into a hidden IFrame element, then use javascript to execute the print function of that page.

But, like @carter suggested, CSS is the better approach. Why load duplicate content more than once when you can just restyle the current content?

myermian
What if the data to be displayed is not loaded on the current page, and the printable version contains all its details: would be better to load the remaining content via something like ajax or just load the content on a hidden panel?
Rafael Belliard
+1  A: 

When the Print button is clicked, add the Printable.aspx into a hidden panel of view.aspx. Respond by adding javascript into the onload event of view.aspx & print the hidden panel with window.print()

loxxy