Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create thumbnail image for PDF in Java

Tags:

I'm looking for a Java library that will can take a PDF and create a thumbnail image (PNG) from the first page.

I've already looked at JPedal, but its insane licensing fee is completely prohibitive. I am using iText to manipulate PDF files at the moment, but I believe it doesn't do thumbnail generation. I can use something like Ghostscript on the command line, but I'm hoping to keep my project all-Java if possible.

like image 584
Shaggy Frog Avatar asked May 16 '10 18:05

Shaggy Frog


People also ask

How do I show a thumbnail of a PDF file?

To show a thumbnail, write an img element with a data-pdf-thumbnail-file attribute: The pdf file path is a relative or absolute path to a file hosted on your site (no cross domain request). You can add a width or a height in pixels for the generated image. If not, the size of the generated image will be the one of the pdf.

How do I display a thumbnail image in data-pdfjs-SRC?

The data-pdfjs-src attribute specifies the path of the library, which will only be loaded if there's any PDF thumbnail to display in the page. To show a thumbnail, write an img element with a data-pdf-thumbnail-file attribute: The pdf file path is a relative or absolute path to a file hosted on your site (no cross domain request).

How do I add a thumbnail image to an HTML page?

In your html file, include the javascripts: The data-pdfjs-src attribute specifies the path of the library, which will only be loaded if there's any PDF thumbnail to display in the page. To show a thumbnail, write an img element with a data-pdf-thumbnail-file attribute:

How do I get the image of a PDF file?

The image is a view of the first page of the pdf. The script relies on the pdf.js library. See a PDF Thumbnails demo here. First get a local copy of the code, clone it using git: Then get a local copy of pdf.js (>=2.0). Use the prebuilt version that you can find on this page . Download and extract it.


Video Answer


2 Answers

PDF Renderer is a LGPL licensed pure-java library that makes this as simple as (taken from their example page):

File file = new File("test.pdf"); RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdffile = new PDFFile(buf);  // draw the first page to an image PDFPage page = pdffile.getPage(0);  //get the width and height for the doc at the default zoom  Rectangle rect = new Rectangle(0,0,                 (int)page.getBBox().getWidth(),                 (int)page.getBBox().getHeight());  //generate the image Image img = page.getImage(                 rect.width, rect.height, //width & height                 rect, // clip rect                 null, // null for the ImageObserver                 true, // fill background with white                 true  // block until drawing is done                 ); 
like image 115
FRotthowe Avatar answered Sep 30 '22 02:09

FRotthowe


PDF Renderer is fine so long as you only use the subset of PDF files they use. With JPod and JPedal you are paying for an active and developed library not a dead project.

like image 23
mark stephens Avatar answered Sep 30 '22 03:09

mark stephens