Skip to content

fix: serialize booleans as lowercase keywords in create_dictionary_string - #1973

Merged
andersonhc merged 1 commit into
py-pdf:masterfrom
agustin18:fix/pdfdict-serialize-boolean
Sep 29, 2026
Merged

andersonhc merged 1 commit into
py-pdf:masterfrom
agustin18:fix/pdfdict-serialize-boolean

Conversation

@agustin18

Copy link
Copy Markdown

Description

In create_dictionary_string() (fpdf/syntax.py), dictionary entries are serialized by stringifying keys and values:

field_join.join(key_value_join.join((k, str(v))) for k, v in dict_.items())

When a dictionary contains native Python boolean values (True or False), calling raw str(v) produces uppercase True or False (e.g. <</Marked True>>), and has_empty_fields=True inadvertently stripped False and 0 due to if v.

This violates the PDF 1.7 / 2.0 specification (ISO 32000-1 § 7.3.2: boolean objects must be written as the lowercase keywords true and false).

This fix:

  1. Serializes boolean values with str(v).lower() in create_dictionary_string(), ensuring strict compliance with ISO 32000-1 § 7.3.2.
  2. Ensures clear_empty_fields() preserves significant falsy values (False and numeric 0) while filtering out None and empty values.
  3. Extends test/test_syntax.py with parameterized test coverage for boolean dictionary serialization.

@andersonhc
andersonhc merged commit dd0d451 into py-pdf:master Sep 29, 2026
23 checks passed
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