Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed importing of links in none PdfDocEncoding #219

Merged
merged 2 commits into from
Oct 10, 2024
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
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
Loading