#image-conversion #pdf #image #poppler #conversion #convert #pdf2img

pdf2image

A simplified port of Python's pdf2image that wraps pdftoppm and pdftocairo to convert PDFs into images

4 releases

new 0.1.3 Feb 9, 2025
0.1.2 May 7, 2024
0.1.1 May 7, 2024
0.1.0 May 7, 2024

#188 in Images

Download history 69/week @ 2024-10-23 80/week @ 2024-10-30 128/week @ 2024-11-06 156/week @ 2024-11-13 157/week @ 2024-11-20 108/week @ 2024-11-27 157/week @ 2024-12-04 133/week @ 2024-12-11 99/week @ 2024-12-18 38/week @ 2024-12-25 86/week @ 2025-01-01 99/week @ 2025-01-08 266/week @ 2025-01-15 316/week @ 2025-01-22 105/week @ 2025-01-29 192/week @ 2025-02-05

890 downloads per month
Used in 3 crates

MIT license

20KB
358 lines

pdf2image

A simplified port of Python's pdf2image that wraps pdftoppmand pdftocairo (part of poppler) to convert PDFs to image::DynamicImages.

Installation

Add to your project: cargo add pdf2image

pdf2image requires poppler to be installed.

Windows

Windows users will have to build or download poppler for Windows. Python's pdf2image maintainer recommends @oschwartz10612 version. You will then have to add the bin/ folder to PATH or use the environment variable PDF2IMAGE_POPPLER_PATH.

macOS

using homebrew:

brew install poppler

Linux

Most distros ship with pdftoppm and pdftocairo. If they are not installed, refer to your package manager to install poppler-utils

Platform-independent (Using conda)

  1. Install poppler: conda install -c conda-forge poppler
  2. Install pdf2image: pip install pdf2image

Quick Start

use pdf2image::{PDF2ImageError, RenderOptionsBuilder, PDF};

fn main() -> Result<(), PDF2ImageError> {
    let pdf = PDF::from_file("examples/pdfs/ropes.pdf").unwrap();
    let pages = pdf.render(
        pdf2image::Pages::Range(1..=8),
        RenderOptionsBuilder::default().pdftocairo(true).build()?,
    )?;

    std::fs::create_dir("examples/out").unwrap();
    for (i, page) in pages.iter().enumerate() {
        page.save_with_format(format!("examples/out/{}.jpg", i + 1), image::ImageFormat::Jpeg)?;
    }

    Ok(())
}

Changelog

  • Reviewed all occurences of .unwrap(), refactored unnecessary ones to return errors and added comments detailing why it is safe to call (Thank you @qarmin!).

License

pdf2image includes code derived from Edouard Belval's pdf2image Python module, which is MIT licensed. Similarly, pdf2image is also licensed under the MIT License.

Dependencies

~17MB
~390K SLoC