1

I am trying to create a pdf file from database and I am stuck at the image part, since Apache pdf box only accepts physical files and the images in my database are in blob format.

PDXObjectImage image = new PDJpeg(doc,rs.getBlob("image"));

Can anyone help me?

Gorpik
  • 10,940
  • 4
  • 36
  • 56
  • I would recommend storing the images on disk and instead store metadata about the images in the database (i.e. location/name etc) unless you really have to store them directly. See this thread, really puts light on the discussion https://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay?lq=1 – Tjernquist1 Dec 30 '15 at 11:25

1 Answers1

1

Convert your blob into an InputStream and pass this to PDJpeg:

Blob imageBlob = rs.getBlob("image");
try (InputStream imageInputStream = imageBlob.getBinaryStream()) {
    PDXObjectImage image = new PDJpeg(doc, imageInputStream);
}
Benedikt Köppel
  • 4,853
  • 4
  • 32
  • 42
  • 1
    I am probably nit picking, but I would close that imageInputStream with a try with resources :) – rjdkolb Dec 30 '15 at 11:32