Want to read the PDF file from the dam folder and convert the pdf data into byte array | Community
Skip to main content
Level 2
March 8, 2024
Solved

Want to read the PDF file from the dam folder and convert the pdf data into byte array

  • March 8, 2024
  • 2 replies
  • 1159 views

Hi ,

I am able to get the PDF file path as /content/dam/folder1/folder2/pdf.pdf, not I want to read the pdf data and convert it into the byteArray. 

I am not able to read the PDF file from DAM asset as we read it simple from local system. kindly revert.

 

 

Thank You

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by EstebanBustamante

Hi, 

You could follow the next steps:

- Get the asset rendition

- From the rendition, you get the InputStream

- Then you convert your InputStream to ByteArray

 

Something like this:

Resource rr = resourceResolver.getResource(pathToYourAsset); Asset asset = rr.adaptTo(Asset.class); Rendition originalRendition = asset.getOriginal(); InputStream is = originalRendition.getStream(); byte[] bytes = IOUtils.toByteArray(is);

 

Please check these links for more details:

https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/dam/api/Asset.html

https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/dam/api/Rendition.html#getStream--

https://stackoverflow.com/questions/1264709/convert-inputstream-to-byte-array-in-java

https://www.baeldung.com/convert-input-stream-to-array-of-bytes 

 

Hope this helps

2 replies

EstebanBustamante
Adobe Champion and Community Advisor
EstebanBustamanteAdobe Champion and Community AdvisorAccepted solution
Adobe Champion and Community Advisor
March 8, 2024

Hi, 

You could follow the next steps:

- Get the asset rendition

- From the rendition, you get the InputStream

- Then you convert your InputStream to ByteArray

 

Something like this:

Resource rr = resourceResolver.getResource(pathToYourAsset); Asset asset = rr.adaptTo(Asset.class); Rendition originalRendition = asset.getOriginal(); InputStream is = originalRendition.getStream(); byte[] bytes = IOUtils.toByteArray(is);

 

Please check these links for more details:

https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/dam/api/Asset.html

https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/dam/api/Rendition.html#getStream--

https://stackoverflow.com/questions/1264709/convert-inputstream-to-byte-array-in-java

https://www.baeldung.com/convert-input-stream-to-array-of-bytes 

 

Hope this helps

Esteban Bustamante
Dear_ATAuthor
Level 2
March 22, 2024

Thank You so much , it's working.