views:

1471

answers:

5

I have a J2EE based web application.

In one of the pages there is a button labeled "Print".

My problem is something like this:

User enters tool names for e.g: ToolName1 ToolName2 ToolName3

Then clicks on "Print".

The intended action is that tool details of the 3 tools are retrieved from db and then printed one tool per page. (i.e On clicking this button some processing should take place in the background db retrieval before sending the details to the printer...)

Please suggest how best this task of printing the web page could be done.

Hope the problem is clear.

Thanks in advance...:)

A: 

On the client-side I guess? If so, there's nothing about Java but about JavaScript. Simply call the window.print() method and it will prompt a print dialog window.

gizmo
I am sorry I think I didnt make the question clear....have added more info to the question...
ria
+3  A: 

You could make the button call a bit of Javascript:

<script>
    function printPage() {
        window.print();  
    }
</script>

You'd call this using something like:

<form>
<input TYPE="button" onClick="printPage()">
</form>

or directly as:

<input TYPE="button" onClick="window.print()">

If you need to format the output onto multiple pages you would use the page-break-before CSS property. For example:

<style>
    div.tool {
        page-break-before: always
    }
</style>

<div class="tool" id="tool1">
...
</div>
<div class="tool" id="tool2">
...
</div>

You could put this in a "print" specific stylesheet as suggested by Guido García.

Nick Pierpoint
I am sorry I think I didnt make the question clear....I need to do some processing in the background before sending the details to the printer...That page actually has some tool names, details of which are stored in the database. I need to fetch the details and print each tools details in a page
ria
I assume you're not asking how to retrieve data from the database. Are you asking how to get each set of results on a different page?
Nick Pierpoint
+3  A: 

On the client side you can just call window.print();

Other tips:

  • You could use a printer friendly CSS to customize your page presentation :
<link rel="stylesheet" type="text/css" media="print" href="print.css" />
  • You could use a server side component that generates a PDF version of the page. In many cases it is interesting to have a deep control of the final presentation.
Guido
Thanks.I have modified the question to make it make the problem more clear.
ria
A: 

If your to be printed page needs extra logic you could do either of the following things:

  • include the calculated data in your regular page, but hide it (style="visibility:hidden") then make it visible with a print.css (media=print) as described by Guido so it only shows up when you print.
  • Create a new page you can link to from the regular page that has the calculated data

Both have their advantages.

I also suggest that you don't only rely on Javascript. It can be rather confusing when a print button doesn't print because you're not running JS. I suggest you create a regular html link that goes to the to-be printed page where the user can use the browser's print button. Hide this link with javascript and replace it with the javascript dependant button. This will make sure the button only shows up when javascript is enabled.

borisCallens
Thanks BorisMy problem is something like this:User enters tool names for e.g:ToolName1ToolName2ToolName3Then clicks on "Print".The intended action is that tool details of the 3 tools are retrieved from db and then printed one tool per page.Hope i made the problem clear.
ria