diff --git a/src/Document/CMap/ToUnicode/ToUnicodeCMap.php b/src/Document/CMap/ToUnicode/ToUnicodeCMap.php index 1d38ca8c..e7e535d9 100644 --- a/src/Document/CMap/ToUnicode/ToUnicodeCMap.php +++ b/src/Document/CMap/ToUnicode/ToUnicodeCMap.php @@ -31,12 +31,13 @@ public function __construct( } /** @throws PdfParserException */ - public function textToUnicode(string $characterGroup): string { + public function textToUnicode(string $binaryString): string { $unicode = ''; - $chunkSize = $this->byteSize * 2; - $nrOfChunks = strlen($characterGroup) / $chunkSize; + $nrOfChunks = strlen($binaryString) / $this->byteSize; for ($i = 0; $i < $nrOfChunks; $i++) { - $unicode .= $this->charToUnicode((int) hexdec(substr($characterGroup, $i * $chunkSize, $chunkSize))) ?? ''; + $binaryChunk = substr($binaryString, $i * $this->byteSize, $this->byteSize); + $charCode = (int) hexdec(bin2hex($binaryChunk)); + $unicode .= $this->charToUnicode($charCode) ?? ''; } return $unicode; diff --git a/src/Document/ContentStream/PositionedText/TextSegment/TextSegment.php b/src/Document/ContentStream/PositionedText/TextSegment/TextSegment.php index 505b2a58..d5e43e20 100644 --- a/src/Document/ContentStream/PositionedText/TextSegment/TextSegment.php +++ b/src/Document/ContentStream/PositionedText/TextSegment/TextSegment.php @@ -37,10 +37,10 @@ private function decode(string $binaryString, ?EncodingNameValue $encoding, ?ToU } if ($toUnicodeCMap !== null) { - return $toUnicodeCMap->textToUnicode(bin2hex($binaryString)); + return $toUnicodeCMap->textToUnicode($binaryString); } - if ($encoding !== null) { + if ($encoding !== null && !in_array($encoding, [EncodingNameValue::IdentityH, EncodingNameValue::IdentityV], true)) { return $encoding->decodeString($binaryString); } diff --git a/src/Document/Dictionary/DictionaryValue/Name/EncodingNameValue.php b/src/Document/Dictionary/DictionaryValue/Name/EncodingNameValue.php index 9f9a2f5d..209a2e62 100644 --- a/src/Document/Dictionary/DictionaryValue/Name/EncodingNameValue.php +++ b/src/Document/Dictionary/DictionaryValue/Name/EncodingNameValue.php @@ -2,7 +2,6 @@ namespace PrinsFrank\PdfParser\Document\Dictionary\DictionaryValue\Name; -use PrinsFrank\PdfParser\Document\CMap\Registry\Adobe\Identity0; use PrinsFrank\PdfParser\Document\Encoding\MacRoman; use PrinsFrank\PdfParser\Document\Encoding\WinAnsi; use PrinsFrank\PdfParser\Exception\ParseFailureException; @@ -16,8 +15,6 @@ enum EncodingNameValue: string implements NameValue { public function decodeString(string $characterGroup): string { return match ($this) { - self::IdentityH, - self::IdentityV => (new Identity0())->getToUnicodeCMap()->textToUnicode($characterGroup), self::WinAnsiEncoding => WinAnsi::textToUnicode($characterGroup), self::MacRomanEncoding => MacRoman::textToUnicode($characterGroup), default => throw new ParseFailureException(sprintf('Unsupported encoding %s', $this->name)), diff --git a/tests/Samples/files/issue-534/contents.yml b/tests/Samples/files/issue-534/contents.yml new file mode 100644 index 00000000..a1a415b4 --- /dev/null +++ b/tests/Samples/files/issue-534/contents.yml @@ -0,0 +1,17 @@ +# yaml-language-server: $schema=../../schema.json +version: '1.7' +userPassword: null +ownerPassword: null +fileEncryptionKey: null +title: null +producer: null +author: null +creator: null +subject: null +keywords: null +creationDate: null +modificationDate: null +pages: + - + text: 'Invoice 2026-0912 reference ABCDEF' + markdown: 'Invoice 2026-0912 reference ABCDEF' diff --git a/tests/Samples/files/issue-534/file.pdf b/tests/Samples/files/issue-534/file.pdf new file mode 100644 index 00000000..b2708ac0 Binary files /dev/null and b/tests/Samples/files/issue-534/file.pdf differ