Published on September 11, 2024By DeveloperBreeze

How to Convert PDF to Image on Ubuntu

Converting a PDF to an image can be useful for sharing content on platforms that don't support PDF, or for extracting images from a PDF file. This tutorial will guide you through converting PDF files to image formats like PNG or JPEG on Ubuntu.

Requirements

To convert PDFs to images on Ubuntu, we’ll use a tool called pdftoppm, which is part of the poppler-utils package, and ImageMagick to provide more options if needed.

Step 1: Install `poppler-utils`

The pdftoppm tool is available via poppler-utils. To install it, open a terminal and run the following command:


sudo apt update
sudo apt install poppler-utils

Step 2: Convert PDF to Image

Once pdftoppm is installed, you can use it to convert a PDF to an image format like PNG or JPEG.

Syntax


pdftoppm [options] input.pdf output

For example, to convert the first page of a PDF to a PNG image, use the following command:


pdftoppm -png input.pdf output

This will convert all pages of the PDF into PNG images, and the images will be saved with names like output-1.png, output-2.png, etc.

If you want to convert specific pages, you can specify the page range using the -f (from page) and -l (last page) options:


pdftoppm -png -f 1 -l 1 input.pdf output

This command converts only the first page of the PDF into a PNG image.

Step 3: Convert PDF to JPEG

To convert the PDF to JPEG format, simply change the -png flag to -jpeg:


pdftoppm -jpeg input.pdf output

Again, all pages will be converted, and each page will be saved as output-1.jpg, output-2.jpg, etc.

Step 4: Install ImageMagick (Optional)

For more advanced conversion options, you can use ImageMagick, which is a powerful image processing tool.

Install it by running:


sudo apt install imagemagick

Once installed, you can use the convert command to convert PDFs to images:


convert -density 150 input.pdf output.png

Here, -density 150 sets the resolution of the output image, which can improve image quality. You can replace output.png with output.jpg for JPEG conversion.

Step 5: Handling Multi-Page PDFs

If you want to convert only specific pages of a PDF with convert, you can specify the page number:


convert -density 150 input.pdf[0] output.png

This command converts only the first page (0-indexed) of the PDF to an image. You can adjust the page number as needed.

Step 6: Batch Conversion (Optional)

If you have multiple PDF files to convert, you can use a simple loop to convert all PDFs in a directory to images:


for pdf in *.pdf; do
    pdftoppm -png "$pdf" "${pdf%.pdf}"
done

This script converts all PDF files in the current directory to PNG images.

Conclusion

You now know how to convert PDF files to image formats like PNG or JPEG using both pdftoppm and ImageMagick on Ubuntu. These tools provide flexibility, allowing you to convert single or multiple pages and adjust image quality as needed.

Summary of Key Commands:

  • Convert PDF to PNG:

  pdftoppm -png input.pdf output
  • Convert PDF to JPEG:

  pdftoppm -jpeg input.pdf output
  • Using ImageMagick:

  convert -density 150 input.pdf output.png

Comments

Please log in to leave a comment.