hwssoft
asked on
Java printing: Invalid Flavor when trying to print HTML content
I need to print the contents of an EditorPane. For this, I've searched and found example code for using DocFlavors from the javax.print package.
Here's a code fragment:
try{
DocFlavor flavor = DocFlavor.STRING.TEXT_HTML ;
PrintService service = PrintServiceLookup.lookupD efaultPrin tService() ;
// Create the print job
DocPrintJob job = service.createPrintJob();
String s="<HTML><HEAD><TITLE> New Document </TITLE></HEAD>"+"<BODY><h 1>Hello there "
+"</h1></BODY></HTML>";
//error here Doc doc = new SimpleDoc(s, flavor, null);
System.out.println("Doc flavor is "+doc.getDocFlavor());
// Print it
job.print(doc, null);
}catch(exception e)
{
e.printStackTrace();
}
At the line marked 'error here', I get a ''sun.print.PrintJobFlavor Exception: invalid flavor"
This code is adapted from a similar one (http://javaalmanac.com/egs/javax.print/WaitForDone.html)
If you run the code listing at the above link, the program works for printing 'image/gif' and loading a gif image from a file inputstream.
If you change it to any form of text (text/plain, text/html etc) and try loading a text or HTML file-you get the same error.
Every place on the net seems to detail this error when using DocFlavor.
Any help would be appreciated.
Here's a code fragment:
try{
DocFlavor flavor = DocFlavor.STRING.TEXT_HTML
PrintService service = PrintServiceLookup.lookupD
// Create the print job
DocPrintJob job = service.createPrintJob();
String s="<HTML><HEAD><TITLE> New Document </TITLE></HEAD>"+"<BODY><h
+"</h1></BODY></HTML>";
//error here Doc doc = new SimpleDoc(s, flavor, null);
System.out.println("Doc flavor is "+doc.getDocFlavor());
// Print it
job.print(doc, null);
}catch(exception e)
{
e.printStackTrace();
}
At the line marked 'error here', I get a ''sun.print.PrintJobFlavor
This code is adapted from a similar one (http://javaalmanac.com/egs/javax.print/WaitForDone.html)
If you run the code listing at the above link, the program works for printing 'image/gif' and loading a gif image from a file inputstream.
If you change it to any form of text (text/plain, text/html etc) and try loading a text or HTML file-you get the same error.
Every place on the net seems to detail this error when using DocFlavor.
Any help would be appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I consider the link I posted also valuable
- Main idea: Is that flavor supported on your system/printer?
- shows how to list the available flavors
- Main idea: Is that flavor supported on your system/printer?
- shows how to list the available flavors
ASKER
I found a solution here:
http://java.sun.com/jsp_utils/PrintPage.jsp?url=http%3A%2F%2Fjava.sun.com%2Fproducts%2Fjava-media%2F2D%2FforDevelopers%2Fsdk12print.html
This worked for me.
http://java.sun.com/jsp_utils/PrintPage.jsp?url=http%3A%2F%2Fjava.sun.com%2Fproducts%2Fjava-media%2F2D%2FforDevelopers%2Fsdk12print.html
This worked for me.
http://groups.google.com/groups?hl=nl&lr=&frame=right&th=c0ca4fe3a88b9e0e&seekm=3D85EE2C.A90D48FE%40jalice.ch#link1