Python bindings to PDFium
Project description
PyPDFium2
PyPDFium2 is a Python 3 binding to PDFium, the liberal-licensed PDF rendering library authored by Foxit and maintained by Google.
Install/Update
Install from PyPI
python3 -m pip install -U pypdfium2
Manual installation
The following steps require git
, ctypesgen
and gcc
to be installed and
available in PATH
.
Package locally
This will download a pre-built binary for PDFium, generate the bindings and build a wheel.
python3 update.py
python3 setup_${platform_name}.py bdist_wheel
python3 -m pip install -U dist/pypdfium2-${version}-py3-none-${platform_tag}.whl
Source build
If you are using an architecture where no pre-compiled package is available, it is possible to build PDFium from source. However, this is a complex process that can vary depending on the host system, and it may take a long time.
python3 setup_source.py bdist_wheel
pip3 install dist/pypdfium2-${version}-py3-none-${platform_tag}.whl
Examples
Using the command-line interface
pypdfium2 -i your_file.pdf -o your_output_dir/ --scale 1 --rotation 0 --optimise-mode none
If you want to render multiple files at once, a bash for
-loop may be suitable:
for file in ./*.pdf; do echo "$file" && pypdfium2 -i "$file" -o your_output_dir/ --scale 2; done
To obtain a list of possible command-line parameters, run
pypdfium2 --help
CLI documentation: https://pypdfium2.readthedocs.io/en/latest/cli.html
Using the support model
import pypdfium2 as pdfium
with pdfium.PdfContext(filename) as pdf:
pil_image = pdfium.render_page(
pdf,
page_index = 0,
scale = 1,
rotation = 0,
background_colour = 0xFFFFFFFF,
render_annotations = True,
optimise_mode = pdfium.OptimiseMode.none,
)
pil_image.save("out.png")
Support model documentation: https://pypdfium2.readthedocs.io/en/latest/api.html
Using the PDFium API
import ctypes
from PIL import Image
import pypdfium2 as pdfium
doc = pdfium.FPDF_LoadDocument(filename, None) # load document (filename, password string)
page_count = pdfium.FPDF_GetPageCount(doc) # get page count
assert page_count >= 1
page = pdfium.FPDF_LoadPage(doc, 0) # load the first page
width = int(pdfium.FPDF_GetPageWidthF(page) + 0.5) # get page width
height = int(pdfium.FPDF_GetPageHeightF(page) + 0.5) # get page height
# render to bitmap
bitmap = pdfium.FPDFBitmap_Create(width, height, 0)
pdfium.FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF)
pdfium.FPDF_RenderPageBitmap(
bitmap, page, 0, 0, width, height, 0,
pdfium.FPDF_LCD_TEXT | pdfium.FPDF_ANNOT
)
# retrieve data from bitmap
cbuffer = pdfium.FPDFBitmap_GetBuffer(bitmap)
buffer = ctypes.cast(cbuffer, ctypes.POINTER(ctypes.c_ubyte * (width * height * 4)))
img = Image.frombuffer("RGBA", (width, height), buffer.contents, "raw", "BGRA", 0, 1)
img.save("out.png")
if bitmap is not None:
pdfium.FPDFBitmap_Destroy(bitmap)
pdfium.FPDF_ClosePage(page)
pdfium.FPDF_CloseDocument(doc)
Documentation for the PDFium API is available. PyPDFium2 transparently maps all PDFium classes, enums and functions to Python. However, there can sometimes be minor differences between Foxit and open-source PDFium. In case of doubts, take a look at the inline source code documentation of PDFium.
Licensing
PyPDFium2 source code itself is Apache-2.0 licensed. The auto-generated bindings file contains BSD-3-Clause code.
Documentation and examples are CC-BY-4.0.
PDFium is available by the terms and conditions of either Apache 2.0 or BSD-3-Clause, at your choice.
Various other BSD- and MIT-style licenses apply to the dependencies of PDFium.
License texts for PDFium and its dependencies are included in the file
LICENSE-PDFium.txt
, which is also shipped with binary re-distributions.
History
PyPDFium2 is the successor of pypdfium and pypdfium-reboot.
The initial pypdfium was packaged manually and did not get regular updates. There were no platform-specific wheels, but only a single wheel that contained binaries for 64-bit Linux, Windows and macOS.
pypdfium-reboot then added a script to automate binary deployment and bindings generation to simplify regular updates. However, it was still not platform specific.
PyPDFium2 is a full rewrite of pypdfium-reboot to build platform-specific wheels. It also adds a basic support model and a command-line interface on top of the PDFium C API to simplify common use cases. Moreover, PyPDFium2 includes facilities to build PDFium from source, to extend platform compatibility.
Development
PDFium builds are retrieved from bblanchon/pdfium-binaries. Python bindings are auto-generated with ctypesgen
Currently supported architectures:
- macOS x86_64 *
- macOS arm64 *
- Linux x86_64
- Linux aarch64 (64-bit ARM) *
- Linux armv7l (32-bit ARM hard-float, e. g. Raspberry Pi 2)
- Windows 64bit
- Windows 32bit *
*
Not tested yet
If you have access to a theoretically supported but untested system, please report success or failure on the issues panel.
(In case bblanchon/pdfium-binaries
would add support for more architectures, PyPDFium2
could be adapted easily.)
For wheel naming conventions, please see Python Packaging: Platform compatibility tags and the various referenced PEPs.
PyPDFium2 contains scripts to automate the release process:
- To build wheels for all platforms, run
./release.sh
. This will download binaries and header files, write finished Python wheels todist/
, and runcheck-wheel-contents
. - To clean up after a release, run
./clean.sh
. This will remove downloaded files and build artifacts.
Publishing the wheels
- You may want to upload to TestPyPI first to ensure
everything works as expected:
twine upload --verbose --repository-url https://test.pypi.org/legacy/ dist/*
- If all went well, upload to the real PyPI:
twine upload dist/*
Issues
Since PyPDFium2 is built using upstream binaries and an automatic bindings creator, issues that are not related to packaging most likely need to be addressed upstream. However, the PyPDFium2 issues panel is always a good place to start if you have any problems, questions or suggestions.
If the cause of an issue could be determined to be in PDFium, the problem needs to be reported at the PDFium bug tracker.
Issues related to build configuration should be discussed at pdfium-binaries, though.
If your issue is caused by the bindings generator, refer to the ctypesgen bug tracker.
Known limitations
Non-ascii file paths on Windows
On Windows, PDFium currently is not able to open documents with file names containing multi-byte, non-ascii characters. This bug is reported since March 2017. However, the PDFium development team so far has not given it much attention. The cause of the issue is known and the structure for a fix was proposed, but it has not been applied yet.
The following approaches have been considered to work around this limitation in PyPDFium2:
-
Using
FPDF_LoadMemDocument()
rather thanFPDF_LoadDocument()
is not possible due to issues with concurrent access to the same file. Moreover, it would be less efficient as the whole document has to be loaded into memory. This makes it impractical for large files. -
FPDF_LoadCustomDocument()
is not a solution, since mapping the complex file reading callback to Python is hardly feasible. Furthermore, there would likely be the same problem with concurrent access. -
Creating a tempfile with a compatible name would be possible, but cannot be done in
PdfContext
itself: For faster rendering, you usually set up a multiprocessing pool or a concurrent future. This means each process has to initialise its ownPdfContext
. If an automatic tempfile workaround were implemented inPdfContext
, this would mean that each process creates its own temporary copy of the file, which would be highly inefficient. The tempfile should be created only once for all pages, not for each page separately. The workaround could be done somewhat like this:import sys if sys.platform.startswith('win32') and not filename.isascii(): # create a temporary copy and remap the file name # (str.isascii() requires at least Python 3.7) ...
This concept is currently used for the
render_pdf()
support model of PyPDFium2 (see_helpers.py
).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for pypdfium2-0.2.0-py3-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f97a8c3389f8b64b62bb7bfaaec445b166a65d69e52dcbcb7b7aaab6c96d9c97 |
|
MD5 | 1d37b7d2f695b019f60b7e60b11accaf |
|
BLAKE2b-256 | 8feabc09d8812004588b2cc0977b41def9cabb06fc13c8d7efd58e3440be0c52 |
Hashes for pypdfium2-0.2.0-py3-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ebaaf99ce04ee3f42f9496d433b06cacac880b82cda04e81aa148930e752277c |
|
MD5 | 92edd7f322e2c1fe7107fe684b2bbafe |
|
BLAKE2b-256 | 873b0d3d3b4f1f6e7920ccfb62b2c0315ef71628a1dc7938d22c4f45d60e53bc |
Hashes for pypdfium2-0.2.0-py3-none-manylinux_2_17_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1fcfd29019a06296141c34f5beeada4faf20afbaf1594fe316dd0e3b55732fd9 |
|
MD5 | 7241339403931d73db4d71ab3322bd8e |
|
BLAKE2b-256 | ea30a9bb2724e0270a84cb13c217ebb78a8d8cb3162c58031a1cf85dd24049ab |
Hashes for pypdfium2-0.2.0-py3-none-manylinux_2_17_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b6713599af1f8cee6f2e7f0306a8997819cbad7e17fe45494b7e1e294aa237e |
|
MD5 | aa644f2137c943eb5f7f41519bf746cf |
|
BLAKE2b-256 | 465beec8171c6f33e0f870ea52e498796ed034a1e041de02f49392cba8c891ef |
Hashes for pypdfium2-0.2.0-py3-none-manylinux_2_17_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84101f76f8868a15a41dc5b5dd745284838d8ed679dc6ba2a2b0e94325ff6bff |
|
MD5 | ec3795799bf6be6ac192de535c00651e |
|
BLAKE2b-256 | 31afa91212bb140de8eb10f264651c029a855d30da1272d33a9c0796a9416959 |
Hashes for pypdfium2-0.2.0-py3-none-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60537d2bdff8f45a4a98acb9bcfdf68f5935513145edbae9b6da741120e79c5c |
|
MD5 | 524c464e49290eb95da1940b65cf15c9 |
|
BLAKE2b-256 | 52f1477f4761e303886a238ec889d46a27f41f9d7df43b1ebdcfe6d708ece47b |
Hashes for pypdfium2-0.2.0-py3-none-macosx_10_11_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bbf837906b4fd1d0f3df763e0cc2d135c4eaf6c7fe85b6a6af2d2cc19b8d31d |
|
MD5 | 0895b36a2c5e0dfc3c2b09ae31ba1946 |
|
BLAKE2b-256 | 96a3438cad1b6b9c8dd96ec730601a645d0d3084217df8894fa5be6f823bf983 |