Skip to main content

pdfdol

Data Object Layer for PDF data

To install: pip install pdfdol

Documentation

Examples

Pdf "Stores"

Get a dict-like object to list and read the pdfs of a folder, as text:

>>> from pdfdol import PdfFilesReader
>>> from pdfdol.tests import get_test_pdf_folder
>>> folder_path = get_test_pdf_folder()
>>> pdfs = PdfFilesReader(folder_path)
>>> sorted(pdfs)
['sample_pdf_1', 'sample_pdf_2']
>>> assert pdfs['sample_pdf_2'] == [
...     'Page 1\nThis is a sample text for testing Python PDF tools.'
... ]

See that the values of a PdfFilesReader are lists of pages. If you need strings (i.e. all the pages together) you can add a decoder like so:

from dol import add_decoder
page_separator = '---------------------'
pdfs = add_decoder(pdfs, decoder=page_separator.join)

If you need this at the level of the class, just do this:

from dol import add_decoder
page_separator = '---------------------'
FilesReader = add_decoder(PdfFilesReader, decoder=page_separator.join)
# and then
pdfs = FilesReader(folder_path)
# ...

If you need to concatinate a bunch of pdfs together, you can do so in many ways. Here's one:

from dol import Files
from pdfdol import concat_pdfs

s = Files('~/Downloads/cosmograph_documentation_pdfs/')
concat_pdfs(s, key_order=sorted)

Converting ebooks and documents to PDF (optional Calibre integration)

pdfdol natively converts images, HTML, and Markdown to PDF. For additional formats -- EPUB, MOBI, DOCX, ODT, DJVU, RTF, and many more -- install Calibre, which provides the ebook-convert command-line tool.

pdfdol does not depend on Calibre; it auto-detects the tool at runtime and uses it only for formats that have no built-in converter.

Installing ebook-convert:

Platform Command
macOS (Homebrew) brew install --cask calibre
Debian / Ubuntu sudo apt install calibre
Fedora / RHEL sudo dnf install calibre
Linux servers (headless) sudo -v && wget -nv -O- https://download.calibre-ebook.com/linux-installer.sh | sudo sh /dev/stdin

The Linux CLI installer is self-contained (no GUI/X11 needed) and recommended for servers. See https://calibre-ebook.com/download for all options.

from pdfdol import ebook_convert_to_pdf, find_ebook_convert

# Check whether Calibre is available
if find_ebook_convert():
    pdf_bytes = ebook_convert_to_pdf("book.epub")

You can also go through the usual get_pdf entry point -- it will automatically route to ebook-convert when it recognises the file extension:

from pdfdol import get_pdf
pdf_bytes = get_pdf("book.epub")                     # returns PDF bytes
get_pdf("book.epub", egress="book.pdf")              # saves to file

What cannot be converted

DRM-protected ebooks (Amazon KFX/AZW, Adobe-DRM EPUB) are encrypted, so neither Calibre nor pdfdol can read them; the DRM has to be removed first, where you are legally entitled to do so. Image-only sources (CBZ/CBR, DJVU, scanned PDF) do convert, but carry no text layer -- extracting their words needs OCR, which pdfdol does not do.

Custom converters

pdfdol maintains a format converter registry that maps file extensions to converter functions. You can register your own:

from pdfdol import register_format_converter, supported_extensions

def my_custom_converter(source):
    """source is a filepath (str) or raw bytes; must return PDF bytes."""
    ...

register_format_converter('.xyz', my_custom_converter)

# See everything that's currently supported
print(supported_extensions())

Get pdf from various sources

Example with a URL

pdf_data = get_pdf("https://pypi.org", src_kind="url")
print("Got PDF data of length:", len(pdf_data))

Example with HTML content

html_content = "<html><body><h1>Hello, PDF!</h1></body></html>"
pdf_data = get_pdf(html_content, src_kind="html")
print("Got PDF data of length:", len(pdf_data))

Example saving to file

filepath = get_pdf("https://pypi.org", egress="output.pdf", src_kind="url")
print("PDF saved to:", filepath)

Download files

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

Source Distribution

pdfdol-0.1.27.tar.gz (25.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pdfdol-0.1.27-py3-none-any.whl (27.3 kB view details)

Uploaded Python 3

File details

Details for the file pdfdol-0.1.27.tar.gz.

File metadata

  • Download URL: pdfdol-0.1.27.tar.gz
  • Upload date:
  • Size: 25.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.10.21

File hashes

Hashes for pdfdol-0.1.27.tar.gz
Algorithm Hash digest
SHA256 404842c42c67f98d062ee779b8d69ffda67d489c85353099351f2913aaaec6bb
MD5 b14f2c32c98134548617282b818f3bdb
BLAKE2b-256 e00db589a9810e9e464187e14dd695bba48db9d89d407b699f4baa1bfdb77d6d

See more details on using hashes here.

File details

Details for the file pdfdol-0.1.27-py3-none-any.whl.

File metadata

  • Download URL: pdfdol-0.1.27-py3-none-any.whl
  • Upload date:
  • Size: 27.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.10.21

File hashes

Hashes for pdfdol-0.1.27-py3-none-any.whl
Algorithm Hash digest
SHA256 5fb9d7f558806e79bba2855bc0f11a78135ab0a16279412e3040502fd5f680ba
MD5 abed01eae487182c6c4b810692e99ac5
BLAKE2b-256 1ba198ded98f621d22465a8c0c79e3f434ade6ef9231985bd5288ae28f6c81cc

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.27 This release

2 files

0.1.26

2 files

0.1.25

2 files

0.1.24

2 files

0.1.23

2 files

0.1.22

2 files

0.1.21

2 files

0.1.20

2 files

0.1.19

2 files

0.1.18

2 files

0.1.17

2 files

0.1.16

2 files

0.1.15

2 files

0.1.14

2 files

0.1.13

2 files

0.1.12

2 files

0.1.11

2 files

0.1.10

2 files

0.1.9

2 files

0.1.8

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page