0

I would like to generate the resume to PDF. Firstly, the resume will be viewed by getting the id of the user. The next step is to generate the resume to PDF.

Below is the codes of servlet and SQLCommands.java. What should I do?

Servlet

package com.dls.csb.process.web.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dls.csb.utility.SQLOperations;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

@WebServlet("/generateresume.html")
public class GenerateResume extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Document dc = new Document();
        try {

            ResultSet rs = SQLOperations.viewResume(id);
            while (rs.next()) {

                   dc.open();
                   dc.add(new Paragraph(rs.getBlob("resume")));

                   dc.close();    


                              }

             }
            catch (DocumentException de) {
                de.printStackTrace();
            }
            catch (SQLException e) {

            e.printStackTrace();
            }


        }

   }

SQLCommands.java

package com.dls.csb.utility;

public interface SQLCommands {

String VIEW_RESUME ="select resume from account where id=?"
}
Kate
  • 15
  • 1
  • 7
  • 1
    You have to write the PDF file into the HttpResponse's output stream ref this http://stackoverflow.com/questions/1442893/implementing-a-simple-file-download-servlet – seenukarthi Jul 31 '14 at 03:43
  • You should use iReport/JasperPeport to generate your template file and call it passing your object rendered from db. – Diego Polido Santana Jul 31 '14 at 03:48
  • I am encountering error on rs.getBlob. It says "the constructor paragraph(blob) is undefined." How do I retrieve file from the database and generate pdf using itext? – Kate Jul 31 '14 at 04:47
  • 1
    What is the content stored in ```resume``` column if its a PDF file you dont need itext you can directly write to HTTP Response. – seenukarthi Jul 31 '14 at 09:25
  • The resume column contains the binary data and filename column is the name of the file which is a varchar data type. Btw, any type of file can be stored in the database. But I am focusing on documents only. – Kate Aug 02 '14 at 14:41

0 Answers0