目前我正在使用此代码,但它正在抛出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 in thread "main" sun.print.PrintJobFlavorException: invalid flavor
at sun.print.Win32PrintJob.print(Win32PrintJob.java:327)
at Collections.PrinterJobUtil.main(PrinterJobUtil.java:89)
答案 0 :(得分:2)
您的打印机可能不支持基于文本的表示。请查看本文java printing,特别是第5页。
答案 1 :(得分:1)
正如其他人所指出的那样,你不能只创建一个名为PDF的文件并将其打印出来。如果您需要生成PDF,那么您可以查看itext。
答案 2 :(得分:0)
只是为您提供另一种创建PDF文件的选项。尝试使用Apache's PDFBox并查看食谱。 HelloWorld示例向您展示了如何创建一个简单的PDF文档,就像您尝试在示例代码中创建的那样。
答案 3 :(得分:0)
另请查看Jasper Reports http://community.jaspersoft.com/project/jasperreports-library
答案 4 :(得分:0)
将 DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF 更改为* DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE *。
E Pavan Varma