Forums Register Login

How to print a HTML file to a browser look

+Pie Number of slices to send: Send
Hello guys,

Does anyone know how to print HTML output/file into browser look? :roll:
I'm using DocPrintJob and the DocFlavor set to DocFlavor.INPUT_STREAM.AUTOSENSE.

posted below is my 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 output 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:

sun.print.PrintJobFlavorException: invalid flavor



if you guys has any idea or solution, can you share with me
really appreciated it...

thanks in advanced
+Pie Number of slices to send: Send
Hi ammameiya Equ, welcome to Javaranch,
sorry cant help you with your problem, but I am sure someone else will.
Just a suggestion use UseCodeTags for your code.

Hope this helps
+Pie Number of slices to send: Send
oooppps... next time I'll be more careful
+Pie Number of slices to send: Send
Thanks for adding the code tags. But they don't accomplish much if the code you post isn't actually formatted/indented. Please do that next time.

As to your question, I don't think there's a printer driver that can handle HTML, so this won't work. What you need is a Java component that can interpret HTML, and then print it. There's a Java web browser component called "Lobo" which may be worthwhile checking out.

Alternatively, it may be possible to start a native web browser with that file from the command line (using Runtime.exec or ProcesBuilder), and have it print it.
+Pie Number of slices to send: Send
Have you considered using a servlet?
+Pie Number of slices to send: Send
Servlet??? How? Any idea... :roll:
+Pie Number of slices to send: Send
Download Apache Tomcat 6 you will need it to run your app. It contains some good servlet tutorials. You will probably also need an IDE like eclipse http://www.eclipse.org/downloads/ (make sure you get the EE version).
+Pie Number of slices to send: Send
How would running the above code as a servlet help with printing?
[ May 01, 2008: Message edited by: Ulf Dittmer ]
+Pie Number of slices to send: Send
The way I read it was that ammameiya wants a web output not just a printout


the printed output for this code will be look like this

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

 
Well THAT's new! Comfort me, reliable tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3392 times.
Similar Threads
PrintJobListener Question
Problem with printing multiple files
Print Code help
Java Print Problem
javax.print.PrintException: already printing
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:27:59.