This discussion is archived
8 Replies Latest reply: May 22, 2008 9:24 PM by 807591 RSS

How to convert data in to PDF file using java?

807591 Newbie
Currently Being Moderated
Hi All,

How can we convert data that we have retrived from html in PDF file..Help me plz...
  • 1. Re: How to convert data in to PDF file using java?
    807591 Newbie
    Currently Being Moderated
    Take a look at iText or PDFBox.
  • 2. Re: How to convert data in to PDF file using java?
    807591 Newbie
    Currently Being Moderated
    yes i saw a program in ur forum only.But its showing errors where i was compiling it,because

    import com.lowagie.text.Document;
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.html.HtmlWriter;
    import com.lowagie.text.pdf.PdfWriter;
    import com.lowagie.text.rtf.RtfWriter2;

    these packages are not available.

    the following program:

    import java.io.IOException;
    import java.util.Date;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.lowagie.text.Document;
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.html.HtmlWriter;
    import com.lowagie.text.pdf.PdfWriter;
    import com.lowagie.text.rtf.RtfWriter2;

    /**
    * Hello World example as a Servlet.
    *
    * @author blowagie
    */
    public class HelloWorldServlet extends HttpServlet {

    private static final long serialVersionUID = -6033026500372479591L;

    /**
    * Returns a PDF, RTF or HTML document.
    *
    * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
    */
    public void doGet (HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {

    // we retrieve the presentationtype
    String presentationtype = request.getParameter("presentationtype");

    // step 1
    Document document = new Document();
    try {
    // step 2: we set the ContentType and create an instance of the corresponding Writer
    if ("pdf".equals(presentationtype)) {
    response.setContentType("application/pdf");
    PdfWriter.getInstance(document, response.getOutputStream());
    }
    else if ("html".equals(presentationtype)) {
    response.setContentType("text/html");
    HtmlWriter.getInstance(document, response.getOutputStream());
    }
    else if ("rtf".equals(presentationtype)) {
    response.setContentType("text/rtf");
    RtfWriter2.getInstance(document, response.getOutputStream());
    }
    else {
    response.sendRedirect("http://itextdocs.lowagie.com/tutorial/general/webapp/index.html#HelloWorld");
    }

    // step 3
    document.open();

    // step 4
    document.add(new Paragraph("Hello World"));
    document.add(new Paragraph(new Date().toString()));
    }
    catch(DocumentException de) {
    de.printStackTrace();
    System.err.println("document: " + de.getMessage());
    }

    // step 5: we close the document (the outputstream is also closed internally)
    document.close();
    }
    }



    error is Pakages doesnt exist...plz help me..
  • 3. Re: How to convert data in to PDF file using java?
    807591 Newbie
    Currently Being Moderated
    You have to download the iText library from http://www.lowagie.com/iText/ then include it in your classpath and everything will be fine.
  • 4. Re: How to convert data in to PDF file using java?
    807591 Newbie
    Currently Being Moderated
    Is it not possible with out using it.
  • 5. Re: How to convert data in to PDF file using java?
    807591 Newbie
    Currently Being Moderated
    Nope, because your programm needs classes used in iText and how do you expect it to have access to those classes if they are not available in your computer?
  • 6. Re: How to convert data in to PDF file using java?
    807591 Newbie
    Currently Being Moderated
    I mean cant we use different coding other than this program and Itext?
  • 7. Re: How to convert data in to PDF file using java?
    807591 Newbie
    Currently Being Moderated
    Yes abenstex .As u said I opened that link to download ,but i found three things to down load source code compiled code and documentation..Among this which one i have to download..?
  • 8. Re: How to convert data in to PDF file using java?
    807591 Newbie
    Currently Being Moderated
    take a look at this

    http://schmidt.devlib.org/java/libraries-pdf.html