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