nr.iras.sk
nr.iras.sk

Reputation: 8498

"java.io.IOException: This may not be a PDF File"

I want to read a pdf file from a url and convert it into a thumbnail image. I am using the following code. I didnt included the converting portion here.The problem is on the line "pdffile = new PDFFile(buf);" I get an exception "java.io.IOException: This may not be a PDF File". But I can see the pdf on the browser. What is wrong with me? Please help me.

    byte[] byteArray = null;
    InputStream is = null;
    String streamTo = null;
    BufferedImage bmg = null;
    PDFFile pdffile;
    ByteBuffer buf;
    int pageNumber = 1;

    try {
       is = fetchImageFromServer(url); //Pdf Url path
       if (!pageNumber.isEmpty()) {
         streamTo = is.toString(); 
         byteArray = streamTo.getBytes();
         buf = ByteBuffer.wrap(byteArray);
         pdffile = new PDFFile(buf);
       }
    } catch (IOExceptio e) {
    }

Upvotes: 1

Views: 2450

Answers (3)

vz0
vz0

Reputation: 32923

The is.toString() call won't read all the bytes correctly. There is an utility function at Apache Commons IO that will help you, IOUtils.toByteArray(). Try this:

is = fetchImageFromServer(url); //Pdf Url path
if (!pageNumber.isEmpty()) {
    byteArray = IOUtils.toByteArray(is);
    buf = ByteBuffer.wrap(byteArray);
    pdffile = new PDFFile(buf);
}

Upvotes: 2

mark stephens
mark stephens

Reputation: 3184

PDf is a binary object. If you convert it to a string, it will change byte values and break the file.

Upvotes: 0

Maurice Perry
Maurice Perry

Reputation: 32831

You must read the content of the stream. toString will not do that.

Upvotes: 3

Related Questions