Results 1 to 6 of 6
- 04-24-2008, 05:10 AM #1
How to print a HTML file in browser look using JAVA
Hello guys, :)
Does anyone know how to print HTML output/file into browser look?
I'm using DocPrintJob and the DocFlavor set to DocFlavor.INPUT_STREAM.AUTOSENSE.
posted below is the code :
public class BasicPrint {
public static void main(String[] args) {
try {
// Open the image file
String testData = "C:/new_page_1.html";
InputStream is = new BufferedInputStream(new FileInputStream(testData));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
// Find the default service
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
System.out.println(service);
// Create the print job
DocPrintJob job = service.createPrintJob();
Doc doc= new SimpleDoc(is, flavor, null);
// Monitor print job events; for the implementation of PrintJobWatcher,
// see e702 Determining When a Print Job Has Finished
PrintJobWatcher pjDone = new PrintJobWatcher(job);
// Print it
job.print(doc, null);
// Wait for the print job to be done
pjDone.waitForDone();
// It is now safe to close the input stream
is.close();
} catch (PrintException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static class PrintJobWatcher {
// true iff it is safe to close the print job's input stream
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
// Add a listener to the print job
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}
}
the printed ouput for this code will be look like this
<html>
<body>
<div style="page-break-after:'always';
background-color:#EEEEEE;
width:400;
height:70">
testPrint</div>
ABCDEFGHIJK<p>
</p>
</body>
</html>
however, the output that i want is the HTML in browser look not HTML code itself.
i've tried to change the DocFlavor into any TEXT_HTML type but it gives error:
if you guys has any idea or solution, can you share with me...:osun.print.PrintJobFlavorException: invalid flavor
Thanks in advanced.
- 04-24-2008, 05:32 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You can use JEditorPane for this.
- 06-19-2008, 01:49 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 1
- Rep Power
- 0
how to print html using browser.
Hello
I dont know if you already solved the problem but I have come face to face with
somewhat same problem and I solved it like this:
I created a temporary file and I set the content of it with the html content that I want to print. then I got the URL of the file and set it to my browsers url. then
I used a small javascript code to print my browser's context. here is the source code that I used.
Browser browser = new Browser(c,SWT.NONE);
File temp = null;
try {
// Create temp file.
temp = File.createTempFile("pattern", ".suffix");
// Delete temp file when program exits.
temp.deleteOnExit();
// Write to temp file
BufferedWriter out = new BufferedWriter(new FileWriter(temp));
out.write(toPrint);
out.close();
} catch (IOException e) {
}
//get the uri and url and set the browsers URL
try {
browser.setUrl(temp.toURI().toURL().toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
browser.execute("javascript:print()");
}
if you have questions dont hesitate to ask...
deniz gul
- 06-19-2008, 01:52 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Hi femme,
It's better to use code tags when you post a code again into the forum. It's much easy to read your code for others.
Have a nice time on our community. :)
- 06-24-2010, 08:29 PM #5
Member
- Join Date
- Jun 2010
- Posts
- 1
- Rep Power
- 0
Hi Femme,
I am new to javascript and eclipse RCP. Could you please post your java script as well ? It would be of great help to me.
- 06-25-2010, 02:36 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
But please don't discuss more about JS in the forum, since this is a Java forum. :)
Similar Threads
-
How to print text file in java(dotmatrix printer)
By yoganeethi in forum Advanced JavaReplies: 4Last Post: 12-01-2010, 01:45 PM -
Include Java file in HTML Page
By kathyc in forum New To JavaReplies: 2Last Post: 03-07-2008, 03:51 AM -
File Save not showing FileSAVE dialog box , getting displayed in browser
By deepdba in forum Java ServletReplies: 0Last Post: 12-06-2007, 06:10 PM -
Print a picture file
By oli001 in forum New To JavaReplies: 0Last Post: 11-26-2007, 01:40 PM -
how to call a JAR FILE from HTML
By leonard in forum Java AppletsReplies: 1Last Post: 08-05-2007, 06:06 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks