java - 如何使用java itext保存pdf

标签 java web-applications pdf-generation itext

我正在为类(class)作业编写一个 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 提交的问题“是否有必要从网络加载 PDF”非常相关。为什么不在内存中创建 PDF,而不是将其写入文件?请参阅“iText in Action”中第 9 章的示例以获得灵感:http://itextpdf.com/book/chapter.php?id=9 (阅读本章也会有所帮助)。

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

关于java - 如何使用java itext保存pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13363774/

相关文章:

php - WebView - 如何使用从网站上的应用程序收集的数据

javascript - 错误 mPDF 需要 mb_string 函数

pdf - 那里有质量好的 djvu 到 pdf 转换器吗?

perl - 如何使用 Perl 的 PDF::API2 使复选框在 PDF 中呈现

java - 尝试查找属性文件中缺少 key 的 apt java 代码逻辑

java - 什么是汤姆猫? Web 容器还是 EE 容器?

Java,将字节数组转换为HashMap或JSON字符串

java - 如何使用 HAProxy 检查 Solr 是否健康

html - list 更新时无法获取 iOS webapp 文件。 401未经授权

security - "execute app as me"部署设置安全吗?