Convert PDF file to images

Convert PDF file to images

Posted: 9 years ago in  Unix | Miscellaneous |


Sometime, we need display PDF file on website instead of a direct link because not all users has Adobe Reader installed and integrated in web browser to read it directly.


Sometime, we need display PDF file on website instead of a direct link because not all users has Adobe Reader installed and integrated in web browser to read it directly.

To make it attractive, there are javascript library built for it such as "turn4j". However, we need convert PDF file to images then let the library generate a reader with page-turn animation

In the scope of this article, I would like to share the way to convert PDF files using command-line in Ubuntu: poppler-utils

First of all, install the package:

sudo apt-get install poppler-utils

Then use utility pdftoppm to convert:

pdftoppm -png /path/to/pdf/file.pdf prefix

-png is image format you need

prefix is used in extracted images file. e.g.: prefix_0001.png

The default output resolution is 150dpi. We can specify to get higher resolution images:

pdftoppm -rx 300 -ry 300 -png file.pdf prefix

Here are other options you may use:

-o print only odd pages
-e print only even pages
-r <fp> resolution, in DPI (default is 150)
-rx <fp> X resolution, in DPI (default is 150)
-ry <fp> Y resolution, in DPI (default is 150)
-png generate a PNG file
-jpeg generate a JPEG file
-tiff generate a TIFF file
-tiffcompression <string> set TIFF compression: none, packbits, jpeg, lzw, deflate
-freetype <string> enable FreeType font rasterizer: yes, no
-aa <string> enable font anti-aliasing: yes, no
-opw <string> owner password (for encrypted files)
-upw <string> user password (for encrypted files)