目前我正在使用此代碼,但其投擲PrintJobFlavorException
。這是我的代碼幫我解決這個一個:如何在Java中將字符串轉換爲PDF
public class PJUtil {
public static void main(String[] args) throws Exception {
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
Writer output = null;
String text = "printing in pdfPrinting in Java ";
File file = new File("C:\\CMPSup_AL_.PDF");
output = new BufferedWriter(new FileWriter(file));
output.write(text);
output.close();
InputStream is = new BufferedInputStream(new FileInputStream(file));
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
job.print(doc, null);
pjDone.waitForDone();
is.close();
}
}
和異常是
Exception in thread "main" sun.print.PrintJobFlavorException: invalid flavor
at sun.print.Win32PrintJob.print(Win32PrintJob.java:327)
at Collections.PrinterJobUtil.main(PrinterJobUtil.java:89)
是什麼讓你認爲該文本字符串是一個有效的PDF文檔? (提示,不是)。 – jtahlborn
你不能僅僅調用一個PDF文件,它需要是一個真正的PDF格式的文件。不知道這是什麼問題,但它肯定無濟於事。 –