关于itext pdf创建

时间:2014-10-22 12:30:22

标签: java java-ee itext

我刚开始在Java中使用itext pdf。

我在J2EE中使用itext创建了pdf。它使用Apache 5.5服务器在Eclipse中正常运行,但在上传到实时服务器后,pdf将不会生成。我无法弄清问题是什么。

我在我的网络应用程序中使用了itext-5.5.3 jar。我的课程如下:

public class create_pdf {

    public String pdfcreate() throws FileNotFoundException {
        try{
            OutputStream file = new FileOutputStream(new File("C://Windows//temp//hello.pdf"));
            Document document = new Document();  
            Font font = new Font(Font.FontFamily.COURIER, 8, 0, BaseColor.DARK_GRAY);

            PdfWriter writer =PdfWriter.getInstance(document, file);

            document.open();  
            document.add(new Paragraph("E-Ticket",fontheader));
            document.add(new Paragraph("E-Ticket Generated On - "+new Date().toString(),font));
            document.add(new Paragraph("hello",font));
            document.close();
        }
        catch(Exception ex){
            ex.printStackTrace();
        } 
        return null;
    }   

}

1 个答案:

答案 0 :(得分:0)

尝试更改路径。检查它是否是您服务器中有价值的路径,

OutputStream file = new FileOutputStream(new File("C://Windows//temp//hello.pdf"))

可能尝试使用服务器的安装文件夹路径或可以访问的路径。像/ directory_name

这样的东西

如果您尝试提供下载,请参阅How to offer download of local PDF file in Java?