Overview of pdf2image
masterpdftoppm and pdftocairo utilities. To use this library, you must have these underlying utilities installed on your system.repository·master·Indexed 24 days ago
https://github.com/belval/pdf2imageA Python 3.7+ module that acts as a wrapper around the pdftoppm and pdftocairo command-line tools to convert PDF documents into PIL Image objects or image files. It provides functions like convert_from_path and convert_from_bytes, and requires the poppler library to be installed on the system.
pdftoppm and pdftocairo utilities. To use this library, you must have these underlying utilities installed on your system.When processing large PDFs, converting them directly to memory can exhaust system resources. To prevent this, provide an output_folder argument. This instructs pdf2image to save the converted images to disk. The resulting objects are still compatible with Pillow and will load the image data on demand.
import tempfile
from pdf2image import convert_from_path
with tempfile.TemporaryDirectory() as path:
images_from_path = convert_from_path("/home/user/example.pdf", output_folder=path)For a platform-independent installation using Conda, follow these steps:
conda install -c conda-forge popplerpip install pdf2imageconda install -c conda-forge poppler
pip install pdf2imageTo achieve better performance when converting PDFs:
fmt='jpeg' can lead to significant gains. Note that PNG is slower due to compression.pdftocairo: Set use_pdftocairo=True to force the use of pdftocairo, which may improve performance.paths_only=True: If you are dealing with very large PDFs and want to avoid memory exhaustion, set paths_only=True to receive file paths instead of PIL Image objects.Use apt-get to install the poppler-utils package.
sudo apt-get install poppler-utilsWindows users must install poppler. It is recommended to use the @oschwartz10612 version.
After downloading, you have two options to make it available to pdf2image:
bin/ folder of the poppler installation to your system PATH.poppler_path argument.Example:
images = convert_from_path(r'C:\path\to\poppler-xx\bin', '/home/belval/example.pdf')Since Poppler is not available via standard Windows package managers, follow these steps:
bin/ directory from the extracted folder to your system's PATH environment variable.cmd and running pdftoppm -h.Use convert_from_bytes to convert PDF data directly from a byte stream. This is useful when the PDF content is already in memory or being read from a stream.
from pdf2image import convert_from_bytes
with open("/home/user/example.pdf","rb") as pdf:
images = convert_from_bytes(pdf.read())Use pacman to install the poppler package.
sudo pacman -S popplerUse Homebrew to install the poppler package.
brew install popplerInstall the pdf2image Python module using pip:
pip install pdf2imageNote: This module requires poppler to be installed on your system to function.
If you need to modify the tool (e.g., to add a new language), you can install it from the official repository source.
git clone https://github.com/Belval/pdf2image
python3 setup.py install