• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to print a HTML file to a browser look  RSS feed

 
ammameiya Equ
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ammameiya Equ
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oooppps... next time I'll be more careful
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adam J Smith
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you considered using a servlet?
 
ammameiya Equ
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet??? How? Any idea... :roll:
 
Adam J Smith
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would running the above code as a servlet help with printing?
[ May 01, 2008: Message edited by: Ulf Dittmer ]
 
Adam J Smith
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!