thumbnailator icon indicating copy to clipboard operation
thumbnailator copied to clipboard

Does thumbnailator support thumbnail generation for pdf files?

Open bhupendersinghh opened this issue 3 years ago • 2 comments

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

bhupendersinghh avatar Oct 24 '21 18:10 bhupendersinghh

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.

coobird avatar Oct 25 '21 16:10 coobird

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;  

}

cluth17 avatar Nov 16 '21 20:11 cluth17