Skip to content

AssertionError when calling PdfWriter.add_metadata: "mypy" #3491

@yannickBona

Description

@yannickBona

PdfWriter.add_metadata raises an AssertionError when invoked for a metadata dict containing what it seems corrupted utf-16 byte string.

Environment

Which environment were you using when you encountered the problem?

$ python -m platform
Linux-6.10.14-linuxkit-x86_64-with

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==5.4.0, crypt_provider=('cryptography', '44.0.2'), PIL=11.1.0

Code + PDF

This is a minimal, complete example that shows the issue:

reader = PdfReader(pdf_content) # pdf_content: IO[bytes]
writer = PdfWriter()
writer.add_metadata(reader.metadata)

Here's the metadata for the PDF:

[ LOCAL_DEV ]
In [16]: reader.metadata
Out[16]:
{'/CreationDate': "D:20240807153803+02'00'",
 '/Creator': 'PDFium',
 '/Keywords': b'\xfe\xff\x00\x00\x00\x000\xa0\xec_\xf1\x9c\x9fT\x00\x03\x00\x003@\xec_\xf3\xf0\x9f\x18\x00\x1d\x00\x00\x00\x1e\x00\x00\xd0,\x9f\x0c\x88\xf0\x9f%\x00\x0b\x00\x00\x00\x1d\x00\x00\xc4I\xe9\x96\x00\x00\x00\x00\x00\x00\x00\x000\xd4\xec_\xb0^\x9f\x82\xb0\\\x9f\x82\xf5h\x9e\xad\x19\x80\x9fs[\x98\xe9Y\x00\x04\x00\x00\x19\xc0\x9e\xbe\x00\x00\x00\x00\x00\x01\x00\x000\xa0\xec_\x00\x0b\x00\x00\x00\x00\x00\x003@\xec_\xf3\x80\x9f\x18\xef\xe6\x9f\xc2\x00\x18\x00\x00\xcdt\x9f\x0c0\xa0\xec_\x19\x80\x9fs3@\xec_\xb4T\x9f\x1c\x00\x00\x00\x00\x00X\x00\x00\x00X\x00\x00\x00X\x00\x003\xcf\x14\x02\xd0,\x9f\x0c\x19\xc0\x9e\xbe\xf5h\x9e\xad\x13\x80\xef\x16\t\xac\xe9\xb3!\x80\xc7\x81\xa8@\xff\x9a\xf5h\x9e\xad\x0e5\xe9wl0\xc7\x80\x00\x00\x00\x000\xa0\xec_\x00\x02\x00\x00\t\xac\xe9\xb3\xa84\xff\x9al0\xc7\x80\x00\x00\x00\x00\xa8@\xff\x9a\xe0s\xe9\xae3\xcf\x14\x02\x13\x80\xef\x16W\xcc\xea\xb13\xcf\x14\x02\x00\x00\x00\x00n\x00\xef\x1e\x86\x80\xe9]\xf1\xa0\x9fT\xd8\x00\xef\'\x00\x06\x00\x00\x00\x06\x00\x00K\xb7q\x95\xa8\xfc\xff\x9al0\xc7\x80\xd0,\x9f\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa9D\xff\x9a\xa9\x10\xff\x9a\xf1\x9c\x9fT\x00\x00\x00\x00\x86\x80\xe9]\'\x9e\x9f\xb5\x1a0\xe9^W\xcc\xea\xb1\xf4\xd8\xc7\xd9c\x10\xc8\xd5\x18\x00\xc7\xda\xff\xff\xff\xffbH\xc8\xd5\'\x9e\x9f\xb5\xa9|\xff\x9a\xf4\xd8\xc7\xd9I\x98\xc8\xd5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x18\x00\xc7\xda\x00\x00\x00\x00Mp\xc9YMx\xc9Y"0\xc8\xd5\x00\x00\x00\x00\xf4\xd8\xc7\xd9I\x98\xc8\xd5\xb6\x80\x93\xa7\xff\xff\xff\xffp\x00\x87j\x00\x00\x00\x00\x00\x03\x00\x00\x18\x00\xc7\xda\x00\x00\x00\x00Mp\xc9YMx\xc9Y"0\xc8\xd5\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00&\xbc\x9f\xb5\xaat\xff\x9a\xaa\x08\xff\x9a@n\x00\x00\x00\x00\x00\x00n\x00\xef\x1e\x86\x80\xe9]\xfcF\x9f\xb4\x1d\x84\xe9^X,\xea\xb1\xa9\xe8\xff\x9a) \xc7\xd9\x00\x13\x00\x00\xc3\xbe\x9f\xc2\x00\x05\x00\x003\xcf\x14\x02\x00\x00\x00\x00\xfcF\x9f\xb4\xaa\xec\xff\x9a\xbd@\xc8\xcb\xfc\xa0\xc8\xd4\xfc\xc0\xc8\xd4\xfc\xe8\xc8\xd4\xfc\x80\xc8\xd4\xf4\xd8\xc7\xd9\x00\x00\x00\x00\x14\x10\xc8\xd5\x19\xe8\xc8\xd5\xa5\xc0\xc7\xde"0\xc8\xd5Mp\xc9Y) \xc7\xd9 \xe0\xc8\xd5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Mp',
 '/ModDate': "D:20240807153803+02'00'",
 '/Producer': 'iText 2.1.7 by 1T3XT'}

Traceback

This is the complete traceback I see:

[ LOCAL_DEV ]
In [10]: writer.add_metadata(reader.metadata)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[10], line 1
----> 1 writer.add_metadata(reader.metadata)

File /opt/venv/lib/python3.12/site-packages/pypdf/_writer.py:1671, in PdfWriter.add_metadata(self, infos)
   1669     if isinstance(value, PdfObject):
   1670         value = value.get_object()
-> 1671     args[NameObject(key)] = create_string_object(str(value))
   1672 if self._info is None:
   1673     self._info = DictionaryObject()

File /opt/venv/lib/python3.12/site-packages/pypdf/generic/_utils.py:142, in create_string_object(string, forced_encoding)
    127 """
    128 Create a ByteStringObject or a TextStringObject from a string to represent the string.
    129
   (...)    139
    140 """
    141 if isinstance(string, str):
--> 142     return TextStringObject(string)
    143 if isinstance(string, bytes):
    144     if isinstance(forced_encoding, (list, dict)):

File /opt/venv/lib/python3.12/site-packages/pypdf/generic/_base.py:672, in TextStringObject.__new__(cls, value)
    670 o.utf16_bom = b""
    671 if o.startswith(("\xfe\xff", "\xff\xfe")):
--> 672     assert org is not None, "mypy"
    673     try:
    674         o = str.__new__(cls, org.decode("utf-16"))

AssertionError: mypy

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions