Expand my Community achievements bar.

SOLVED

PDFbox- not able to write images in pdf

Avatar

Level 5

Hi,

I am using PDFbox for generating pdf by my webpage, I have images in my webpage and I am trying to download the all images as a pdf file(single), but I am not able to write the images in pdf , I am getting corrupted pdf file.

I am storing all the image path in json and getting path from json one by one

Please let me know how can i convert images in pdf file.

 

Regards,

1 Accepted Solution

Avatar

Correct answer by
Level 5

thanks Kautuk,

yes this is PDFbox issue not AEM , pdfbox not supporting all the features , I have tried with itextpdf and its working fine.

 

 

Regards

View solution in original post

9 Replies

Avatar

Level 10

I recommend trying to get PDFBOX code to work outside of AEM. This stackoverflow thread will help you: 

http://stackoverflow.com/questions/8521290/cant-add-an-image-to-a-pdf-using-pdfbox

Avatar

Administrator

Hi

Adding to what scott has mentioned,

Please refer to the article :- http://stackoverflow.com/questions/22358478/java-create-pdf-pages-from-images-using-pdfbox-library

// Create PDF pages from images using PDFBox library

PDDocument document = new PDDocument();InputStream in = new FileInputStream(someImage);BufferedImage bimg = ImageIO.read(in);float width = bimg.getWidth();float height = bimg.getHeight();PDPage page = new PDPage(new PDRectangle(width, height));document.addPage(page);PDXObjectImage img = new PDJpeg(document, new FileInputStream(someImage));PDPageContentStream contentStream = new PDPageContentStream(document, page);contentStream.drawImage(img, 0, 0);contentStream.close();in.close();document.save("test.pdf");document.close();

 

Link:- http://stackoverflow.com/questions/8521290/cant-add-an-image-to-a-pdf-using-pdfbox

// Add an image to a pdf using PDFBox

I hope this will help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Level 5

thanks for response,but I am getting number of images in response in ByteArrayInputStreamand trying to get the images from ByteArrayInputStream in PDF but format of the image not proper.Please check the attached code.

Avatar

Level 10

Does your PDFBOX code work outside of AEM? If you are having problems wiht PDFBox code - i would ask that community:

https://pdfbox.apache.org/support.html

THis sounds like a PDFBox issue - not an AEM issue. 

Avatar

Administrator

Hi

As mentioned by Scott, please if code is working fine outside the AEM environment.

Please have a look at these couple of articles:-

Link:- https://stackoverflow.com/questions/35751150/print-byte-to-pdf-using-pdfbox

//

 final byte[] image = ... // your codeByteArrayInputStream bais = new ByteArrayInputStream(image);BufferedImage bim = ImageIO.read(bais);PDImageXObject pdImage = LosslessFactory.createFromImage(doc, bim);

 

Read more from the article.

I hope this will help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Level 5

Hi Scott,

code is not working outside AEM also, this pdfbox issue only.

 

 

Regards,

Avatar

Level 5

Hi Kautuk ,

I have tried the code, its getting incorrect image format .

 

Regards,

Avatar

Administrator

varuns46785756 wrote...

Hi Kautuk ,

I have tried the code, its getting incorrect image format .

 

Regards,

 


Hi Varun

I would like you to please check this with PDFBox's Support team. As it is related to PDFBox not that to AEM.

They would be in better position to help you.

Link:- https://pdfbox.apache.org/mailinglists.html

Link:- https://pdfbox.apache.org/support.html

I hope this would help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Correct answer by
Level 5

thanks Kautuk,

yes this is PDFbox issue not AEM , pdfbox not supporting all the features , I have tried with itextpdf and its working fine.

 

 

Regards