From 41e4d042785056e362f2e9b3a91f757217647ab5 Mon Sep 17 00:00:00 2001 From: Diego Ponciano Date: Thu, 7 Jul 2022 10:27:23 -0600 Subject: [PATCH 1/5] - Adds support for a FPDF_FONTPATH .env variable --- src/FpdfServiceProvider.php | 4 ++++ src/config/fpdf.php | 1 + 2 files changed, 5 insertions(+) diff --git a/src/FpdfServiceProvider.php b/src/FpdfServiceProvider.php index afb4d2b..3eb183e 100644 --- a/src/FpdfServiceProvider.php +++ b/src/FpdfServiceProvider.php @@ -18,6 +18,10 @@ class FpdfServiceProvider extends ServiceProvider */ public function boot() { + if(config('fpdf.fontpath')){ + define('FPDF_FONTPATH', config('fpdf.fontpath')); + } + $this->publishes([ __DIR__.'/config/fpdf.php' => config_path('fpdf.php'), ], 'config'); diff --git a/src/config/fpdf.php b/src/config/fpdf.php index de440b4..d216fa1 100644 --- a/src/config/fpdf.php +++ b/src/config/fpdf.php @@ -14,6 +14,7 @@ 'orientation' => 'P', 'unit' => 'mm', 'size' => 'A4', + 'fontpath' => env('FPDF_FONTPATH', false), /* |-------------------------------------------------------------------------- From ad155c9603dcf3c56d45179a577863c984c32f8f Mon Sep 17 00:00:00 2001 From: Diego Ponciano Date: Fri, 8 Jul 2022 15:46:17 -0600 Subject: [PATCH 2/5] - Adds option to use a memory optimization extension --- src/Extensions/FpdfOptimize.php | 142 ++++++++++++++++++++++++++++++++ src/FpdfServiceProvider.php | 23 ++++-- src/config/fpdf.php | 1 + 3 files changed, 160 insertions(+), 6 deletions(-) create mode 100644 src/Extensions/FpdfOptimize.php diff --git a/src/Extensions/FpdfOptimize.php b/src/Extensions/FpdfOptimize.php new file mode 100644 index 0000000..521c84f --- /dev/null +++ b/src/Extensions/FpdfOptimize.php @@ -0,0 +1,142 @@ +Error('Version 1.8 or above is required by this extension'); + $this->f=fopen($file,'wb'); + if(!$this->f) + $this->Error('Unable to create output file: '.$file); + $this->_putheader(); + } + + function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link=''){ + if(!isset($this->images[$file])){ + //Retrieve only meta-information + $a=getimagesize($file); + if($a===false) + $this->Error('Missing or incorrect image file: '.$file); + $this->images[$file]=array('w'=>$a[0],'h'=>$a[1],'type'=>$a[2],'i'=>count($this->images)+1); + } + parent::Image($file,$x,$y,$w,$h,$type,$link); + } + + function Output($dest='', $name='', $isUTF8=false){ + if($this->state<3) + $this->Close(); + } + + function _endpage(){ + parent::_endpage(); + //Write page to file + $this->_putstreamobject($this->pages[$this->page]); + unset($this->pages[$this->page]); + } + + function _getoffset(){ + return ftell($this->f); + } + + function _put($s){ + fwrite($this->f,$s."\n",strlen($s)+1); + } + + function _putimages(){ + foreach(array_keys($this->images) as $file) + { + $type=$this->images[$file]['type']; + if($type==1) + $info=$this->_parsegif($file); + elseif($type==2) + $info=$this->_parsejpg($file); + elseif($type==3) + $info=$this->_parsepng($file); + else + $this->Error('Unsupported image type: '.$file); + $this->_putimage($info); + $this->images[$file]['n']=$info['n']; + unset($info); + } + } + + function _putpages(){ + $nb=$this->page; + for($n=1;$n<=$nb;$n++) + $this->PageInfo[$n]['n']=$this->n+$n; + if($this->DefOrientation=='P') + { + $wPt=$this->DefPageSize[0]*$this->k; + $hPt=$this->DefPageSize[1]*$this->k; + } + else + { + $wPt=$this->DefPageSize[1]*$this->k; + $hPt=$this->DefPageSize[0]*$this->k; + } + //Page objects + for($n=1;$n<=$nb;$n++) + { + $this->_newobj(); + $this->_put('<_put('/Parent 1 0 R'); + if(isset($this->PageInfo[$n]['size'])) + $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageInfo[$n]['size'][0],$this->PageInfo[$n]['size'][1])); + if(isset($this->PageInfo[$n]['rotation'])) + $this->_put('/Rotate '.$this->PageInfo[$n]['rotation']); + $this->_put('/Resources 2 0 R'); + if(isset($this->PageLinks[$n])) + { + //Links + $annots='/Annots ['; + foreach($this->PageLinks[$n] as $pl) + { + $rect=sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]); + $annots.='<_textstring($pl[4]).'>>>>'; + else + { + $l=$this->links[$pl[4]]; + if(isset($this->PageInfo[$l[0]]['size'])) + $h=$this->PageInfo[$l[0]]['size'][1]; + else + $h=$hpt; + $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',$this->PageInfo[$l[0]]['n'],$h-$l[1]*$this->k); + } + } + $this->_put($annots.']'); + } + if($this->WithAlpha) + $this->_put('/Group <>'); + $this->_put('/Contents '.(2+$n).' 0 R>>'); + $this->_put('endobj'); + } + //Pages root + $this->offsets[1]=$this->_getoffset(); + $this->_put('1 0 obj'); + $this->_put('<_put($kids.']'); + $this->_put('/Count '.$nb); + $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt)); + $this->_put('>>'); + $this->_put('endobj'); + } + + function _putheader(){ + if($this->_getoffset()==0) + parent::_putheader(); + } + + function _enddoc(){ + parent::_enddoc(); + fclose($this->f); + } +} diff --git a/src/FpdfServiceProvider.php b/src/FpdfServiceProvider.php index 3eb183e..f991c14 100644 --- a/src/FpdfServiceProvider.php +++ b/src/FpdfServiceProvider.php @@ -1,6 +1,7 @@ app->singleton('fpdf', function() - { - return new Fpdf\Fpdf( - config('fpdf.orientation'), config('fpdf.unit'), config('fpdf.size') - ); - }); + if(config('fpdf.optimize')){ + $this->app->singleton('fpdf', function() + { + return new FpdfOptimize( + config('fpdf.orientation'), config('fpdf.unit'), config('fpdf.size') + ); + }); + }else{ + $this->app->singleton('fpdf', function() + { + return new Fpdf\Fpdf( + config('fpdf.orientation'), config('fpdf.unit'), config('fpdf.size') + ); + }); + } + } /** diff --git a/src/config/fpdf.php b/src/config/fpdf.php index d216fa1..7487ed5 100644 --- a/src/config/fpdf.php +++ b/src/config/fpdf.php @@ -15,6 +15,7 @@ 'unit' => 'mm', 'size' => 'A4', 'fontpath' => env('FPDF_FONTPATH', false), + 'optimize' => env('FPDF_OPTIMIZE', false) /* |-------------------------------------------------------------------------- From f53e564ba1a0432c4a2d7b93fdf6e69500e5c249 Mon Sep 17 00:00:00 2001 From: Diego Ponciano Date: Fri, 8 Jul 2022 15:47:53 -0600 Subject: [PATCH 3/5] - Fix indentation to use spaces on FdpfOptimize --- src/Extensions/FpdfOptimize.php | 252 ++++++++++++++++---------------- 1 file changed, 126 insertions(+), 126 deletions(-) diff --git a/src/Extensions/FpdfOptimize.php b/src/Extensions/FpdfOptimize.php index 521c84f..89fa1b7 100644 --- a/src/Extensions/FpdfOptimize.php +++ b/src/Extensions/FpdfOptimize.php @@ -3,140 +3,140 @@ use Codedge\Fpdf\Fpdf\Fpdf; class FpdfOptimize extends Fpdf { - - protected $f; + + protected $f; - function Open($file='doc.pdf'){ - if(FPDF_VERSION<'1.8') - $this->Error('Version 1.8 or above is required by this extension'); - $this->f=fopen($file,'wb'); - if(!$this->f) - $this->Error('Unable to create output file: '.$file); - $this->_putheader(); - } + function Open($file='doc.pdf'){ + if(FPDF_VERSION<'1.8') + $this->Error('Version 1.8 or above is required by this extension'); + $this->f=fopen($file,'wb'); + if(!$this->f) + $this->Error('Unable to create output file: '.$file); + $this->_putheader(); + } - function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link=''){ - if(!isset($this->images[$file])){ - //Retrieve only meta-information - $a=getimagesize($file); - if($a===false) - $this->Error('Missing or incorrect image file: '.$file); - $this->images[$file]=array('w'=>$a[0],'h'=>$a[1],'type'=>$a[2],'i'=>count($this->images)+1); - } - parent::Image($file,$x,$y,$w,$h,$type,$link); - } + function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link=''){ + if(!isset($this->images[$file])){ + //Retrieve only meta-information + $a=getimagesize($file); + if($a===false) + $this->Error('Missing or incorrect image file: '.$file); + $this->images[$file]=array('w'=>$a[0],'h'=>$a[1],'type'=>$a[2],'i'=>count($this->images)+1); + } + parent::Image($file,$x,$y,$w,$h,$type,$link); + } - function Output($dest='', $name='', $isUTF8=false){ - if($this->state<3) - $this->Close(); - } + function Output($dest='', $name='', $isUTF8=false){ + if($this->state<3) + $this->Close(); + } - function _endpage(){ - parent::_endpage(); - //Write page to file - $this->_putstreamobject($this->pages[$this->page]); - unset($this->pages[$this->page]); - } + function _endpage(){ + parent::_endpage(); + //Write page to file + $this->_putstreamobject($this->pages[$this->page]); + unset($this->pages[$this->page]); + } - function _getoffset(){ - return ftell($this->f); - } + function _getoffset(){ + return ftell($this->f); + } - function _put($s){ - fwrite($this->f,$s."\n",strlen($s)+1); - } + function _put($s){ + fwrite($this->f,$s."\n",strlen($s)+1); + } - function _putimages(){ - foreach(array_keys($this->images) as $file) - { - $type=$this->images[$file]['type']; - if($type==1) - $info=$this->_parsegif($file); - elseif($type==2) - $info=$this->_parsejpg($file); - elseif($type==3) - $info=$this->_parsepng($file); - else - $this->Error('Unsupported image type: '.$file); - $this->_putimage($info); - $this->images[$file]['n']=$info['n']; - unset($info); - } - } + function _putimages(){ + foreach(array_keys($this->images) as $file) + { + $type=$this->images[$file]['type']; + if($type==1) + $info=$this->_parsegif($file); + elseif($type==2) + $info=$this->_parsejpg($file); + elseif($type==3) + $info=$this->_parsepng($file); + else + $this->Error('Unsupported image type: '.$file); + $this->_putimage($info); + $this->images[$file]['n']=$info['n']; + unset($info); + } + } - function _putpages(){ - $nb=$this->page; - for($n=1;$n<=$nb;$n++) - $this->PageInfo[$n]['n']=$this->n+$n; - if($this->DefOrientation=='P') - { - $wPt=$this->DefPageSize[0]*$this->k; - $hPt=$this->DefPageSize[1]*$this->k; - } - else - { - $wPt=$this->DefPageSize[1]*$this->k; - $hPt=$this->DefPageSize[0]*$this->k; - } - //Page objects - for($n=1;$n<=$nb;$n++) - { - $this->_newobj(); - $this->_put('<_put('/Parent 1 0 R'); - if(isset($this->PageInfo[$n]['size'])) - $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageInfo[$n]['size'][0],$this->PageInfo[$n]['size'][1])); - if(isset($this->PageInfo[$n]['rotation'])) - $this->_put('/Rotate '.$this->PageInfo[$n]['rotation']); - $this->_put('/Resources 2 0 R'); - if(isset($this->PageLinks[$n])) - { - //Links - $annots='/Annots ['; - foreach($this->PageLinks[$n] as $pl) - { - $rect=sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]); - $annots.='<_textstring($pl[4]).'>>>>'; - else - { - $l=$this->links[$pl[4]]; - if(isset($this->PageInfo[$l[0]]['size'])) - $h=$this->PageInfo[$l[0]]['size'][1]; - else - $h=$hpt; - $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',$this->PageInfo[$l[0]]['n'],$h-$l[1]*$this->k); - } - } - $this->_put($annots.']'); - } - if($this->WithAlpha) - $this->_put('/Group <>'); - $this->_put('/Contents '.(2+$n).' 0 R>>'); - $this->_put('endobj'); - } - //Pages root - $this->offsets[1]=$this->_getoffset(); - $this->_put('1 0 obj'); - $this->_put('<_put($kids.']'); - $this->_put('/Count '.$nb); - $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt)); - $this->_put('>>'); - $this->_put('endobj'); - } + function _putpages(){ + $nb=$this->page; + for($n=1;$n<=$nb;$n++) + $this->PageInfo[$n]['n']=$this->n+$n; + if($this->DefOrientation=='P') + { + $wPt=$this->DefPageSize[0]*$this->k; + $hPt=$this->DefPageSize[1]*$this->k; + } + else + { + $wPt=$this->DefPageSize[1]*$this->k; + $hPt=$this->DefPageSize[0]*$this->k; + } + //Page objects + for($n=1;$n<=$nb;$n++) + { + $this->_newobj(); + $this->_put('<_put('/Parent 1 0 R'); + if(isset($this->PageInfo[$n]['size'])) + $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageInfo[$n]['size'][0],$this->PageInfo[$n]['size'][1])); + if(isset($this->PageInfo[$n]['rotation'])) + $this->_put('/Rotate '.$this->PageInfo[$n]['rotation']); + $this->_put('/Resources 2 0 R'); + if(isset($this->PageLinks[$n])) + { + //Links + $annots='/Annots ['; + foreach($this->PageLinks[$n] as $pl) + { + $rect=sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]); + $annots.='<_textstring($pl[4]).'>>>>'; + else + { + $l=$this->links[$pl[4]]; + if(isset($this->PageInfo[$l[0]]['size'])) + $h=$this->PageInfo[$l[0]]['size'][1]; + else + $h=$hpt; + $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',$this->PageInfo[$l[0]]['n'],$h-$l[1]*$this->k); + } + } + $this->_put($annots.']'); + } + if($this->WithAlpha) + $this->_put('/Group <>'); + $this->_put('/Contents '.(2+$n).' 0 R>>'); + $this->_put('endobj'); + } + //Pages root + $this->offsets[1]=$this->_getoffset(); + $this->_put('1 0 obj'); + $this->_put('<_put($kids.']'); + $this->_put('/Count '.$nb); + $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt)); + $this->_put('>>'); + $this->_put('endobj'); + } - function _putheader(){ - if($this->_getoffset()==0) - parent::_putheader(); - } + function _putheader(){ + if($this->_getoffset()==0) + parent::_putheader(); + } - function _enddoc(){ - parent::_enddoc(); - fclose($this->f); - } + function _enddoc(){ + parent::_enddoc(); + fclose($this->f); + } } From ca7b3528790db3ba6d13022d6aea2c7f989fe98e Mon Sep 17 00:00:00 2001 From: Diego Ponciano Date: Fri, 8 Jul 2022 15:49:57 -0600 Subject: [PATCH 4/5] - Fix config --- src/config/fpdf.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/fpdf.php b/src/config/fpdf.php index 7487ed5..dae894c 100644 --- a/src/config/fpdf.php +++ b/src/config/fpdf.php @@ -15,7 +15,7 @@ 'unit' => 'mm', 'size' => 'A4', 'fontpath' => env('FPDF_FONTPATH', false), - 'optimize' => env('FPDF_OPTIMIZE', false) + 'optimize' => env('FPDF_OPTIMIZE', false), /* |-------------------------------------------------------------------------- From b4ff89f464dc1e9ead8c8590c3744ea4ff88ff72 Mon Sep 17 00:00:00 2001 From: Diego Ponciano Date: Fri, 8 Jul 2022 16:04:56 -0600 Subject: [PATCH 5/5] - Fixes styles --- src/Extensions/FpdfOptimize.php | 166 +++++++++++++++++--------------- 1 file changed, 91 insertions(+), 75 deletions(-) diff --git a/src/Extensions/FpdfOptimize.php b/src/Extensions/FpdfOptimize.php index 89fa1b7..89c7467 100644 --- a/src/Extensions/FpdfOptimize.php +++ b/src/Extensions/FpdfOptimize.php @@ -1,141 +1,157 @@ -Error('Version 1.8 or above is required by this extension'); - $this->f=fopen($file,'wb'); - if(!$this->f) + } + $this->f = fopen($file, 'wb'); + if (! $this->f) { $this->Error('Unable to create output file: '.$file); + } $this->_putheader(); } - function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link=''){ - if(!isset($this->images[$file])){ + public function Image($file, $x = null, $y = null, $w = 0, $h = 0, $type = '', $link = '') + { + if (! isset($this->images[$file])) { //Retrieve only meta-information - $a=getimagesize($file); - if($a===false) + $a = getimagesize($file); + if ($a === false) { $this->Error('Missing or incorrect image file: '.$file); - $this->images[$file]=array('w'=>$a[0],'h'=>$a[1],'type'=>$a[2],'i'=>count($this->images)+1); + } + $this->images[$file] = ['w'=>$a[0], 'h'=>$a[1], 'type'=>$a[2], 'i'=>count($this->images) + 1]; } - parent::Image($file,$x,$y,$w,$h,$type,$link); + parent::Image($file, $x, $y, $w, $h, $type, $link); } - function Output($dest='', $name='', $isUTF8=false){ - if($this->state<3) + public function Output($dest = '', $name = '', $isUTF8 = false) + { + if ($this->state < 3) { $this->Close(); + } } - function _endpage(){ + public function _endpage() + { parent::_endpage(); //Write page to file $this->_putstreamobject($this->pages[$this->page]); unset($this->pages[$this->page]); } - function _getoffset(){ + public function _getoffset() + { return ftell($this->f); } - function _put($s){ - fwrite($this->f,$s."\n",strlen($s)+1); + public function _put($s) + { + fwrite($this->f, $s."\n", strlen($s) + 1); } - function _putimages(){ - foreach(array_keys($this->images) as $file) - { - $type=$this->images[$file]['type']; - if($type==1) - $info=$this->_parsegif($file); - elseif($type==2) - $info=$this->_parsejpg($file); - elseif($type==3) - $info=$this->_parsepng($file); - else + public function _putimages() + { + foreach (array_keys($this->images) as $file) { + $type = $this->images[$file]['type']; + if ($type == 1) { + $info = $this->_parsegif($file); + } elseif ($type == 2) { + $info = $this->_parsejpg($file); + } elseif ($type == 3) { + $info = $this->_parsepng($file); + } else { $this->Error('Unsupported image type: '.$file); + } $this->_putimage($info); - $this->images[$file]['n']=$info['n']; + $this->images[$file]['n'] = $info['n']; unset($info); } } - function _putpages(){ - $nb=$this->page; - for($n=1;$n<=$nb;$n++) - $this->PageInfo[$n]['n']=$this->n+$n; - if($this->DefOrientation=='P') - { - $wPt=$this->DefPageSize[0]*$this->k; - $hPt=$this->DefPageSize[1]*$this->k; + public function _putpages() + { + $nb = $this->page; + for ($n = 1; $n <= $nb; $n++) { + $this->PageInfo[$n]['n'] = $this->n + $n; } - else - { - $wPt=$this->DefPageSize[1]*$this->k; - $hPt=$this->DefPageSize[0]*$this->k; + if ($this->DefOrientation == 'P') { + $wPt = $this->DefPageSize[0] * $this->k; + $hPt = $this->DefPageSize[1] * $this->k; + } else { + $wPt = $this->DefPageSize[1] * $this->k; + $hPt = $this->DefPageSize[0] * $this->k; } //Page objects - for($n=1;$n<=$nb;$n++) - { + for ($n = 1; $n <= $nb; $n++) { $this->_newobj(); $this->_put('<_put('/Parent 1 0 R'); - if(isset($this->PageInfo[$n]['size'])) - $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageInfo[$n]['size'][0],$this->PageInfo[$n]['size'][1])); - if(isset($this->PageInfo[$n]['rotation'])) + if (isset($this->PageInfo[$n]['size'])) { + $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]', $this->PageInfo[$n]['size'][0], $this->PageInfo[$n]['size'][1])); + } + if (isset($this->PageInfo[$n]['rotation'])) { $this->_put('/Rotate '.$this->PageInfo[$n]['rotation']); + } $this->_put('/Resources 2 0 R'); - if(isset($this->PageLinks[$n])) - { + if (isset($this->PageLinks[$n])) { //Links - $annots='/Annots ['; - foreach($this->PageLinks[$n] as $pl) - { - $rect=sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]); - $annots.='<_textstring($pl[4]).'>>>>'; - else - { - $l=$this->links[$pl[4]]; - if(isset($this->PageInfo[$l[0]]['size'])) - $h=$this->PageInfo[$l[0]]['size'][1]; - else - $h=$hpt; - $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',$this->PageInfo[$l[0]]['n'],$h-$l[1]*$this->k); + $annots = '/Annots ['; + foreach ($this->PageLinks[$n] as $pl) { + $rect = sprintf('%.2F %.2F %.2F %.2F', $pl[0], $pl[1], $pl[0] + $pl[2], $pl[1] - $pl[3]); + $annots .= '<_textstring($pl[4]).'>>>>'; + } else { + $l = $this->links[$pl[4]]; + if (isset($this->PageInfo[$l[0]]['size'])) { + $h = $this->PageInfo[$l[0]]['size'][1]; + } else { + $h = $hpt; + } + $annots .= sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>', $this->PageInfo[$l[0]]['n'], $h - $l[1] * $this->k); } } $this->_put($annots.']'); } - if($this->WithAlpha) + if ($this->WithAlpha) { $this->_put('/Group <>'); - $this->_put('/Contents '.(2+$n).' 0 R>>'); + } + $this->_put('/Contents '.(2 + $n).' 0 R>>'); $this->_put('endobj'); } //Pages root - $this->offsets[1]=$this->_getoffset(); + $this->offsets[1] = $this->_getoffset(); $this->_put('1 0 obj'); $this->_put('<_put($kids.']'); $this->_put('/Count '.$nb); - $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt)); + $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]', $wPt, $hPt)); $this->_put('>>'); $this->_put('endobj'); } - function _putheader(){ - if($this->_getoffset()==0) + public function _putheader() + { + if ($this->_getoffset() == 0) { parent::_putheader(); + } } - function _enddoc(){ + public function _enddoc() + { parent::_enddoc(); fclose($this->f); }