在 Mac OS X (Lion) 下,这种打印方法可以正常工作。在 Windows 下,它什么也不做;它没有找到任何打印机,并且菜单列表保持空白。
我使用这个类来发送我的页面:
new SelectPrinterDialog(this, new PrintContent(this, ord_id, this.sessionID));
在课堂PrintContent
上,我使用:
private DocFlavor flavor;
和
PrintService[] getPrinters(){
查找打印机。
public class PrintContent {
private static final boolean TEXT = false;
private OrdiniFrame frame;
private String content;
private long ordID;
private long sessionID;
private DocFlavor flavor;
public static final String PDF_PRINT_FILE = "__temp_print_file.pdf";
public static final String TXT_PRINT_FILE = "__temp_print_file.txt";
public static final String HTML_PRINT_FILE = "__temp_print_file.html";
public PrintContent(OrdiniFrame frame, long ordID, long sessionID){
this.frame = frame;
this.ordID = ordID;
this.sessionID = sessionID;
if(TEXT){
this.flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII;
this.generateText();
}
else {
this.flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
this.generateHTML();
}
}
public PrintService[] getPrinters(){
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
//aset.add(MediaSizeName.ISO_A8);
return PrintServiceLookup.lookupPrintServices(flavor, aset);
}
public XHTMLPrintable createRender () {
try{
XHTMLPanel renderer = new XHTMLPanel();
renderer.getSharedContext().setDPI(300f);
renderer.setDocument(new File(HTML_PRINT_FILE));
JFrame f = new JFrame();
f.getContentPane().add(renderer, "Center");
f.setVisible(true);
return new XHTMLPrintable(renderer);
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
public void print(PrintService service){
try{
float paperWidth = 62.0f;
// paperHeight aumenta l'altezza del foglio
float paperHeight = 70.0f;
paperWidth *= 2.83;
paperHeight *= 2.83;
//val printJob = PrinterJob.getPrinterJob
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintService(service);
PageFormat pageFormat = printJob.defaultPage();
pageFormat.setOrientation(PageFormat.PORTRAIT);
Paper labelPaper = pageFormat.getPaper();
labelPaper.setSize(paperWidth, paperHeight);
labelPaper.setImageableArea(+30,+30,paperWidth, paperHeight);
pageFormat.setPaper(labelPaper);
Book book = new Book();
book.append(this.createRender(), pageFormat);
printJob.setPageable(book);
PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
attributeSet.add(new PrinterResolution(300, 300, ResolutionSyntax.DPI));
printJob.print(attributeSet);
}catch(Exception ex){
ex.printStackTrace();
}
我究竟做错了什么?
更新:我已经解决了。问题出在标签 html-><meta charset="utf-8"/>
中!我在创建文件 .bat 以执行 .jar 后发现此错误。