user1776106
user1776106

Reputation: 21

having trouble in pdf conversion using itext

This is my Java class for generating PDF. I am using iText for PDF generation.

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

}

This is my JSP file in which I am calling my the above class

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

When I run the program I am getting FileNotFoundException "http://localhost:8080/New/FirstServlet" Your file is not found. Can anyone help me where I am doing wrong?

Upvotes: 2

Views: 1091

Answers (1)

mkl
mkl

Reputation: 95888

You call your PDF creation method like this

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

Inside that method you use the first parameter (named inputFile) like this:

File file = new File(inputFile);

"http://localhost:8080/New/FirstServlet" is no file, so

FileUtils.readFileToString(file)

is bound to fail with the exception you get.

In the code before you do

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

and then iterate over the lines using line = in.readLine(). Instead of just printing the lines, you could additionally append the lines to some StringBuilder and use the String built in

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

Upvotes: 2

Related Questions