Question

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...:)

Was it helpful?

Solution

The print button should submit the form to the server where you prepare the output you want to print (the tool details etc.). This is rendered as per usual using your JSP page or whatever.

To complete the job, put a call to window.print() on the results page and have it fired on the page load (or document ready event).

Edit: Hmm. I should have looked at the date. Oh well, the advice stands.

OTHER TIPS

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top