Skip to content

Justified text with a Unicode font writes NUL bytes outside TJ strings (pypdf cannot open the PDF) #3771

Description

@juanlentino

In Cpdf::addText() (lib/Cpdf.php), word spacing for Unicode fonts is implemented with

$place_text = str_replace("\x00\x20", "\x00\x20)\x00\x20" . (-round($space_scale * $wordSpaceAdjust)) . "\x00\x20(", $place_text);

which places \x00 bytes between TJ array elements, outside any string. pypdf rejects the file (PdfReadError: Invalid Elementary Object starting with b'\x00'), and upload parsers built on it report the PDF as unreadable; pdf.js, poppler and PyMuPDF tolerate it.

Writing the number as plain ASCII renders identically (pdftoppm output byte-identical on our two-page document) and opens in pypdf:

$place_text = str_replace("\x00\x20", "\x00\x20) " . (-round($space_scale * $wordSpaceAdjust)) . " (", $place_text);

Repro: any text-align: justify paragraph with an embedded TTF font (Unicode), dompdf 3.1.6, then python -c "from pypdf import PdfReader; [p.extract_text() for p in PdfReader('out.pdf').pages]".

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions