Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve indirect objects when reading metadata #1183

Closed
MartinThoma opened this issue Jul 30, 2022 · 1 comment · Fixed by #1851
Closed

Resolve indirect objects when reading metadata #1183

MartinThoma opened this issue Jul 30, 2022 · 1 comment · Fixed by #1851
Assignees
Labels
is-feature A feature request PdfReader The PdfReader component is affected

Comments

@MartinThoma
Copy link
Member

Explanation

The pattern writer.add_metadata(reader.metadata) doesn't work as there can be indirect objects.
This is unfortunate and an unnecessary complexity for the user.

Code Example

from PyPDF2 import PdfReader, PdfWriter

reader = PdfReader("example.pdf")
writer = PdfWriter()

# How I want to use it
writer.add_metadata(reader.metadata)
@MartinThoma MartinThoma self-assigned this Jul 30, 2022
@MartinThoma
Copy link
Member Author

I imagine something like this on top of the existing code:

def get_meta(reader: PdfReader) -> Dict[str, Any]:
    meta_new = {}
    for key, value in reader.metadata.items():
        i = 0  # I'm not sure if there could be infinite loops
        while isinstance(value, IndirectObject) and i < 3:
            value = reader.metadata[key].get_object()
            i += 1
        if not isinstance(value, IndirectObject):
            meta_new[key] = value
    return meta_new

@MartinThoma MartinThoma added PdfReader The PdfReader component is affected is-feature A feature request labels Jul 30, 2022
pubpub-zz added a commit to pubpub-zz/pypdf that referenced this issue May 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is-feature A feature request PdfReader The PdfReader component is affected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant