aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Printing a open window using javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Printing a open window using javascript" Watch "Printing a open window using javascript" New topic
Author

Printing a open window using javascript

seshu Palamanti
Ranch Hand

Joined: Jul 03, 2004
Posts: 59
Hi Every one,
I have a open window which contains the data from a jsp that needs to be printed. There are 2 things about the window
1) It has been opened using a javascript like this

var win = window.open(url, name,
'width=' + width + ', height=' + height + ', ' +
'left=' + left + ', top=' + top + ', ' +
'location=no, menubar=no, ' +
'status=yes, toolbar=no, scrollbars=no, resizable=yes');
2) The content which is a huge list of data is available on the page with internal scroll bars using a css like this
div#scrollableContent {
height:380px;
overflow-x:hidden;
overflow-y:scroll;
}
<div id="scrollableContent">

As mentioned earlier the aim of this exercise is to print the window containing whole content currently when i just use
window.print(); it only prints the first few lines with internal scroll bars for the div. I changed "scrollbars=yes" in the open window
function and also changed overflow-y:hidden; in css which gets the desired print but i wanted to get any help to do this using javascript
dynamically as this should only be for printing the window and not for display.

Any help or direction is really appreciated.

Regards
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

If you print from JavaScript, you're going to get whatever the browser decides to print. You don't have any control over it.

If you want to control exactly what gets printed, you're better of formatting a PDF from the data when the user asks to print the information.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

But you can attempt to control it with a CSS stylesheet defined via <style media="print">, right? So I don't think Javascript should be necessary at all. Just make a CSS stylesheet which specifies how the document should look when it's printed, instead of trying to manipulate the regular stylesheet on the fly before printing.

Of course, yeah, there are still documents which the browsers can't print right. Maybe seshu's document is one of those.
seshu Palamanti
Ranch Hand

Joined: Jul 03, 2004
Posts: 59
Thanks for your fast reply Paul, i really appreciate it....as you mentioned that window.print() will only print what ever is there on the window at that movement is there a way we could dynamically change the window scroll bars and also css content before we call print() so that content prints out? Again any help will be appreciated.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

Yes, CSS is a possibility. Good idea to bring it up. It just sounded to me like he wanted even more control over the formatting.
seshu Palamanti
Ranch Hand

Joined: Jul 03, 2004
Posts: 59
Paul,
Thanks again for your super quick reply....is there a way to put on scrollbars to an open window which was opened with 'scrollbars=no'?

var win = window.open(url, name,
'width=' + width + ', height=' + height + ', ' +
'left=' + left + ', top=' + top + ', ' +
'location=no, menubar=no, ' +
'status=yes, toolbar=no, scrollbars=no, resizable=yes');
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

seshu Palamanti wrote:Thanks for your fast reply Paul, i really appreciate it....as you mentioned that window.print() will only print what ever is there on the window at that movement is there a way we could dynamically change the window scroll bars and also css content before we call print() so that content prints out? Again any help will be appreciated.


I don't think you're quite following. My suggestion is to have two CSS stylesheets. One is the standard one which controls how data appears on the screen and the other one (declared with media="print") controls how data appears when printed. No "dynamically" (which as far as I can see is a code word for "some complex process which I don't understand") required.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You should use CSS Print Media to change the page css when it is to be printed.

You can make things that overflow to be visible.

Eric
seshu Palamanti
Ranch Hand

Joined: Jul 03, 2004
Posts: 59
Paul,
Thanks again.....sorry if dynamic might have mis represented what I was trying to look for.. i get your idea here...of having two types of css one each for display and print.
seshu Palamanti
Ranch Hand

Joined: Jul 03, 2004
Posts: 59
Thanks for all your help..by creating another css and overriding the classes as desired i was able to achieve what i was looking for. I added that link in the webpage and when window.print() was called this print css would be used and this would happen with out any further code.

<LINK href="${pageContext.request.contextPath}/css/print.css" media="print" rel="stylesheet" type="text/css">

regards
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Printing a open window using javascript
 
Similar Threads
Regarding Scrollable Table
Weird behavior of pop-up window
Scrollable table
DIV tag for vertical scrollbar causing blank space at the top of the DIV
Keeping DIV tags Displayed Correctly