How to use the command 'pdftoppm' (with examples)
- Linux
- December 25, 2023
The ‘pdftoppm’ command is a tool for converting PDF document pages to portable Pixmap (image formats). It is a command-line utility that provides various options to control the conversion process.
Use case 1: Specify the range of pages to convert (N-first page, M-last page)
Code:
pdftoppm -f N -l M path/to/file.pdf image_name_prefix
Motivation: This use case is useful when you want to convert a specific range of pages from a PDF document into images. For example, if you have a 100-page PDF document and you only want to convert pages 10 to 20 into images, you can use this command.
Explanation:
-f N
: Specifies the first page to convert. Replace ‘N’ with the desired page number.-l M
: Specifies the last page to convert. Replace ‘M’ with the desired page number.path/to/file.pdf
: Specifies the path to the input PDF file.image_name_prefix
: Specifies the prefix for naming the output images. The images will be named as “image_name_prefix-01.ppm”, “image_name_prefix-02.ppm”, and so on.
Example output: If you run the following command:
pdftoppm -f 10 -l 20 input.pdf output/image
It will convert pages 10 to 20 of the ‘input.pdf’ file into images named ‘output/image-01.ppm’, ‘output/image-02.ppm’, and so on.
Use case 2: Convert only the first page of a PDF
Code:
pdftoppm -singlefile path/to/file.pdf image_name_prefix
Motivation: This use case is helpful when you only need to convert the first page of a PDF document into an image. It can be useful for generating previews or thumbnails of PDF files.
Explanation:
-singlefile
: Converts only the first page of the PDF file.path/to/file.pdf
: Specifies the path to the input PDF file.image_name_prefix
: Specifies the prefix for naming the output image. The image will be named as “image_name_prefix.ppm”.
Example output: If you run the following command:
pdftoppm -singlefile input.pdf output/image
It will convert the first page of the ‘input.pdf’ file into an image named ‘output/image.ppm’.
Use case 3: Generate a monochrome PBM file (instead of a color PPM file)
Code:
pdftoppm -mono path/to/file.pdf image_name_prefix
Motivation: This use case is beneficial when you want to convert a PDF document into a monochrome image format, such as PBM (Portable Bitmap). Monochrome images use only black and white pixels, which can be useful for certain applications like barcode scanning or OCR (Optical Character Recognition).
Explanation:
-mono
: Generates a monochrome (black and white) image instead of a color image.path/to/file.pdf
: Specifies the path to the input PDF file.image_name_prefix
: Specifies the prefix for naming the output image. The image will be named as “image_name_prefix.pbm”.
Example output: If you run the following command:
pdftoppm -mono input.pdf output/image
It will convert the ‘input.pdf’ file into a monochrome image named ‘output/image.pbm’.
Use case 4: Generate a grayscale PGM file (instead of a color PPM file)
Code:
pdftoppm -gray path/to/file.pdf image_name_prefix
Motivation: This use case is useful when you want to convert a PDF document into a grayscale image format, such as PGM (Portable Graymap). Grayscale images use shades of gray to represent different levels of intensity, which can be helpful for preserving details in images that do not require color.
Explanation:
-gray
: Generates a grayscale image instead of a color image.path/to/file.pdf
: Specifies the path to the input PDF file.image_name_prefix
: Specifies the prefix for naming the output image. The image will be named as “image_name_prefix.pgm”.
Example output: If you run the following command:
pdftoppm -gray input.pdf output/image
It will convert the ‘input.pdf’ file into a grayscale image named ‘output/image.pgm’.
Use case 5: Generate a PNG file instead of a PPM file
Code:
pdftoppm -png path/to/file.pdf image_name_prefix
Motivation: This use case is beneficial when you want to convert a PDF document into a PNG (Portable Network Graphics) image format. PNG is a popular image format that provides lossless compression, making it suitable for web and graphic design applications.
Explanation:
-png
: Generates a PNG image file instead of a PPM image file.path/to/file.pdf
: Specifies the path to the input PDF file.image_name_prefix
: Specifies the prefix for naming the output image. The image will be named as “image_name_prefix.png”.
Example output: If you run the following command:
pdftoppm -png input.pdf output/image
It will convert the ‘input.pdf’ file into a PNG image named ‘output/image.png’.
Conclusion:
The ‘pdftoppm’ command is a powerful tool for converting PDF document pages into image formats. By using various options like specifying page ranges, generating monochrome or grayscale images, and choosing different image formats, you can customize the conversion process according to your needs. This flexibility makes ‘pdftoppm’ a versatile command for working with PDF files.