0
我已经安装了doPDF打印机驱动程序,并且我想从Java将它用于将HTML转换为PDF。Java将HTML打印为PDF
PrintService service = getPrinterByName("doPDF");
DocPrintJob printJob = service.createPrintJob();
Doc document = new SimpleDoc(conn.getInputStream(), DocFlavor.INPUT_STREAM.AUTOSENSE, null);
PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
File f = new File(new File(System.getProperty("user.home")),"out.pdf");
attr.add(new Destination(f.toURI()));
printJob.print(document, attr);
的问题是,当我打开out.pdf与任何PDF阅读器,它说格式错误,并用记事本++它只显示HTML。
private PrintService getPrinterByName(String name) {
PrintService[] list = getPrintersList();
if (list.length > 0) {
for (PrintService service : list) {
if (service.getName().contains(name)) {
return service;
}
}
}
return null;
}
private PrintService[] getPrintersList() {
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);
return services;
}
请发布'getPrinterByName()'的源码' – Aerospace