PyPDFium
A simple python wrapper for PDFium.
Installation
pip install pypdfium
Quick start
import sys
from PIL import Image
import ctypes
import pypdfium as PDFIUM
PDFIUM.FPDF_InitLibraryWithConfig(PDFIUM.FPDF_LIBRARY_CONFIG(2, None, None, 0))
if __name__ == "__main__":
if len(sys.argv) < 2:
print("USAGE: demo.py somefile.pdf")
exit(0)
fname = sys.argv[1]
doc = PDFIUM.FPDF_LoadDocument(fname, None) # load document
page_count = PDFIUM.FPDF_GetPageCount(doc) # get page counts
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
buffer = PDFIUM.FPDFBitmap_GetBuffer(bitmap)
buffer_ = ctypes.cast(buffer, 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)
Notes
-
Working on 64bit Windows, Mac OS X and Linux.
-
The
pypdfium.pyfile is generated by ctypesgen with command:ctypesgen -lpdfium -L somewhere/pdfium-linux/lib somewhere/pdfium-linux/include/*.h -o pypdfium.py -
PDFium binaries are from https://github.com/bblanchon/pdfium-binaries
-
API documentation: https://developers.foxitsoftware.com/resources/pdf-sdk/c_api_reference_pdfium/
-
A PDF manager based on pypdfium: https://github.com/YinlinHu/kuafu
Metadata
Release files for pypdfium 0.0.15
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pypdfium-0.0.15.tar.gz | 7.8 MB | Without provenance | Details for pypdfium-0.0.15.tar.gz |
Release files / pypdfium-0.0.15.tar.gz
| Download URL | pypdfium-0.0.15.tar.gz |
|---|---|
| Size | 7.8 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d890581c98417fad829a0312ab78d1e9ac6f0ff0722df3e472c75cbc8bd0eb8f
|
|
BLAKE2b-256 checksum How to use checksums |
295d17a388df5bd2e71b7a45bd5e64e3094d17afc86f461bca107109e3f031d1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.4rc1
|
Security for pypdfium 0.0.15
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.