Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use setasign\Fpdi\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 myFpdi 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'];
- foreach ($links as $link) {
- $this->Link(
- $link[0],
- $this->getPageHeight() - $link[1],
- $link[2],
- $link[3],
- $link[4]
- );
- }
- return $ret;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement