Advertisement
Guest User

Untitled

a guest
Feb 12th, 2021
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.12 KB | None | 0 0
  1. <?php
  2.  
  3. use setasign\Fpdi\Fpdi;
  4. use setasign\Fpdi\PdfReader\PageBoundaries;
  5. use setasign\Fpdi\PdfParser\Type\PdfType;
  6. use setasign\Fpdi\PdfParser\Type\PdfDictionary;
  7. use setasign\Fpdi\PdfParser\Type\PdfArray;
  8.  
  9. class myFpdi extends Fpdi {
  10.  
  11.     /**
  12.      * @inheritdoc
  13.      * @param int $pageNumber
  14.      * @param string $box
  15.      * @param bool $groupXObject
  16.      * @return string
  17.      */
  18.     public function importPage($pageNumber, $box = PageBoundaries::CROP_BOX, $groupXObject = true)
  19.     {
  20.         $pageId = parent::importPage($pageNumber, $box, $groupXObject);
  21.  
  22.         // gather links
  23.         $links = [];
  24.         $reader = $this->getPdfReader($this->currentReaderId);
  25.         $parser = $reader->getParser();
  26.         $pageObj = $reader->getPage($pageNumber)->getPageObject();
  27.         $annotationsObject = PdfDictionary::get(PdfType::resolve($pageObj, $parser), 'Annots');
  28.         $annotations = PdfType::resolve($annotationsObject, $parser);
  29.         if($annotations->value) {
  30.             foreach($annotations->value as $annotationRef) {
  31.                 $annotation = PdfType::resolve($annotationRef, $parser);
  32.                 if(PdfDictionary::get($annotation, 'Type')->value === 'Annot' && PdfDictionary::get($annotation, 'Subtype')->value === 'Link') {
  33.                     $a = PdfDictionary::get($annotation, 'A');
  34.                     if($a) {
  35.                         $link = PdfType::resolve($a, $parser);
  36.                         if(PdfDictionary::get($link, 'S')->value === 'URI') {
  37.                             $rectObj = PdfDictionary::get($annotation, 'Rect');
  38.                             if($rectObj instanceof PdfArray) {
  39.                                 $rect = $rectObj->value;
  40.                                 $links[] = [
  41.                                     $rect[0]->value,
  42.                                     $rect[1]->value,
  43.                                     $rect[2]->value - $rect[0]->value,
  44.                                     $rect[1]->value - $rect[3]->value,
  45.                                     PdfDictionary::get($link, 'URI')->value
  46.                                 ];
  47.                             }
  48.                         }
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.  
  54.         // save for future
  55.         $this->importedPages[$pageId]['links'] = $links;
  56.  
  57.         return $pageId;
  58.     }
  59.  
  60.     /**
  61.      * @inheritdoc
  62.      * @param mixed $tpl
  63.      * @param float|int|array $x
  64.      * @param float|int $y
  65.      * @param float|int|null $width
  66.      * @param float|int|null $height
  67.      * @param bool $adjustPageSize
  68.      * @return array
  69.      */
  70.     public function useTemplate($tpl, $x = 0, $y = 0, $width = null, $height = null, $adjustPageSize = false)
  71.     {
  72.         $ret = parent::useTemplate($tpl, $x, $y, $width, $height, $adjustPageSize);
  73.         $links = $this->importedPages[$tpl]['links'];
  74.         foreach ($links as $link) {
  75.             $this->Link(
  76.                 $link[0],
  77.                 $this->getPageHeight() - $link[1],
  78.                 $link[2],
  79.                 $link[3],
  80.                 $link[4]
  81.             );
  82.         }
  83.         return $ret;
  84.     }
  85.    
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement