Question

I Have some values associated with id i want to print those values with respective id's in pdf format, values can be in database or in some variable , can anyone suggest me which way i should go... i have some ideas either i can save those values in database and can make a single xml file from database of all records and then divide each node with different different pdf or directly get the values from database and generate the pdf so please anyone suggest me ideas and ways also if you can refer some links then most welcome please try to help me here.....

 public class PDFGenerator extends HttpServlet
     {



private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException 
        {
    try
    {
        Connection con =  ConnectionManager.getConnection();
         Statement st4= con.createStatement();
        ResultSet rs1 = st4.executeQuery("select count(*) from salary");
        while(rs1.next())
        {
            int p = Integer.parseInt(rs1.getString(1));

        System.out.println("saurabh :" +p);

        for(int q=1;q<=p;q++)
        {

    Document document=new Document(); 

        PdfWriter.getInstance(document,new FileOutputStream("c:/temp/salary.pdf"));
        document.open(); 

         PdfPTable table = new PdfPTable(2);
        table.addCell("eid"); 
        table.addCell("salary");

         Statement st3= con.createStatement();

    ResultSet rs = st3.executeQuery("select * from salary where eid ='"+q+"'");
         while(rs.next())
         {
             table.addCell(rs.getString("eid"));
             table.addCell(rs.getString("salary")); 
        }
         document.add(table);
             document.close();
             }
        }
        }
    catch (Exception e) 
{
    System.out.println(e);
}

}}

please anyone help me here....its printing last row only because its dealing with same pdf and again values are getting over written so please anyone help me how to generate pdf with multiple names so i can store in the same folder... with different name ....

Was it helpful?

Solution 2

its already generating n number of pdf which is specified through results of rows but not getting saved only last one is getting saved because its getting over written so its working because i have to send the mail after getting generated one mail.. so here is solution...

 for(int q=1;q<=p;q++)
    {

   Document document=new Document(); 

    PdfWriter.getInstance(document,new FileOutputStream("c:/temp/salary.pdf"));
    document.open(); 

     PdfPTable table = new PdfPTable(2);
    table.addCell("eid"); 
    table.addCell("salary");

     Statement st3= con.createStatement();

  ResultSet rs = st3.executeQuery("select * from salary where eid ='"+q+"'");
     while(rs.next())
     {
         table.addCell(rs.getString("eid"));
         table.addCell(rs.getString("salary")); 
    }
     document.add(table);
         document.close();
         }
    }

OTHER TIPS

If you want to create PDF's from Java, this is covered pretty comprehensively in the following question

http://www.stackoverflow.com/questions/6118635/

Here's a tutorial for the iText Java PDF library. I've used this guys tutorials for several Java/Eclipse/Android related problems and find him to be thorough and easy to follow:

http://www.vogella.com/tutorials/JavaPDF/article.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top