Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display first page of PDF as Image

I am creating web application where I am displaying images/ pdf in thumbnail format. Onclicking respective image/ pdf it get open in new window.

For PDF, I have (this is code of the new window)

<iframe src="images/testes.pdf" width="800" height="200" />

Using this I can see all PDF in web browser. However for thumbnail purpose, I want to display only first page of PDF as an Image.

I tried

 <h:graphicImage value="images/testes.pdf" width="800" height="200" />

however it is not working. Any idea how to get this done?

Update 1

I am providing path of pdf file for example purpose. However I have images in Database. In actual I have code as below.

<iframe src="#{PersonalInformationDataBean.myAttachmentString}" width="800" height="200" />

Update 2

For sake of thumbnail, what I am using is

 <h:graphicImage height=200 width=200 value="...."> 

however I need to achieve same for PDF also.

Hope I am clear what I am expecting...

like image 779
Fahim Parkar Avatar asked Aug 06 '12 12:08

Fahim Parkar


People also ask

Can you display a PDF as an image?

Acrobat's online tool lets you quickly convert a PDF to a PNG, TIFF or JPG image using any web browser, such as Google Chrome. Just choose your preferred file format. The Acrobat conversion process happens in seconds, with image quality you can trust.

How do I copy the first page of a PDF?

Copy and paste PDF pagesIn a PDF file, click the 'Preview' icon in the upper left corner of the window to enter the PDF page preview mode. Select(long press/select icon) the page(s) you want to copy. Tap the 'Copy' icon at the top-right corner of the screen.


1 Answers

I'm not sure if all browsers display your embedded PDF (done via <h:graphicImage value="some.pdf" ... /> ) equally well.

Extracting 1st Page as PDF

If you insist on using PDF, I'd recommend one of these 2 commandline tools to extract the first page of any PDF:

  1. pdftk
  2. Ghostscript

Both are available for Linux, Mac OS X and Windows.

pdftk command

pdftk input.pdf cat 1 output page-1-of-input.pdf

Ghostscript command

gs -o page-1-of-input.pdf -sDEVICE=pdfwrite -dPDFLastPage=1 input.pdf

(On Windows use gswin32c.exe or gswin64c.exe instead of gs.)

pdftk is slightly faster than Ghostscript when it comes to page extraction, but for a single page that difference is probably neglectable. As of the most recent released version, v9.05, the previous sentence is no longer true. I found that Ghostscript (including all startup overhead) requires ~1 second to extract the 1st page from the 756 page PDF specification, while PDFTK needed ~11 seconds.

Converting 1st Page to JPEG

If you want to be sure that even older browsers can display your 1st page well, then convert it to JPEG. Ghostscript is your friend here (ImageMagick cannot do it by itself, it needs the help of Ghostscript anyway):

gs -o page-1-of-input-PDF.jpeg -sDEVICE=jpeg -dLastPage=1 input.pdf

Should you need page 33, you can do it like this:

gs -o page-33-of-input-PDF.jpeg -sDEVICE=jpeg -dFirstPage=33 -dLastPage33 input.pdf

If you need a range of PDFs, like pages 17-23, try this:

gs -o page-16+%03d-of-input-PDF.jpeg -sDEVICE=jpeg -dFirstPage=17 -dLastPage23 input.pdf

Note, that the %03d notation increments with each page processed, starting with 1. So your first JPEG's name would be page-16+001-of-input-PDF.jpeg.

Maybe PNG is better?

Be aware that JPEG isn't a format suited well for images containing high black+white contrast and sharp edges like text pages. PNG is much better for this.

To create a PNG from the 1st PDF pages with Ghostscript is easy:

gs -o page-1-of-input-PDF.png -sDEVICE=pngalpha -dLastPage=1 input.pdf

The analog options as with JPEGs are true when it comes to extract ranges of pages.

like image 179
Kurt Pfeifle Avatar answered Sep 20 '22 01:09

Kurt Pfeifle