Reputation: 902
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:
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?
Upvotes: 1
Views: 12105
Reputation: 77606
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.
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).
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.
Upvotes: 2
Reputation: 7344
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()
Upvotes: 0
Reputation: 3367
Is it necessary that there is a pdf loaded from the web? Can't you just generete a pdf file like this:
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()));
Upvotes: 0