2012-10-26 41 views
2

這是用於生成PDF的Java類。我正在使用iText來生成PDF。在使用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. 誰能幫我的節目,我做錯了什麼?

回答

2

你打電話給你的PDF創建方法,這樣

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

裏面你使用的第一個參數(名爲INPUTFILE),其方法是這樣的:

File file = new File(inputFile); 

的「http://本地主機:8080 /新/ FirstServlet「是沒有文件,所以

FileUtils.readFileToString(file) 

肯定會失敗,你會得到例外。

在代碼你

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

,然後遍歷使用行線之前= in.readLine()。而不是僅僅打印線,你可以附加一個行一些StringBuilder的,並使用內置的

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

你做的StringBuffer TEMP =空字符串;字符串行; while((line = in.readLine())!= null){temp.append(line); } ---所以你使用一個明確設置爲null的StringBuffer。顯然你會得到一個NPE。 – mkl

+0

同樣的錯誤? FNF還是NPE?您目前的代碼是什麼?您可能需要相應地更新原始文章。如果您想要其他方法來實現您的目標,請正確描述您想要實現的目標。 – mkl

+0

所以你的工作流程是:1)有人調用你的JSP頁面; 2)JSP頁面建立一些表格(順便說一句,你的arrcol似乎沒有在任何地方定義過); 3)JSP頁面觸發你的createPdf方法來創建一些PDF, 4)createPdf連接到由FirstServlet表示的本地本地Web服務(在同一個Web容器中?在另一個Web容器中),5)該Web服務創建一些明確的文本響應,6)createPdf從該文本創建PDF並存儲它本地和7)您的JSP頁面返回一些HTML。流程的哪些部分是實際需求,哪些部分是任意實施選擇? – mkl