Forums Register Login

Problem for writing result set of data into pdf

+Pie Number of slices to send: Send
Hello Friends,

I got a problem i.e i am able to create a pdf file with some text using iText but i want to write a query output(result set) into that pdf file

can any one tell me how to solve this one.

waiting for your replies.

Here is the sample code for it
public class VishalPdf extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

try {

Document document = new Document();


String filePath =this.getServletContext().getRealPath("/");
PdfWriter writer=PdfWriter.getInstance(document,new FileOutputStream(filePath+"/sat.pdf"));
document.open();
document.add(new Paragraph("Sathish Reddy"));//text
document.add(new Paragraph("Swe"));//Text


Vector v=new Vector();
Collection al = new ArrayList();

Connection con = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
con = DriverManager.getConnection ("jdbc racle:thin:@10.10.108.199:1521:TRIPURA", "beneficiary", "beneficiary");

} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
}


PreparedStatement ps2 = con.prepareStatement("select * from Master.MS001_SCHM_MASTER ");
ResultSet rs2=ps2.executeQuery();
//how to add this result set in to pdf
while(rs2.next())
{
//System.out.println("inside selectAllScheme");

v.addElement(rs2.getString(1));
v.addElement(rs2.getString(2));
v.addElement(rs2.getString(3));

//return v;
}


Enumeration e=v.elements();
while(e.hasMoreElements())
{

document.addSubject((String)e.nextElement());
document.addSubject((String)e.nextElement());
document.addSubject((String)e.nextElement());

}

document.close();

ServletOutputStream so = response.getOutputStream();
int read=0;
byte[] bytes=new byte[1024];
System.out.println("sathish");

String tempFilePath = filePath + "/"+"sat.pdf";
String temp_1="sat.pdf";

//String contentType = URLConnection.guessContentTypeFromName("sat.pdf");
response.setContentType("application/pdf");
response.setHeader("pragma", "public");
response.setHeader("Cache-control", "public");
//response.setHeader("Content-Type",contentType);
response.setHeader("Content-Disposition", "attachment; filename=/"+temp_1+"/");
ServletContext ctx=getServletContext();
InputStream is =ctx.getResourceAsStream("/sat.pdf");
OutputStream os =response.getOutputStream();

while((read=is.read(bytes))!=-1)
{
System.out.println("sathish");
so.write(bytes, 0, read);
// os.write(bytes,0,read);
}
//os.flush();
//os.close();
so.flush();
so.close();
//RequestDispatcher rd = request.getRequestDispatcher("Sat");
//rd.forward(request, response);
} catch (SQLException ex) {
Logger.getLogger(VishalPdf.class.getName()).log(Level.SEVERE, null, ex);
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}

finally {
// out.close();

}
}

}
[ July 15, 2008: Message edited by: vishal shiramshetty ]
+Pie Number of slices to send: Send
Hi welcome to Javaranch,
Firstly please UseCodeTags

<blockquote>code:


<pre name="code" class="core">v.addElement(rs2.getString(1));
v.addElement(rs2.getString(2));
v.addElement(rs2.getString(3));

//return v;
}


Enumeration e=v.elements();
while(e.hasMoreElements())
{

document.addSubject((String)e.nextElement());
document.addSubject((String)e.nextElement());
document.addSubject((String)e.nextElement());

}
</pre>


</blockquote>

looks like here you are putting the resultset in a vector and tehn into the PDF document.
So what is the problem.
Are you not getting proper data in the document.

Please take some time and read the ask good questions link below.

Hope this helps .
[ July 15, 2008: Message edited by: Amit Ghorpade ]
+Pie Number of slices to send: Send
thanks for reply

the result set of data is not reflecting in to a PDF file

can you tell me how to write result set of data into PDF
+Pie Number of slices to send: Send
The method you're calling repeatedly is "addSubject", which doesn't seem to make much sense. Did you mean to add the results as paragraphs instead?
I brought this back from the farm where they grow the tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1550 times.
Similar Threads
problem in generating multiple pdf docs on the fly from a servlet
Generating PDF from jsp using iText
Cannot forward after response has been committed? PDF save in Browser?
PDF generation from a servlet
Cannot forward after response has been committed? PDF save in Browser?
Save dialog box in jsf
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:31:05.