Search code examples
javapdfautomationpdfboxbytebuffer

How to convert ByteBuffer to pdf


In my application a pdf report only opens in print preview which allows user to directly print the pdf document. Now i want to automate this to verify the pdf content.

I have got the pdf content through an api which is in base64 [did split to get only data], i tried converting to byte array after decoding but it only prints junk characters.[byte array to string] Now i have converted this data into ByteBuffer and want this to write in pdf.

ByteBuffer decodedBytes = new BASE64Decoder().decodeBufferToByteBuffer(
    new String(base64split2[1].substring(0, base64split2[1].length() - 1))
);

Can someone tell me how do i convert this decodedBytes of ByteBuffer to pdf.

Thanks


Solution

  •   byte[] decodedBytes = new BASE64Decoder().decodeBuffer(str);
            InputStream targetStream = new ByteArrayInputStream(decodedBytes);
            PDDocument document = PDDocument.load(targetStream);
            document.save("C:/test.pdf");
            FileUtils.writeByteArrayToFile(new File("C:/test.pdf"), decodedBytes);
    

    Using above code to convert to pdf.