Pydyf
pydyf is a low-level PDF generator written in Python and based on PDF specification 1.7. It provides fine-grained control over PDF features such as bookmarks, attachments, links, color spaces, and text capabilities. The library is actively maintained with frequent releases, typically every 1-3 months.
Warnings
- breaking Python 3.8 and 3.9 are no longer supported. Users must upgrade to Python 3.10 or newer.
- breaking Deprecated methods `color_space`, `shading`, `text_matrix`, and `transform` have been removed from `pydyf.Stream`.
- breaking The deprecated `version` and `identifier` parameters were removed from `pydyf.PDF.__init__`. Calling `PDF()` with these arguments will now raise a `TypeError`.
Install
-
pip install pydyf
Imports
- PDF
from pydyf import PDF
- Stream
from pydyf import Stream
- Dictionary
from pydyf import Dictionary
- Array
from pydyf import Array
Quickstart
import pydyf
document = pydyf.PDF()
draw = pydyf.Stream()
# Set the drawing color to red
draw.set_color_rgb(1.0, 0.0, 0.0)
# Draw a rectangle
draw.rectangle(10, 10, 100, 50)
draw.fill() # Fill the rectangle
# Add a page with the drawing stream
document.add_page(pydyf.Dictionary({
'Type': '/Page',
'MediaBox': pydyf.Array([0, 0, 200, 200]),
'Contents': draw,
}))
# Write the PDF to a file
with open('output.pdf', 'wb') as f:
document.write(f)