Skip to content

Commit

Permalink
Mejorado el soporte de extensiones en la nueva clase PDF.
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoRazorX committed Nov 11, 2023
1 parent d33f022 commit 4da07f0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
64 changes: 55 additions & 9 deletions Core/Lib/Export/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public function __construct(string $size = 'a4', string $orientation = 'portrait
$this->orientation = $orientation;
$this->size = $size;
$this->title = 'DOC ' . date('YmdHis') . '_' . rand(1, 9999);

$this->pipeFalse('init', $size, $orientation);
}

public function addCompanyHeader(int $idempresa): void
Expand All @@ -79,6 +81,10 @@ public function addCompanyHeader(int $idempresa): void
return;
}

if (false === $this->pipeFalse('addCompanyHeaderBefore', $idempresa)) {
return;
}

$company = Empresas::get($idempresa);

// si la empresa tiene logo, lo añadimos
Expand Down Expand Up @@ -112,6 +118,8 @@ public function addCompanyHeader(int $idempresa): void
}

$this->addText("\n");

$this->pipeFalse('addCompanyHeaderAfter', $idempresa);
}

public function addHtml(string $html): self
Expand All @@ -120,27 +128,33 @@ public function addHtml(string $html): self
return $this;
}

if (false === $this->pipeFalse('addHtml', $html)) {
if (false === $this->pipeFalse('addHtmlBefore', $html)) {
return $this;
}

// no soportado

$this->pipeFalse('addHtmlAfter', $html);

return $this;
}

public function addImage(string $filePath): self
{
if (false === $this->pipeFalse('addImage', $filePath)) {
if (false === $this->pipeFalse('addImageBefore', $filePath)) {
return $this;
}

$this->pdf->Image($filePath);

$this->pipeFalse('addImageAfter', $filePath);

return $this;
}

public function addModel(ModelClass $model, array $options = []): self
{
if (false === $this->pipeFalse('addModel', $model, $options)) {
if (false === $this->pipeFalse('addModelBefore', $model, $options)) {
return $this;
}

Expand Down Expand Up @@ -170,6 +184,8 @@ public function addModel(ModelClass $model, array $options = []): self
break;
}

$this->pipeFalse('addModelAfter', $model, $options);

return $this;
}

Expand All @@ -179,12 +195,22 @@ public function addModelList(array $list, array $header = [], array $options = [
return $this;
}

if (false === $this->pipeFalse('addModelList', $list, $header, $options)) {
if (false === $this->pipeFalse('addModelListBefore', $list, $header, $options)) {
return $this;
}

$this->checkOptions($options);

// convertimos los datos en array
$rows = [];
foreach ($list as $model) {
$rows[] = $model->toArray();
}

$this->addTable($rows, $header, $options);

$this->pipeFalse('addModelListAfter', $list, $header, $options);

return $this;
}

Expand All @@ -194,7 +220,7 @@ public function addTable(array $rows, array $header = [], array $options = []):
return $this;
}

if (false === $this->pipeFalse('addTable', $rows, $header, $options)) {
if (false === $this->pipeFalse('addTableBefore', $rows, $header, $options)) {
return $this;
}

Expand Down Expand Up @@ -236,6 +262,10 @@ public function addTable(array $rows, array $header = [], array $options = []):
$this->pdf->Ln(); // Salta a la siguiente línea
}

$this->pdf->Ln(); // Salta a la siguiente línea

$this->pipeFalse('addTableAfter', $rows, $header, $options);

return $this;
}

Expand All @@ -245,7 +275,7 @@ public function addText(string $text, array $options = []): self
return $this;
}

if (false === $this->pipeFalse('addText', $text, $options)) {
if (false === $this->pipeFalse('addTextBefore', $text, $options)) {
return $this;
}

Expand All @@ -264,6 +294,8 @@ public function addText(string $text, array $options = []): self
// volvemos al tamaño de fuente por defecto
$this->pdf->SetFont($this->font_family, $this->font_weight, $this->font_size);

$this->pipeFalse('addTextAfter', $text, $options);

return $this;
}

Expand All @@ -274,7 +306,7 @@ public static function create(string $size = 'a4', string $orientation = 'portra

public function newPage(): self
{
if (false === $this->pipeFalse('newPage')) {
if (false === $this->pipeFalse('newPageBefore')) {
return $this;
}

Expand All @@ -285,8 +317,7 @@ public function newPage(): self
$this->pdf->SetCreator('FacturaScripts');

// añadimos las fuentes
$this->pdf->AddFont($this->font_family, '', 'DejaVuSans.ttf', true);
$this->pdf->AddFont($this->font_family, 'B', 'DejaVuSans-Bold.ttf', true);
$this->loadFonts();

// establece la fuente por defecto
$this->pdf->SetFont($this->font_family, $this->font_weight, $this->font_size);
Expand All @@ -296,6 +327,9 @@ public function newPage(): self
}

$this->pdf->AddPage($this->orientation, $this->size);

$this->pipeFalse('newPageAfter');

return $this;
}

Expand Down Expand Up @@ -527,4 +561,16 @@ protected function checkOptions(array &$options): void
break;
}
}

protected function loadFonts(): void
{
if (false === $this->pipeFalse('loadFontsBefore')) {
return;
}

$this->pdf->AddFont($this->font_family, '', 'DejaVuSans.ttf', true);
$this->pdf->AddFont($this->font_family, 'B', 'DejaVuSans-Bold.ttf', true);

$this->pipeFalse('loadFontsAfter');
}
}
2 changes: 2 additions & 0 deletions Core/Template/PdfEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ abstract public function addText(string $text, array $options = []);

abstract public static function create(string $size = 'a4', string $orientation = 'portrait');

abstract protected function loadFonts(): void;

abstract public function newPage();

abstract public function output(): string;
Expand Down

0 comments on commit 4da07f0

Please sign in to comment.