talira

pypdfium2

Python bindings to PDFium, reasonably cross-platform

open sourceApache-2.0 OR BSD-3-Clauseself-hostablefree

pypdfium2 is a Python binding to PDFium, a liberally licensed C library for PDF rendering, inspection, manipulation and creation, with pre-built wheels that bundle PDFium for Windows, macOS, Linux (glibc and musl) and Android. Its helper classes render pages to bitmaps (with PIL and NumPy adapters), extract and search text, read the table of contents, extract images and attachments, rearrange and merge pages and create PDFs, while the raw PDFium C API stays available through ctypes; a pypdfium2 command line covers rendering, text and image extraction and more. It's licensed Apache-2.0 or BSD-3-Clause, so unlike PyMuPDF (AGPL) it renders PDFs in Python without strong copyleft; PDF.js and the Rust pdfium-render fill the same role in other languages.

Problems it solves

  • render PDF pages to PNG in Python without pulling in an AGPL library
  • take screenshots of every page of a generated PDF so a visual regression test can diff them
  • turn PDF pages into images I can feed to a vision model or OCR
  • rasterize PDFs on a server with Chrome's PDF engine instead of poppler
  • extract the text of a PDF together with the region it sits in
  • show how a filled PDF form looks with its field values drawn on the page
  • convert PDF pages to NumPy arrays for an image processing pipeline
  • merge pages from several PDFs and save the result from Python
  • pull the embedded images and file attachments out of a PDF
  • a pip-installable PDF renderer with wheels for Linux ARM, musl and Windows, no system packages needed
  • turn a folder of JPEG scans into one PDF from the command line

Features

  • render pages at any scale or rotation, with optional form fields, to PIL images or NumPy arrays
  • text extraction by region or character range, and text search
  • table of contents, page labels and page boxes
  • page objects: iterate, insert and remove; image extraction
  • embedded file attachments: list, extract, add and delete
  • create PDFs, add pages, import pages from other documents and save
  • flatten forms and annotations
  • raw PDFium API through ctypes (pypdfium2.raw)
  • CLI subcommands: render, extract-text, extract-images, arrange, tile, toc, attachments, imgtopdf, pdfinfo, fonts
  • pre-built wheels for Windows, macOS, Linux glibc and musl, and Android; experimental Pyodide
View 2 more
  • optional V8 (JavaScript) and XFA-enabled PDFium builds through a source install (PDFIUM_PLATFORM=auto-v8)
  • can bind against a system or self-built PDFium

Limitations

  • Isn't thread-safe: PDFium can't be called from several threads at once, even for different documents, so render in parallel with processes12
  • Doesn't give access to raw PDF objects (dictionaries, streams, name trees): PDFium's public API offers only higher-level functions1

Capabilities

Has a command line toolplan not known

Alternatives

The same job

These do what pypdfium2 does.

  1. PyMuPDF A high performance Python library for data extraction, analysis, conversion & manipulation of PDF (and other) documents.
  2. PDF.js PDF Reader in JavaScript
  3. pdfium-render A high-level idiomatic Rust wrapper around Pdfium, the C++ PDF library used by the Google Chromium project.
  4. pdfplumber Plumb a PDF for detailed information about each char, rectangle, line, et cetera — and easily extract text and tables.

Compare pypdfium2 with the top 3