2012-10-26 8 views
2

이것은 PDF 생성을위한 Java 클래스입니다. PDF 생성을 위해 iText를 사용하고 있습니다. 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 파일이있는 나는 내가 누군가가 나를 도울 수 FileNotFoundException "http://localhost:8080/New/FirstServlet" Your file is not found. 무엇입니까 프로그램을 실행하면

<%@ 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> 

어디에 내가 잘못하고있는 중이 야 내 위의 클래스를 호출하고 있습니까?

답변

2

당신은 전화를 같은 당신이 (입력 _ 이름) 첫 번째 매개 변수를 사용하는 방법 내부이

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

처럼 PDF 생성 방법 :

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

을하고 다음 줄을 사용하여 라인을 반복하기 전에 = in.readLine(). 줄을 인쇄하는 대신 줄을 추가하여 StringBuilder에 추가 할 수도 있습니다.

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

StringBuffer temp = null; 문자열 줄; while ((line = in.readLine())! = null) {temp.append (line); } --- 그래서 당신은 명시 적으로 null로 설정된 StringBuffer를 사용한다. 분명히 당신은 NPE를 얻을 것입니다. – mkl

+0

같은 오류가 있습니까? FNF 또는 NPE? 현재 코드가 무엇입니까? 그에 따라 원래 게시물을 업데이트 할 수 있습니다. 목표를 달성하기위한 대안적인 방법을 원할 경우, 달성하고자하는 바를 적절하게 기술하십시오. – mkl

+0

그래서 당신의 작업 흐름은 1) 누군가가 당신의 JSP 페이지를 호출하고, 2) JSP 페이지가 어떤 테이블 (BTW, 당신의 arrcol이 어디에도 정의되어 있지 않은 것처럼 보인다)을 빌드하고, 3) JSP 페이지가 createPdf 메소드를 트리거하여 PDF, 4) createPdf는 FirstServlet이 나타내는 로컬 웹 서비스 (동일한 웹 컨테이너에 있습니까? 다른 웹 서비스에 있습니까?)에 연결합니다. 5) 웹 서비스는 일부 일반 텍스트 응답을 만듭니다. 6) createPdf는 해당 텍스트에서 PDF를 만들고 저장합니다. 지역적으로, 7) JSP 페이지가 HTML을 반환합니다. 흐름의 어떤 부분이 실제 요구 사항이며 임의의 구현 선택 사항은 무엇입니까? – mkl

관련 문제