繁体   English   中英

如何用java itext保存pdf

[英]How to save pdf with java itext

我正在为课程作业编写 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)

这是我写的代码:

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);
        }

我确定代码是正确的,而且文件夹存在,为什么我不能保存我的文件?

这不是 iText 问题。 如果您删除所有 iText 代码并尝试将纯文本写入 FileOutputStream,您将遇到相同的异常。

Perneel 提交的问题“是否有必要从 Web 加载 PDF”是非常相关的。 为什么不在内存中创建 PDF 而不是将其写入文件? 请参阅“iText in Action”中第 9 章的示例以获取灵感: http : //itextpdf.com/book/chapter.php?id=9 (阅读本章也会有所帮助)。

在任何情况下:madth3 是正确的:您假设 (1) \\WebApplication\\pdf\\的路径存在并且 (2) Web 应用程序可以访问此路径。 只需使用路径"."创建一个File对象"." 并使用getAbsolutePath()将该路径写入 servlet 的输出。 我敢打赌它不会给你预期的结果。

有必要从网上加载一个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()));

您获得的上下文路径是 URL 中使用的相对路径,因此它可能没有指向您认为的位置。 我想你应该使用类似getRealPath()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM