Skip to content

Commit

Permalink
Fixed importing of links in none PdfDocEncoding
Browse files Browse the repository at this point in the history
  • Loading branch information
JanSlabon committed Sep 25, 2024
1 parent 04065f3 commit fbd3570
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/FpdfTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ protected function _putlinks($n)
$rect = sprintf('%.2F %.2F %.2F %.2F', $pl[0], $pl[1], $pl[0] + $pl[2], $pl[1] - $pl[3]);
$this->_put('<</Type /Annot /Subtype /Link /Rect [' . $rect . ']', false);
if (is_string($pl[4])) {
$this->_put('/A <</S /URI /URI ' . $this->_textstring($pl[4]) . '>>');
if (isset($pl['importedLink'])) {
$this->_put('/A <</S /URI /URI (' . $this->_escape($pl[4]) . ')>>');
$values = $pl['importedLink']['pdfObject']->value;

foreach ($values as $name => $entry) {
Expand All @@ -158,6 +158,7 @@ protected function _putlinks($n)
$this->_put($s);
}
} else {
$this->_put('/A <</S /URI /URI ' . $this->_textstring($pl[4]) . '>>');
$this->_put('/Border [0 0 0]', false);
}
$this->_put('>>');
Expand Down
2 changes: 2 additions & 0 deletions src/PdfReader/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ public function getContentStream()
* All coordinates are normalized in view to rotation and translation of the boundary-box, so that their
* origin is lower-left.
*
* The URI is the binary value of the PDF string object. It can be in PdfDocEncoding or in UTF-16BE encoding.
*
* @return array
*/
public function getExternalLinks($box = PageBoundaries::CROP_BOX)
Expand Down
Binary file added tests/_files/pdfs/links/tuto6.pdf
Binary file not shown.
27 changes: 27 additions & 0 deletions tests/functional/LinkHandling/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -741,4 +741,31 @@ public function testImportOfSpecialPageBoundaries()
$reader = new PdfReader(new PdfParser(StreamReader::createByString($pdfString)));
$this->compareExpectedLinks(1, $expectedLinks, $reader);
}

public function testLinkInUtf16Encoding()
{
$pdf = $this->getInstance();
$pdf->AddPage();
// This file has its link in UTF-16BE saved.
$pdf->setSourceFile(__DIR__ . '/../../_files/pdfs/links/tuto6.pdf');
$tplId = $pdf->importPage(2, PageBoundaries::CROP_BOX, true, true);
$pdf->useTemplate($tplId);
$pdfString = $this->save($pdf);
// file_put_contents(__DIR__ . '/test.pdf', $pdfString);

$expectedLinks = [
[
// the strings are in UTF-16BE: http://pdf.wtf/ümlaut
'uri' => "\xFE\xFF\x00h\x00t\x00t\x00p\x00:\x00/\x00/\x00p\x00d\x00f\x00.\x00w\x00t\x00f\x00/\x00\xfc\x00m\x00l\x00a\x00u\x00t",
'rect' => [28.35, 749.82, 113.39, 807.87],
],
[
'uri' => "\xFE\xFF\x00h\x00t\x00t\x00p\x00:\x00/\x00/\x00p\x00d\x00f\x00.\x00w\x00t\x00f\x00/\x00\xfc\x00m\x00l\x00a\x00u\x00t",
'rect' => [387.18, 756.93, 468.87, 770.93],
],
];

$reader = new PdfReader(new PdfParser(StreamReader::createByString($pdfString)));
$this->compareExpectedLinks(1, $expectedLinks, $reader);
}
}
16 changes: 16 additions & 0 deletions tests/functional/PdfReader/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ public function getExternalLinksProvider()
[
1 => []
]
],
[
__DIR__ . '/../../_files/pdfs/links/tuto6.pdf',
[
2 => [
[
// the strings are in UTF-16BE: http://pdf.wtf/ümlaut
'uri' => "\xFE\xFF\x00h\x00t\x00t\x00p\x00:\x00/\x00/\x00p\x00d\x00f\x00.\x00w\x00t\x00f\x00/\x00\xfc\x00m\x00l\x00a\x00u\x00t",
'rect' => new Rectangle(28.35, 807.87, 113.39, 749.82)
],
[
'uri' => "\xFE\xFF\x00h\x00t\x00t\x00p\x00:\x00/\x00/\x00p\x00d\x00f\x00.\x00w\x00t\x00f\x00/\x00\xfc\x00m\x00l\x00a\x00u\x00t",
'rect' => new Rectangle(387.18, 770.93, 468.87, 756.93)
],
]
]
]
];
}
Expand Down

0 comments on commit fbd3570

Please sign in to comment.