Hi Friend,
We have used itext-api to generate pdf report. Try the following code:
1)pdf.jsp:
<html>
<body>
<form method="get" action="../CreatePDFReport">
<table>
<tr><td>Name</td></th><td><input type="text" name="name"></td></tr>
<tr><td>Address</td></th><td><input type="text" name="address"></td></tr>
<tr><td>Contact No</td></th><td><input type="text" name="contact"></td></tr>
<tr><td>Email</td></th><td><input type="text" name="email"></td></tr>
<tr><td></td><td><input type="submit" value="Create Report"></td></tr>
</table>
</form>
</body>
</html>
2)CreatePDFReport.java:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class CreatePDFReport extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
OutputStream file = new FileOutputStream(new File("
C://text.pdf";));
Document document = new Document();
PdfWriter.getInstance(document, file);
document.open();
String name=request.getParameter("name");
String address=request.getParameter("address");
String contact=request.getParameter("contact");
String email=request.getParameter("email");
document.add(new Paragraph("Hello,"));
document.add(new Paragraph("It is "+new Date().toString()));
document.add(new Paragraph(" "));
document.add(new Paragraph("Name is : "+name));
document.add(new Paragraph("Address : "+address));
document.add(new Paragraph("Contact No : "+contact));
document.add(new Paragraph("Email : "+email));
document.close();
file.close();
out.println("Report is generated successfully.");
} catch (Exception e) {
e.printStackTrace();
} finally {
out.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
Hope that it will be helpful for you.
Thanks