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.
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.
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).
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:
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.
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 );
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With