Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Junilu Lacar
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Himai Minh
  • Carey Brown
  • Frits Walraven

PDF generation from a servlet

Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I am trying to generate PDF from servelet.I tried with JSP but could not able to succedd.Now i am trying with servlet its not giving any exceptions but pdf report is not getting disdplayed and my browser window is getting hanged.
Can some body help me pleae..
Here is my code
import java.util.Date;
import javax.servlet.http.*;
import javax.servlet.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.html.HtmlWriter;
public class Chap0105 extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// we retrieve the presentationtype
String presentationtype = "pdf";
// step 1
System.out.println("---inside servelit----");
Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
try {
// step 2: we set the ContentType and create an instance of the corresponding Writer
if ("pdf".equals(presentationtype)) {
PdfWriter.getInstance(document, response.getOutputStream());
else if ("html".equals(presentationtype)) {
HtmlWriter.getInstance(document, response.getOutputStream());
else {
// step 3;
// step 4
document.add(new Paragraph(new Date().toString()));
catch(DocumentException de) {
System.err.println("document: " + de.getMessage());
// step 5: we close the document (the outputstream is also closed internally)
Author and all-around good cowpoke
Posts: 13078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
It is my understanding that some viewers only work right if you can set the content length. To do this you have to write the PDF to an internal ByteArrayOutputStream first to get the total length, set the content length and then write the resulting byte[] to the output stream.
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I noticed you have asked the same question in the JSP forum. Anyone who wants to help should go to this thread.
Please don't post the same question in multiple forums. It creates duplicate conversations and wastes the time of the people who are trying to help you.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic