我正在尝试将一个简单的pdf文件打印到在CUPS上注册的远程打印机上,并且该打印机不打印文本,而是打印类似Webdings的字符,即使pdf文件仅包含一个“ hello世界”字符串,它可以打印许多页(10页)。我的猜测是打印机正在打印ByteStream。我是javax的新手,所以它可能是一个愚蠢的错误,但是我进行了很多搜索并尝试了多种解决方案。
public class PrintTest2 {
public static void main(String[] args) throws Exception{
try {
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintServiceAttributeSet aset2 = new HashPrintServiceAttributeSet();
aset.add(new Copies(2));
aset2.add(new PrinterURI(new URI("ipp://hostname/printers/PRINTER_NAME")));
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor,aset2);
for(PrintService printService: printServices){
System.out.println(printService.getName());
}
PrintService service = PrintServiceLookup.lookupPrintServices(flavor,aset2)[3];
System.out.println(service.getName());
// Create the print job
DocPrintJob job = service.createPrintJob();
InputStream in = new FileInputStream("MyPath\\test.pdf.lnk");
Doc doc = new SimpleDoc(in, flavor,null);
job.print(doc, aset);
} //catch (PrintException e) {
//}
catch (IOException e) {
}
}}
我试图将bufferredreader传递给SimpleDoc构造函数,例如:
Doc doc = new SimpleDoc(new BufferedReader(new InputstreamReader(in, "UTF-8")), flavor,null);
但随后引发了以下异常:
Exception in thread "main" java.lang.IllegalArgumentException: data is not of declared type
为解决此问题,我尝试将DocFlavor更改为DocFlavor.INPUT_STREAM.BYTE_ARRAY.AUTOSENSE
没有任何改变。
我还尝试了cups4j文档中的以下代码,但它也打印了火星字符集,并且“副本”属性不起作用,并且用尽了所有打印机纸张。
public static void main(String[] args) throws Exception{
CupsClient cupsClient = new CupsClient(hostname, 631);
CupsPrinter cupsPrinter = cupsClient.getPrinters().get(2); // Maybe it's bad practice, but I wanted the 3rd printer.
System.out.println("Printer No:" + cupsPrinter.getName());
InputStream inputStream = new FileInputStream("C:MYPAth\\test.pdf.lnk");
PrintJob printJob = new PrintJob.Builder(inputStream).copies(2).build();
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(2));
Map attributes = new HashMap();
attributes.put("job-attributes", "print-quality:enum:3#media:keyword:iso_a5_148x210mm");
printJob.setAttributes(attributes);
PrintRequestResult printRequestResult = cupsPrinter.print(printJob);
}
我应该将.pdf.lnk文件扩展名更改为.pdf吗?我没有这样做是因为当我这样做时,Adobe Reader无法读取该文件。
P.S .:如果我将.txt文件提供给FileInputStream,它将正确打印包含的字符串。然后我尝试将Docflavor设置为INPUT_STREAM.PDF,然后再次获得
Exception in thread "main" java.lang.IllegalArgumentException: data is not of declared type