Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Document/CMap/ToUnicode/ToUnicodeCMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)),
Expand Down
17 changes: 17 additions & 0 deletions tests/Samples/files/issue-534/contents.yml
Original file line number Diff line number Diff line change
@@ -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'
Binary file added tests/Samples/files/issue-534/file.pdf
Binary file not shown.
Loading