diff --git a/src/FpdfTrait.php b/src/FpdfTrait.php index 7c79bb7..e48ee9d 100644 --- a/src/FpdfTrait.php +++ b/src/FpdfTrait.php @@ -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('<_put('/A <_textstring($pl[4]) . '>>'); if (isset($pl['importedLink'])) { + $this->_put('/A <_escape($pl[4]) . ')>>'); $values = $pl['importedLink']['pdfObject']->value; foreach ($values as $name => $entry) { @@ -158,6 +158,7 @@ protected function _putlinks($n) $this->_put($s); } } else { + $this->_put('/A <_textstring($pl[4]) . '>>'); $this->_put('/Border [0 0 0]', false); } $this->_put('>>'); diff --git a/src/PdfReader/Page.php b/src/PdfReader/Page.php index a002d14..c2463dd 100644 --- a/src/PdfReader/Page.php +++ b/src/PdfReader/Page.php @@ -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) diff --git a/tests/_files/pdfs/links/tuto6.pdf b/tests/_files/pdfs/links/tuto6.pdf new file mode 100644 index 0000000..d99a86f Binary files /dev/null and b/tests/_files/pdfs/links/tuto6.pdf differ diff --git a/tests/functional/LinkHandling/AbstractTest.php b/tests/functional/LinkHandling/AbstractTest.php index 11f7f1e..7381f87 100644 --- a/tests/functional/LinkHandling/AbstractTest.php +++ b/tests/functional/LinkHandling/AbstractTest.php @@ -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); + } } diff --git a/tests/functional/PdfReader/PageTest.php b/tests/functional/PdfReader/PageTest.php index dcf1a66..8cdfe83 100644 --- a/tests/functional/PdfReader/PageTest.php +++ b/tests/functional/PdfReader/PageTest.php @@ -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) + ], + ] + ] ] ]; }