coobird / thumbnailator

Thumbnailator - a thumbnail generation library for Java
MIT License
5.08k stars 780 forks source link

Does thumbnailator support thumbnail generation for pdf files? #181

Closed bhupendersinghh closed 1 year ago

bhupendersinghh commented 2 years ago

I want to generate images for pdf files containing multiple pages. Is there a way to leverage thumbnailator for that?

coobird commented 2 years ago

Short answer is no.

Long answer is, if you're able to make a image from a PDF, then yes, you can use Thumbnailator to make a thumbnail of that image. I found an answer in StackOverflow saying that PDFBox can create images, and it sure seems like PDFRenderer can create BufferedImages that Thumbnailator can use as sources for making thumbnails.

cluth17 commented 2 years ago

I was able to accomplish this with the following code:

public static Boolean processImage(Path input, Path output, Integer width, Integer height) {  

     PDDocument pdfDoc = PDDocument.load(new File(input.toString()));  
     PDFRenderer pdfRender = new PDFRenderer(pdfDoc);  
     BufferedImage pdfImage = pdfRender.renderImageWithDPI(0, 72);  
     Thumbnails.of(pdfImage).size(width, height).toFile(output.toString());  
     return true;  

}