1

I have the following situation into a Java application.

From the database a retrieve this Blob object that is the representation of a PDF file on the DB:

Blob blobPdf = cedolinoPdf.getAllegatoBlob();

Now I have to convert it into a PDF file. How can I do this task?

Tnx

3
  • 1
    What do you mean? It already is a PDF file, or? So what do you want to do? Display it? Save it? Commented Jan 29, 2015 at 17:04
  • @ControlAltDel I need to display it into a JSP page Commented Jan 30, 2015 at 8:46
  • @AndreaNobili - if you need to display it in a web page, create a servlet (or spring MVC controller or whatever) that gets the Blob and pipes it's input stream to the servlet's output stream. Then in your page, you can create a frame (or a PDF viewer object) that points to your servlet
    – BretC
    Commented Jan 30, 2015 at 10:45

4 Answers 4

6

If the Blob is the binary representation of a PDF, all you need to do is get the bytes. If you wanted to write the PDF to a file, you could do...

Blob blobPdf = ...;
File outputFile = new File("/tmp/blah/whatever.pdf");
FileOutputStream fout = new FileOutputStream(outputFile);
IOUtils.copy(blobPdf.getBinaryStream(), fout);

This should write your PDF to a file called "/tmp/blah/whatever.pdf"

2
1

You can use the getBinaryStream() from the ResultSet, then you can create a file from InputStream returned by the getBinaryStream().

Your code may look something like this

Connection connection = null;
Statement statement = null;
ResultSet rs = null;
  
try {
         
        Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
        connection = DriverManager.getConnection(connectionURL, "sa", "sa");
        statement = connection.createStatement();
        rs = statement.executeQuery("SELECT fileName,blobFile FROM tblBlobFiles");
     
     
        if (rs.next()) {
  
String filename = rs.getString(1);
           Blob blob = rs.getBlob(2);
          InputStream is = blob.getBinaryStream();
              FileOutputStream fos = new FileOutputStream("C:\\DownloadedFiles"+ "\\" + filename);
 
int b = 0;
while ((b = is.read()) != -1)
{
    fos.write(b); 
}
        }
    } catch (IOException e) 
    {
    e.getMessage (); e.printStackTrace(); 
System.out.println(e); 
    } 
    catch (SQLException e) 
    {
    e.getMessage (); e.printStackTrace(); 
System.out.println(e); 
    }

If you need all the blobs, do a iteration on the resultset

0

you can use Java NIO

InputStream fileBolb = rs.getBinaryStream("columnName");
ReadableByteChannel rbc = Channels.newChannel(fileBolb );
FileOutputStream fos = new FileOutputStream(filePath + fname);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
2
  • What is Long.MAX_VALUE in this example? Is there no way to get the size of the ReadableByteChannel?
    – jabe
    Commented Jan 29, 2016 at 18:33
  • Log.MAX_VALUE is a constant holding the maximum value Commented Mar 9, 2016 at 13:42
-1

Converting Blob into PDF is not that much straight forward task. I had used itext.5.5.9.jar to achieve same, here is my code:

public static void blobToPDF(String[] args) throws IOException {

    Blob blobPdf = ....;

    com.itextpdf.kernel.pdf.PdfReader newreader = null;

    try (InputStream targetStream = blobPdf.getBinaryStream()) {

        newreader = new com.itextpdf.kernel.pdf.PdfReader(targetStream);

        System.out.println("Reading PDF using com.itextpdf.kernel.pdf.PdfReader...");
        System.out.println("PDF read success with kernel package ");
        System.out.println("PDF length = " + newreader.getFileLength());

    } catch (InvalidPdfException | FileNotFoundException | SQLException ipe) {
        ipe.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {

        if (newreader != null)
            newreader.close();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.