Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/optimize #167

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions src/Extensions/FpdfOptimize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace Codedge\Fpdf\Extensions;

use Codedge\Fpdf\Fpdf\Fpdf;

class FpdfOptimize extends Fpdf
{
protected $f;

public 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();
}

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) {
$this->Error('Missing or incorrect image file: '.$file);
}
$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);
}

public function Output($dest = '', $name = '', $isUTF8 = false)
{
if ($this->state < 3) {
$this->Close();
}
}

public function _endpage()
{
parent::_endpage();
//Write page to file
$this->_putstreamobject($this->pages[$this->page]);
unset($this->pages[$this->page]);
}

public function _getoffset()
{
return ftell($this->f);
}

public function _put($s)
{
fwrite($this->f, $s."\n", strlen($s) + 1);
}

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'];
unset($info);
}
}

public 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('<</Type /Page');
$this->_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 .= '<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
if (is_string($pl[4])) {
$annots .= '/A <</S /URI /URI '.$this->_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 <</Type /Group /S /Transparency /CS /DeviceRGB>>');
}
$this->_put('/Contents '.(2 + $n).' 0 R>>');
$this->_put('endobj');
}
//Pages root
$this->offsets[1] = $this->_getoffset();
$this->_put('1 0 obj');
$this->_put('<</Type /Pages');
$kids = '/Kids [';
for ($n = 1; $n <= $nb; $n++) {
$kids .= (2 + $nb + $n).' 0 R ';
}
$this->_put($kids.']');
$this->_put('/Count '.$nb);
$this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]', $wPt, $hPt));
$this->_put('>>');
$this->_put('endobj');
}

public function _putheader()
{
if ($this->_getoffset() == 0) {
parent::_putheader();
}
}

public function _enddoc()
{
parent::_enddoc();
fclose($this->f);
}
}
26 changes: 20 additions & 6 deletions src/FpdfServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Codedge\Fpdf;

use Illuminate\Support\ServiceProvider;
use Codedge\Fpdf\Extensions\FpdfOptimize;

class FpdfServiceProvider extends ServiceProvider
{
Expand All @@ -18,6 +19,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');
Expand All @@ -43,16 +48,25 @@ public function register()
*/
public function registerFpdf()
{
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')
);
});
}
if(config('fpdf.font_path') !== null) {
define('FPDF_FONTPATH', config('fpdf.font_path'));
}

$this->app->singleton('fpdf', function()
{
return new Fpdf\Fpdf(
config('fpdf.orientation'), config('fpdf.unit'), config('fpdf.size')
);
});
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/config/fpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
'orientation' => 'P',
'unit' => 'mm',
'size' => 'A4',
'fontpath' => env('FPDF_FONTPATH', false),
'optimize' => env('FPDF_OPTIMIZE', false),
'font_path' => env('FPDF_FONTPATH'),

/*
Expand Down