23

From a DB2 table I've got blob which I'm converting to a byte array so I can work with it. I need to take the byte array and create a PDF out of it.

This is what I have:

static void byteArrayToFile(byte[] bArray) {  
    try {  
        // Create file  
        FileWriter fstream = new FileWriter("out.pdf");  
        BufferedWriter out = new BufferedWriter(fstream);  
        for (Byte b: bArray) {  
            out.write(b);  
        }  
        out.close();  
    } catch (Exception e) {  
        System.err.println("Error: " + e.getMessage());  
    }  
}

But the PDF it creates is not right, it has a bunch of black lines running from top to bottom on it.

I was actually able to create the correct PDF by writing a web application using essentially the same process. The primary difference between the web application and the code about was this line:

response.setContentType("application/pdf");

So I know the byte array is a PDF and it can be done, but my code in byteArrayToFile won't create a clean PDF.

Any ideas on how I can make it work?

0

4 Answers 4

63

Sending your output through a FileWriter is corrupting it because the data is bytes, and FileWriters are for writing characters. All you need is:

OutputStream out = new FileOutputStream("out.pdf");
out.write(bArray);
out.close();
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Jason can you please share me the code or share me some link am also facing the issue. Thanks
@Anand bArray is the byte array. If u have got the array, then the code provided with the answer is the usable code to convert a byte array to PDF file.
Hi Jason, I m using same and getting byte array from web service. But even unable to open pdf it is showing "unable to open this file either not a supported file type or file has been damaged". Please help me on this.
Hi Jason, i have the similar question, unable to open the pdf ... please help stackoverflow.com/questions/77823549/…
7

One can utilize the autoclosable interface that was introduced in java 7.

try (OutputStream out = new FileOutputStream("out.pdf")) {
   out.write(bArray);
}

1 Comment

Doesn't really matters here, but try-with-recourses for autoclosable was introduced in java 7. docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html
1

Read from file or string to bytearray.

byte[] filedata = null;
String content = new String(bytearray);
content = content.replace("\r", "").replace("\uf8ff", "").replace("'", "").replace("\"", "").replace("`", "");
String[] arrOfStr = content.split("\n");
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
try (PDPageContentStream cs = new PDPageContentStream(document, page)) {
    // setting font family and font size
    cs.beginText(); 
    cs.setFont(PDType1Font.HELVETICA, 14);
    cs.setNonStrokingColor(Color.BLACK);
    cs.newLineAtOffset(20, 750);
    for (String str: arrOfStr) {
        cs.newLineAtOffset(0, -15);
        cs.showText(str);
    }
    cs.newLine();
    cs.endText();
}
document.save(znaFile);
document.close();

2 Comments

The answer would benefit from better formatting and an explanation.
Formatting is not allowing over this site, and formatting eclipse could do by itself I guess so. Anyway this code is tested and running for changing the bytearray to pdf file. One more suggestion, if file needs to be shared over the network, better to use bytearray and use FileUtils library.
0
 public static String getPDF() throws IOException {

        File file = new File("give complete path of file which must be read");
        FileInputStream stream = new FileInputStream(file);
        byte[] buffer = new byte[8192];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int bytesRead;enter code here
        while ((bytesRead = stream.read(buffer)) != -1) {
            baos.write(buffer, 0, bytesRead);
        }
        System.out.println("it came back"+baos);
        byte[] buffer1= baos.toByteArray();
        String fileName = "give your filename with location";

        //stream.close();
        
        
         FileOutputStream outputStream =
                    new FileOutputStream(fileName);
         
         outputStream.write(buffer1);
        
         return fileName;
        
    }

1 Comment

hello i have the similar issue stackoverflow.com/questions/77823549/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.