Skip to content

Instantly share code, notes, and snippets.

@jakubnavratil
Created September 1, 2020 12:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakubnavratil/65c7674c4d644db107f25f021acd5dfd to your computer and use it in GitHub Desktop.
Save jakubnavratil/65c7674c4d644db107f25f021acd5dfd to your computer and use it in GitHub Desktop.
Tcpdf Fpdi With Links
<?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;
}
}
@jakubnavratil
Copy link
Author

Usage: Replace Fpdi constructor with this.

$pdf = new TcpdfFpdiCustom();

Currently copies only external links and not yet battle tested.

@gavin310
Copy link

I just used this and it worked great for my external links. Thank you!!

@igorsgm
Copy link

igorsgm commented Apr 13, 2021

If you are looking for a solution supporting both external and internal links (hyperlinks), using TCPDF and FPDI, check this answer here:

https://stackoverflow.com/a/67071744/5397846

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment