現在、このコードを使用していますが、そのコードはPrintJobFlavorException
です。これは私のコードはこの1つの固定私を助けている:文字列を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形式のファイルである必要があります。それが問題なのかどうかは分かりませんが、確かに助けにならないでしょう。 –