Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing a open window using javascript

 
seshu Palamanti
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic