Link to home
Start Free TrialLog in
Avatar of hwssoft
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.lookupDefaultPrintService();
                // Create the print job
                DocPrintJob job = service.createPrintJob();
                String s="<HTML><HEAD><TITLE> New Document </TITLE></HEAD>"+"<BODY><h1>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.PrintJobFlavorException: 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.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of hwssoft
hwssoft

ASKER