【问题标题】:having trouble in pdf conversion using itext使用 itext 转换 pdf 时遇到问题
【发布时间】:2012-10-16 08:39:49
【问题描述】:

这是我用于生成 PDF 的 Java 类。我正在使用 iText 生成 PDF。

public class pdfgen {
 public void createPdf(String inputFile, String outputFile, boolean isPictureFile)    

 {    
     Rectangle pageSize = new Rectangle(2780, 2525);    
      Document pdfDoc = new Document(pageSize); 
      String pdfFilePath = outputFile;    
      try  {
          FileOutputStream fileOutputStream = new FileOutputStream(pdfFilePath);  
          PdfWriter writer = null;    
          writer = PdfWriter.getInstance(pdfDoc, fileOutputStream);
          writer.open(); 
          pdfDoc.open();
          if (isPictureFile){
              pdfDoc.add(com.itextpdf.text.Image.getInstance(inputFile));
          }
          else{
              URL url=new URL(inputFile);
              URLConnection conn = url.openConnection();
              BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
              String line; while ((line = in.readLine()) !=null ) {
                  System.out.println(line);
              }
              System.out.println(inputFile);
              in.close(); 
              File file = new File(inputFile);
              pdfDoc.add(new  Paragraph(org.apache.commons.io.FileUtils.readFileToString(file)));

          }
          pdfDoc.close();
          writer.close();
      }catch(DocumentException e){
          System.err.println("The error has occured in the document");
      }catch(FileNotFoundException e){
          System.err.println("Your file is not found.");
      }
      catch(Exception e){
          System.err.println("Exception: "+e);
      }
 }

}

这是我在其中调用上述类的 JSP 文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.Vector"  %>
<%@page import="com.dalkin.pdfgen" %>
<% Vector result=(Vector)request.getAttribute("val");%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Output </title>
</head>
<body>

<%Vector names; %>
<%if(arrcol.size()!=0){  %>
            <div style="width:1024px;">
            <table cellpadding="5" cellspacing="5">

            <tr>                

            <td>
            <%for(int q=0;q<result.size();q+=3){ %>
            <div style="background:url(sample.gif) no-repeat; height:320px; width:500px; float:left;">
            <input type="text" size="50" value=<%=result.get(q) %>>
            <input type="text" size="50" value=  <%=result.get((q+1))%>>    
            <input type="text" size="50" value=<%=result.get((q+2))%>>              
            </div>

            <%}} %>                 
            </td>
            </tr>

</table>
</div>

<%pdfgen pf = new pdfgen();  
pf.createPdf("http://localhost:8080/New/FirstServlet","D:\\first.pdf",false);%>  

</body>
</html>

当我运行程序时,我得到FileNotFoundException "http://localhost:8080/New/FirstServlet" Your file is not found. 谁能帮我在哪里做错了?

【问题讨论】:

    标签: java pdf pdf-generation itext


    【解决方案1】:

    你这样调用你的 PDF 创建方法

    pf.createPdf("http://localhost:8080/New/FirstServlet","D:\\first.pdf",false);
    

    在该方法中,您可以像这样使用第一个参数(名为 inputFile):

    File file = new File(inputFile);
    

    “http://localhost:8080/New/FirstServlet”没有文件,所以

    FileUtils.readFileToString(file)
    

    除了你得到的异常,肯定会失败。

    在你做之前的代码

    URL url=new URL(inputFile);
    URLConnection conn = url.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    

    然后使用 line = in.readLine() 遍历行。除了打印这些行之外,您还可以将这些行附加到一些 StringBuilder 并使用内置的 String

    pdfDoc.add(new  Paragraph(...));
    

    【讨论】:

    • 你做 StringBuffer temp = null;字符串线; while ((line = in.readLine()) != null) { temp.append(line); --- 所以你使用一个明确设置为 null 的 StringBuffer。显然你会得到一个 NPE。
    • 哪个相同的错误? FNF 还是 NPE?您当前的代码是什么?您可能需要相应地更新您的原始帖子。如果您想通过其他方式实现目标,请正确描述您想要实现的目标。
    • 所以你的工作流程是 1)有人调用你的 JSP 页面,2)JSP 页面构建了一些表(顺便说一句,你的 arrcol 似乎没有在任何地方定义),3)JSP 页面触发你的createPdf 方法来创建一些 PDF,4) createPdf 连接到本地本地 Web 服务(在同一个 Web 容器中?在不同的 Web 容器中?),由 FirstServlet 表示,5) Web 服务创建一些明文响应,6) createPdf 创建一个来自该文本的 PDF 并将其存储在本地,并且 7) 您的 JSP 页面返回一些 HTML。流程的哪些部分是实际需求,哪些部分是任意实现选择?
    • 这是否意味着 JSP 末尾的 createPdf 调用中的 URL localhost:8080/New/FirstServlet 是 JSP 本身的 URL?
    • 好的,用户访问New.jsp。 New.jsp 发布到 FirstServlet。 FirstServlet 将请求转发到 NewFile1.jsp。我假设它是您最初发布的 JSP。所以 NewFile1.jsp 为“localhost:8080/New/FirstServlet”调用 c​​reatePdf。因此再次访问 FirstServlet,但这次使用的是默认方法 GET。您的 servlet 容器可能不需要这种循环性,并导致一些异常。如果没有, createPdf 会检索一个空答案,因为您只有一个空的 doGet 实现。但这种循环确实不是好的设计。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多