Created
September 1, 2020 12:39
-
-
Save jakubnavratil/65c7674c4d644db107f25f021acd5dfd to your computer and use it in GitHub Desktop.
Tcpdf Fpdi With Links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use setasign\Fpdi\Tcpdf\Fpdi; | |
use setasign\Fpdi\PdfReader\PageBoundaries; | |
use setasign\Fpdi\PdfParser\Type\PdfType; | |
use setasign\Fpdi\PdfParser\Type\PdfDictionary; | |
use setasign\Fpdi\PdfParser\Type\PdfArray; | |
class TcpdfFpdiCustom extends Fpdi { | |
/** | |
* @inheritdoc | |
* @param int $pageNumber | |
* @param string $box | |
* @param bool $groupXObject | |
* @return string | |
*/ | |
public function importPage($pageNumber, $box = PageBoundaries::CROP_BOX, $groupXObject = true) | |
{ | |
$pageId = parent::importPage($pageNumber, $box, $groupXObject); | |
// gather links | |
$links = []; | |
$reader = $this->getPdfReader($this->currentReaderId); | |
$parser = $reader->getParser(); | |
$pageObj = $reader->getPage($pageNumber)->getPageObject(); | |
$annotationsObject = PdfDictionary::get(PdfType::resolve($pageObj, $parser), 'Annots'); | |
$annotations = PdfType::resolve($annotationsObject, $parser); | |
if($annotations->value) { | |
foreach($annotations->value as $annotationRef) { | |
$annotation = PdfType::resolve($annotationRef, $parser); | |
if(PdfDictionary::get($annotation, 'Type')->value === 'Annot' && PdfDictionary::get($annotation, 'Subtype')->value === 'Link') { | |
$a = PdfDictionary::get($annotation, 'A'); | |
if($a) { | |
$link = PdfType::resolve($a, $parser); | |
if(PdfDictionary::get($link, 'S')->value === 'URI') { | |
$rectObj = PdfDictionary::get($annotation, 'Rect'); | |
if($rectObj instanceof PdfArray) { | |
$rect = $rectObj->value; | |
$links[] = [ | |
$rect[0]->value, | |
$rect[1]->value, | |
$rect[2]->value - $rect[0]->value, | |
$rect[1]->value - $rect[3]->value, | |
PdfDictionary::get($link, 'URI')->value | |
]; | |
} | |
} | |
} | |
} | |
} | |
} | |
// save for future | |
$this->importedPages[$pageId]['links'] = $links; | |
return $pageId; | |
} | |
/** | |
* @inheritdoc | |
* @param mixed $tpl | |
* @param float|int|array $x | |
* @param float|int $y | |
* @param float|int|null $width | |
* @param float|int|null $height | |
* @param bool $adjustPageSize | |
* @return array | |
*/ | |
public function useTemplate($tpl, $x = 0, $y = 0, $width = null, $height = null, $adjustPageSize = false) | |
{ | |
$ret = parent::useTemplate($tpl, $x, $y, $width, $height, $adjustPageSize); | |
$links = $this->importedPages[$tpl]['links']; | |
$pxToU = $this->pixelsToUnits(1); | |
foreach ($links as $link) { | |
$this->Link( | |
$link[0] * $pxToU, | |
$this->getPageHeight() - $link[1] * $pxToU, | |
$link[2] * $pxToU, | |
$link[3] * $pxToU, | |
$link[4] | |
); | |
} | |
$ret; | |
} | |
} |
I just used this and it worked great for my external links. Thank you!!
If you are looking for a solution supporting both external and internal links (hyperlinks), using TCPDF and FPDI, check this answer here:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: Replace Fpdi constructor with this.
Currently copies only external links and not yet battle tested.