我试图使用Java在Ubuntu 10.10下打印HTML字符串,
使用此代码:
public void printHTMLText(String text) throws Exception {
if (printerName.equals(""))
throw new Exception("no standart printer");
PrintService printService = getCurrentPrinter();
if (printService == null)
helperFunctions
.errorOut("Couldn't print HTML-Text: "
+ text
+ " because no current printer is given or current printer is not available");
else {
DocPrintJob printJob = printService.createPrintJob();
DocFlavor[] avail = printService.getSupportedDocFlavors();
DocFlavor htmlutf8 = null;
for (int i = 0; i < avail.length; i++) {
if (avail[i].toString().equals(
"text/html; charset=\"utf-8\"; class=\"[B\""))
htmlutf8 = avail[i];
helperFunctions.debugOut(avail[i].toString());
}
SimpleDoc doc;
try {
if (htmlutf8 == null)
throw new PrintException(
"Sorry the chosen printer can't deliever text/html; charset=utf-8");
// helperFunctions.debugOut(htmlutf8.getMimeType());
doc = new SimpleDoc(text.getBytes("UTF-8"), new DocFlavor(
htmlutf8.getMimeType(),
htmlutf8.getRepresentationClassName()), null);
printJob.print(doc, null);
helperFunctions.infoOut("Printing HTML: " + text);
} catch (PrintException ex) {
helperFunctions.errorOut(ex.toString());
}
}
}根据getSupportedDocFlavors(),支持HTML打印。
我没有收到Java错误,打印机响应。但我得到的只是一个空白的页面。
我做错了什么?
由于Java没有报告任何错误,所以花了我大量的时间并且没有任何线索。
提前致谢
编辑:更新了pastelink,因为它已过期