Reputation: 7984
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...)
How best this task of printing the web page could be done?
Upvotes: 2
Views: 9874
Reputation: 4021
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.
Upvotes: 2
Reputation: 17779
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.
Upvotes: 4
Reputation: 93397
If your to be printed page needs extra logic you could do either of the following things:
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.
Upvotes: 0
Reputation: 47695
On the client side you can just call window.print();
Other tips:
<link rel="stylesheet" type="text/css" media="print" href="print.css" />
Upvotes: 3
Reputation: 11909
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.
Upvotes: 0