Ubuntu中的Java打印
我正在开发一个在Ubuntu中打印数据的小应用程序,问题是我的应用程序在windows中运行良好,使用:Ubuntu中的Java打印,java,ubuntu,printing,Java,Ubuntu,Printing,我正在开发一个在Ubuntu中打印数据的小应用程序,问题是我的应用程序在windows中运行良好,使用: PrintService service = PrintServiceLookup.lookupDefaultPrintService(); FileInputStream fis = new FileInputStream(myfile); DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; DocPrintJob job = se
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
FileInputStream fis = new FileInputStream(myfile);
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(fis, flavor, null);
job.print(doc, null);
fis.close();
然而在Ubuntu中,它只是不打印。对于我正在使用的打印API,Linux打印是否有任何特殊配置?还是我遗漏了什么?我认为,您的打印机在操作系统中不是默认安装的。检查您的“服务”是什么。 您也可以从“打印”对话框中选择打印机,如下所示:
PrintRequestAttributeSet pras =
new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;
PrintRequestAttributeSet aset =
new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
aset.add(new Copies(1));
aset.add(Sides.ONE_SIDED);
aset.add(Finishings.STAPLE);
PrintService printService[] =
PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService =
PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200, 200,
printService, defaultService, flavor, pras);
if (service != null) {
try {
FileInputStream fis = new FileInputStream("c://test.txt");
DocAttributeSet das = new HashDocAttributeSet();
Doc doc1 = new SimpleDoc(fis, flavor, das);
DocPrintJob job1 = service.createPrintJob();
try {
job1.print(doc1, pras);
} catch (PrintException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
if (Desktop.isDesktopSupported()){
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.PRINT))
{
try {
File html1 = new File("c://file1.html");
desktop.print(html1);
desktop.print(html2);
} catch (IOException e) {
e.printStackTrace();
}
}
有些打印机不支持文本,只支持图像。您还可以使用操作系统本机方法简单地打印html文件,如下所示:
PrintRequestAttributeSet pras =
new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;
PrintRequestAttributeSet aset =
new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
aset.add(new Copies(1));
aset.add(Sides.ONE_SIDED);
aset.add(Finishings.STAPLE);
PrintService printService[] =
PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService =
PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200, 200,
printService, defaultService, flavor, pras);
if (service != null) {
try {
FileInputStream fis = new FileInputStream("c://test.txt");
DocAttributeSet das = new HashDocAttributeSet();
Doc doc1 = new SimpleDoc(fis, flavor, das);
DocPrintJob job1 = service.createPrintJob();
try {
job1.print(doc1, pras);
} catch (PrintException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
if (Desktop.isDesktopSupported()){
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.PRINT))
{
try {
File html1 = new File("c://file1.html");
desktop.print(html1);
desktop.print(html2);
} catch (IOException e) {
e.printStackTrace();
}
}
}如果我将“myfile”替换为指向文本文件的路径,则您的代码在这里工作。您的系统中是否正确安装了打印机?你能在其他应用程序中打印吗?e、 g.gedit,gvim…?我可以使用cat“filename”| lpr从终端打印…通常“myfile”是来自打开的文件对话框的路径字符串…我删除了“eclipse”标记。这个标签是专门用来回答关于Eclipse(IDE)的问题的,我在Linux(Ubuntu12.10)上也遇到过同样的问题。我唯一能打印的文件是PDF和PS文件。纯文本、HTML、图像生成了一个空白页。也尝试了该对话框,它根本不起作用,我在那里看到打印机,当我单击“打印”时,没有作业发送到我的打印队列…:(对不起,我忘了将DocFlavor更改为“TEXT\u PLAIN\u UTF\u 8”。请尝试此操作,或者尝试将数据打印为html。我将此添加到答案中。