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

fix PDF writer bug when encountering free object #61

Merged
merged 1 commit into from
Jun 21, 2020
Merged

fix PDF writer bug when encountering free object #61

merged 1 commit into from
Jun 21, 2020

Conversation

askerlee
Copy link

When editing an existing PDF, occasionally the PDF contains a "free" IndirectObject that refers to nothing (correct me if I'm conceptually wrong). In this situation the reader will raise a PdfReadError:

            warnings.warn(
                "Object %d %d not defined." %
                (ref.idnum, ref.generation), PdfReadWarning
            )
            raise PdfReadError(
                "Could not find object (%d, %d)" % (ref.idnum, ref.generation)
            )

This exception will be passed to writer._sweepIndirectReferences(). However the except statement doesn't capture this exception, but only captures ValueError, which is not a base class of PdfReadError:

                     except ValueError:
                        # Unable to resolve the Object, returning NullObject
                        # instead.
                        warnings.warn(
                            "Unable to resolve [{}: {}], returning NullObject "
                            "instead".format(data.__class__.__name__, data)
                        )
                        return NullObject()

This will crash the script.

Hence this pull request updates this line to handle the PdfReadError exception. To be safer, it's modified to capture its base class, PyPdfError:

                     except (ValueError, PyPdfError):
                     ...

@claird claird merged commit 74189aa into claird:master Jun 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants