maxpmaxp / pdfreader

Python API for PDF documents
MIT License
117 stars 27 forks source link

Allow `PDFDocument` to be used in a context manager declaration #92

Closed Xepheryy closed 2 years ago

Xepheryy commented 2 years ago

Description

Make use of context managers to write slightly cleaner code for both PDFDocuments and SimplePDFViewers. Currently this is not supported and AttributeError: __enter__ is displayed as it is not implemented yet.

Intended usage

with PDFDocument(open('filepath', 'rb')) as doc:
    print(doc.header.version)

Current usage

with open('filepath', 'rb') as pdf:
    doc = PDFDocument(pdf)
    print(doc.header.version)

pdfreader Version 0.1.11

maxpmaxp commented 2 years ago

Nice suggestion. I'll add this feature when have time. Feel free to contribute btw!

maxpmaxp commented 2 years ago

@Xepheryy Fixed on master. Going to be a part of v0.1.12