Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java打印问题_Java_Ubuntu_Printing - Fatal编程技术网

Java打印问题

Java打印问题,java,ubuntu,printing,Java,Ubuntu,Printing,我正在用Java打印Ubuntu10.10下的HTML字符串, 使用此代码: public void printHTMLText(String text) throws Exception { if (printerName.equals("")) throw new Exception("no standart printer"); PrintService printService = getCurrentPrinter(); if (printSe

我正在用Java打印Ubuntu10.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());
        }

    }
}
public void printHTMLText(字符串文本)引发异常{
if(printerName.equals(“”)
抛出新异常(“无标准打印机”);
PrintService PrintService=getCurrentPrinter();
if(printService==null)
助手函数
.errorOut(“无法打印HTML文本:”
+正文
+“因为没有提供当前打印机或当前打印机不可用”);
否则{
DocPrintJob printJob=printService.createPrintJob();
DocFlavor[]avail=printService.getSupportedDocFlavors();
DocFlavor htmlutf8=null;
for(int i=0;i
根据getSupportedDocFlavors(),支持HTML打印。 我没有收到Java错误,打印机也会响应。但我得到的只是一个空页面

我做错了什么? 因为Java没有报告任何错误,所以已经花费了我相当多的时间,而且没有任何线索

提前谢谢

编辑:更新了粘贴链接,因为它已过期

请尝试使用。即使您的程序与Swing无关。 具有HTML支持并扩展了
JTextComponent
,因此它最适合该工作。
Oracle关于打印的教程:。

您好,我现在无法测试,但它看起来像是打印功能打开了一个打印对话框。但由于这是一个服务器程序,将由android平板电脑进行指示,因此它是一个普通的控制台程序,不需要一些图形用户界面或用户交互。有一个重载的方法ich接受一个
showPrintDialog
boolean属性。如果您的服务器具有某种GUI层(甚至桌面仿真),则可以使用此方法。只需公开一个web服务或其他东西来激活打印功能。从Javadocs:注意:在无标题模式下,将忽略showPrintDialog和interactive参数,并且不显示任何对话框