Skip to content

fix PDF writer bug when encountering free object#61

Merged
claird merged 1 commit intoclaird:masterfrom
askerlee:master
Jun 21, 2020
Merged

fix PDF writer bug when encountering free object#61
claird merged 1 commit intoclaird:masterfrom
askerlee:master

Conversation

@askerlee
Copy link
Copy Markdown

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