java 如何用java itext保存pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13363774/
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 save pdf with java itext
提问by DamianFox
I'm doing a java web application for a coursework. In this application there are two type of users: seller and buyer. The buyer can buy a set of stuff and when he does it, I must create a receipt into pdf format; but when I try to buy something tomcat give me this error:
我正在为课程作业编写 Java Web 应用程序。在这个应用程序中有两种类型的用户:卖家和买家。买家可以购买一套东西,当他购买时,我必须创建一个pdf格式的收据;但是当我尝试购买一些 tomcat 时,给我这个错误:
HTTP Status 500 - \WebApplication\pdf\test.pdf (Impossibile trovare il percorso specificato)
type Exception report
message \WebApplication\pdf\test.pdf (Impossibile trovare il percorso specificato)
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.io.FileNotFoundException: \WebApplication\pdf\test.pdf (Impossibile trovare il percorso specificato)
java.io.FileOutputStream.open(Native Method)
java.io.FileOutputStream.<init>(FileOutputStream.java:212)
java.io.FileOutputStream.<init>(FileOutputStream.java:104)
viewer.PdfCreator.createPdf(PdfCreator.java:30)
servlet.BuyerConfirmationPage.doGet(BuyerConfirmationPage.java:115)
servlet.BuyerConfirmationPage.doPost(BuyerConfirmationPage.java:61)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
And here is the code I have written:
这是我写的代码:
try {
Document document = new Document(PageSize.A4,50,50,50,50);
PdfWriter.getInstance(document,new FileOutputStream(request.getContextPath() + "/pdf/test.pdf"));
document.open();
PdfPTable table = new PdfPTable(5);
PdfPCell seller_cell = new PdfPCell(new Paragraph("Seller"));
PdfPCell name_cell = new PdfPCell(new Paragraph("Name"));
PdfPCell price_cell = new PdfPCell(new Paragraph("Price"));
PdfPCell UM_cell = new PdfPCell(new Paragraph("UM"));
PdfPCell quantity_cell = new PdfPCell(new Paragraph("Quantity"));
table.addCell(seller_cell);
table.addCell(name_cell);
table.addCell(price_cell);
table.addCell(UM_cell);
table.addCell(quantity_cell);
PdfPCell seller_cell_value = new PdfPCell(new Paragraph(seller));
PdfPCell name_cell_value = new PdfPCell(new Paragraph(name));
PdfPCell price_cell_value = new PdfPCell(new Paragraph(total_price));
PdfPCell UM_cell_value = new PdfPCell(new Paragraph(UM));
PdfPCell quantity_cell_value = new PdfPCell(new Paragraph(quantity));
table.addCell(seller_cell_value);
table.addCell(name_cell_value);
table.addCell(price_cell_value);
table.addCell(UM_cell_value);
table.addCell(quantity_cell_value);
document.add(table);
document.close();
} catch (DocumentException ex) {
Logger.getLogger(PdfCreator.class.getName()).log(Level.SEVERE, null, ex);
}
I'm sure that the code is right and, also, that the folder exists, why can't I save my file?
我确定代码是正确的,而且文件夹存在,为什么我不能保存我的文件?
回答by Bruno Lowagie
This is not an iText question. You will have the same exception if you remove all the iText code and try writing plain text to the FileOutputStream.
这不是 iText 问题。如果您删除所有 iText 代码并尝试将纯文本写入 FileOutputStream,您将遇到相同的异常。
The question submitted by Perneel 'is it necessary that there is a PDF loaded from the Web' is very relevant. Why don't you create the PDF in memory instead of writing it to a file? See the example of chapter 9 in "iText in Action" for inspiration: http://itextpdf.com/book/chapter.php?id=9(reading the chapter will also help).
Perneel 提交的问题“是否有必要从 Web 加载 PDF”是非常相关的。为什么不在内存中创建 PDF 而不是将其写入文件?请参阅“iText in Action”中第 9 章的示例以获取灵感:http: //itextpdf.com/book/chapter.php?id=9(阅读本章也会有所帮助)。
In any case: madth3 is right: you are assuming (1) that the path to \WebApplication\pdf\
exists and (2) that the web application has access to this path. Just create a File
object using the path "."
and use getAbsolutePath()
to write that path to the output of your servlet. I bet it won't give you the result you expected.
在任何情况下:madth3 是正确的:您假设 (1) 路径\WebApplication\pdf\
存在并且 (2) Web 应用程序可以访问此路径。只要创建一个File
使用路径对象"."
和使用getAbsolutePath()
编写路径到Servlet的输出。我敢打赌它不会给你预期的结果。
回答by Perneel
Is it necessary that there is a pdf loaded from the web? Can't you just generete a pdf file like this:
有必要从网上加载一个pdf吗?你不能像这样生成一个 pdf 文件:
Document document = new Document(PageSize.A4,50,50,50,50);
File file = File.createTempFile("dossier_" + selectedDossier.getDossierID() + "_", ".pdf");
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file.getPath()));
回答by madth3
The context path you are getting it's the relative path used in the URL's therefore it's probably not pointing to the place you think. I guess you should be using something like getRealPath()
您获得的上下文路径是 URL 中使用的相对路径,因此它可能没有指向您认为的位置。我想你应该使用类似的东西getRealPath()