thumbnailator
thumbnailator copied to clipboard
Does thumbnailator support thumbnail generation for pdf files?
I want to generate images for pdf files containing multiple pages. Is there a way to leverage thumbnailator for that?
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 BufferedImage
s that Thumbnailator can use as sources for making thumbnails.
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;
}