Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Level 2

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

 

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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/As...

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

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

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

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/As...

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

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