Hi,
I am developing a web application using JSP, servlet in ubuntu. I want to convert a JSP page into PDF. I dont want to store the PDF file in my local drive. Just convert into PDF and view Dynamically while i click a Button.

For Example,

In "index.jsp", i hava 2 Text boxes, one is for from date and another one is for to date. I have one submit button "Generate PDF Report". If i click that button, it will redirect to "report.jsp". In that page, I using the follwing code,

<html>         
    <table>
        <tr>
            <td>Roll No</td>
            <td>Name</td>
        </tr>
        <%
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("select * from testtable where fromdate='"+requset.getParameter("fdate")+"' and todate='"+requset.getParameter("tdate")+"'");
        while(rs.next())
        {
        %>
            <tr>
                <td><%=rs.getString("rno")%></td>
                <td><%=rs.getString("name")%></td>
            </tr>
        <%
        }
        %>
    </table>
</html>

Then the output will be displayed in a PDF page like this,

             Roll No                     Name
                1                       XXXX
                2                       YYYY
                3                       ZZZZ

Any one can help Please....?

Recommended Answers

All 5 Replies

hi rajilakshmi,
to generate a pdf report we have jar file called iText.jar,
Itext is a frame work which helps to mainipulate pdf files,
take a look on the following example

try {
            Document document=new Document();
            PdfWriter.getInstance(document,new FileOutputStream("D:\\myfile.pdf"));
            document.open();  
            document.add(new Paragraph("This is my file"));
            document.close();
        } catch (Exception e) {
            System.out.println("error"+e.getMessage());
        }

before that dont forget to include the itext.jar

I already used iText.jar and iTextPDF.jar files. I dont want to store that file in local disk. I just convert the JSP to PDF dynamically. Because i am using this for report generation in my application. I already done this in PHP. But i dont know how to convert in java. I tried the below code

import javax.servlet.*;
import javax.servlet.http.*;

import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;

import java.io.*;
import java.util.*;

public class pdffile extends HttpServlet 
{

    public void init(ServletConfig config) throws ServletException
        {
        super.init(config);
    }

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

    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
        {
        response.setContentType("application/pdf"); // Code 1

        Document document = new Document();
        try
                {
            PdfWriter.getInstance(document, 
            response.getOutputStream()); // Code 2
            document.open();
            // Code 3
            PdfPTable table = new PdfPTable(2);
            table.addCell("1");
            table.addCell("2");
            table.addCell("3");
            table.addCell("4");
            table.addCell("5");
            table.addCell("6");     
            // Code 4
            document.add(table);        
            document.close(); 
        }
                catch(DocumentException e)
                {
                    e.printStackTrace();
                    //out.println("Exception: "+e);
        }
    }

}

The above code it just convert this static page into PDF. I want to convert the dynamic page into PDF format. For example i want to create a table that retrives the data from database and if the user clicks the "Generate Report" button, it will directly redirect to that PDF table page.
Anyone can help please....?

hi, did u manage to find out the solution ? i m facing the same problem.

you could've created your own thread in which you ask for this, especially while showing your code.
but since this is about jsp, you might as well post it in the jsp sub-forum.

Hi,

Is the solution found, I'm facing the same issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.