目前我正在使用这段代码,但它会抛出 PrintJobFlavorException
。这是我的代码,可以帮助我解决这个问题:
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)是
Exception in thread "main" sun.print.PrintJobFlavorException: invalid flavor
at sun.print.Win32PrintJob.print(Win32PrintJob.java:327)
at Collections.PrinterJobUtil.main(PrinterJobUtil.java:89)
最佳答案
您的打印机可能不支持基于文本的表示。看看这篇文章java printing ,特别是第 5 页。
关于java - 如何在 Java 中将字符串转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7941247/