Pydyf

0.12.1 · active · verified Mon Apr 06

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

Install

Imports

Quickstart

This example demonstrates how to create a basic PDF document, draw a red rectangle on a page, and save it to a file using pydyf.

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)

view raw JSON →