views:

573

answers:

4

I am having with displaying an image file stored in a database as BLOB type.... now i want to call that image and display it in a pdf...i am using jsp and servlet for web client... i just need a central idea or crux of how to go about the problem.

Any help will be highly appreciated

Thank you

Anand

+1  A: 

You have to stream the bytes back to the browser along with a content-type of application/pdf and choose a method of rendering it (inline or attachment).

For example:

byte[] content = getByteArray();

try {
ServletOutputStream outputStream = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-disposition","inline; filename=Example.pdf" );
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
bos.write(content);
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
Otávio Décio
I believe anand wants to wrap some image file (unspecified format...) into the PDF format (unknown reason!), not to output a PDF blob taken from a database. Unless I misunderstood.
PhiLho
yea exactly philho... i want to wrap an image onto a pdf..
anand
A: 

Oracle Technology Net has a good article on this topic.

The article uses .Net, but using Java would be very similar.

jle
+1  A: 

I wonder why you want to do such wrapping, it is just doing things more difficult for the user.

But well, you want to look at the iText library, I think.

PhiLho
+1: iText is the way to go if you want to create PDF files on the fly.
BalusC
A: 

Anand-

The big picture is you need to:

  1. Create a PDF object
  2. Grab the image and drop it in to the PDF
  3. Send the PDF object out to the browser

Ocdecio's answer tells you how to do the last part. Take the iText library from PhiLho's answer to create the PDF and drop the image in to it. Read up on jle's answer to learn some basics about how to deal with BLOBs in the database; that BLOB is where your image is. Take the blob, turn it in to an image object iText can handle, and roll like that.

Good luck!

Rob