java 如何在Java中将字符串转换为PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7941247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Convert from String into PDF in Java
提问by vikram
Currently I am using this code but its throwing PrintJobFlavorException
. This is my code help me out fixing this one:
目前我正在使用此代码,但它会抛出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();
}
}
and exception is
例外是
Exception in thread "main" sun.print.PrintJobFlavorException: invalid flavor
at sun.print.Win32PrintJob.print(Win32PrintJob.java:327)
at Collections.PrinterJobUtil.main(PrinterJobUtil.java:89)
回答by Shahriar
your printer may not support text based representation. Have a look at this article java printing, specially page 5.
您的打印机可能不支持基于文本的表示。看看这篇文章java打印,特别是第5页。
回答by madth3
回答by jkysam
Just to give you another option for creating PDF files. Try using Apache's PDFBoxand take a look at the cookbook. The HelloWorldexample shows you how to create a simple PDF document like the one you were trying to create in your sample code.
只是为您提供另一种创建 PDF 文件的选项。尝试使用Apache 的 PDFBox并查看说明书。的HelloWorld的例子展示了如何创建像你试图在你的示例代码来创建一个简单的PDF文档。
回答by Thakhani Tharage
Also take a look on Jasper Reports http://community.jaspersoft.com/project/jasperreports-library
还可以查看 Jasper Reports http://community.jaspersoft.com/project/jasperreports-library
回答by E Pavan Varma
Change DocFlavor flavor = DocFlavor.INPUT_STREAM.PDFto *DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE*.
将DocFlavor 风味 = DocFlavor.INPUT_STREAM.PDF更改为 *DocFlavor 风味 = DocFlavor.INPUT_STREAM.AUTOSENSE*。
E Pavan Varma
帕万·瓦玛