Skip to content

Commit 0ba18da

Browse files
jakejackson1claude
andcommitted
Find text outside ASCII with OverWrite() in a core-font document
In core-font mode the search strings were matched as raw UTF-8, while the page holds cp1252 bytes with its parentheses and backslashes escaped. Encode and escape them the same way the replacement already is. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 1995bf6 commit 0ba18da

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎src/Mpdf.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28327,7 +28327,9 @@ function OverWrite($file_in, $search, $replacement, $dest = Destination::DOWNLOA
2832728327
$replacement[$k] = $this->writer->escape($replacement[$k]);
2832828328
}
2832928329
} else {
28330-
foreach ($replacement as $k => $val) {
28330+
foreach ($search as $k => $val) {
28331+
$search[$k] = mb_convert_encoding($search[$k], $this->mb_enc, 'utf-8');
28332+
$search[$k] = $this->writer->escape($search[$k]);
2833128333
$replacement[$k] = mb_convert_encoding($replacement[$k], $this->mb_enc, 'utf-8');
2833228334
$replacement[$k] = $this->writer->escape($replacement[$k]);
2833328335
}

‎tests/Mpdf/OverWriteTest.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ public function testTextIsReplacedHoweverTheDocumentAndInstanceAreCompressed($do
108108
$this->assertSame([$filter, $filter], array_column($streams, 0));
109109
}
110110

111+
/**
112+
* A core font writes its text in cp1252 and escapes the parentheses, so the text searched for has to be written
113+
* the same way to be found
114+
*/
115+
public function testCoreFontTextWithCharactersOutsideAsciiIsReplaced()
116+
{
117+
$mpdf = new Mpdf(['mode' => 'c']);
118+
$mpdf->compress = false;
119+
$mpdf->WriteHTML('<p>Café (x) text</p>');
120+
$source = $mpdf->Output('', Destination::STRING_RETURN);
121+
$mpdf->cleanup();
122+
123+
$pdf = $this->mpdf->OverWrite($this->file($source), ['Café (x)'], ['Thé (y)'], Destination::STRING_RETURN);
124+
$text = implode("\n", array_column($this->streams($pdf), 1));
125+
126+
$this->assertStringContainsString("(Th\xE9 \\(y\\) text) Tj", $text);
127+
$this->assertStringNotContainsString('Caf', $text);
128+
}
129+
111130
/**
112131
* The offsets the method keeps have to still add up, or the reader has to repair the file
113132
*/

0 commit comments

Comments
 (0)