Skip to main content

A simple python wrapper for PDFium

Project description

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)

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypdfium-0.0.14.tar.gz (7.8 MB view details)

Uploaded Source

File details

Details for the file pypdfium-0.0.14.tar.gz.

File metadata

  • Download URL: pypdfium-0.0.14.tar.gz
  • Upload date:
  • Size: 7.8 MB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pypdfium-0.0.14.tar.gz
Algorithm Hash digest
SHA256 d749afbece5e0d18bb201247107346a5030040c4966a42943cde5d245fca517d
MD5 e3dbc8255f5161a23cb878dc68dd26da
BLAKE2b-256 1e311557e2b8a13cd6cf8d1348d52c03412c780f50a08876b7bec586a561bad2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page