From 58de3e1df3234506335ceda51571b7dbf1f6b0a1 Mon Sep 17 00:00:00 2001 From: nicolaasuni Date: Mon, 26 Aug 2024 15:55:46 +0100 Subject: [PATCH] Add ability to define default page context like header/footer --- VERSION | 2 +- examples/index.php | 32 +++++++------- resources/debian/control | 2 +- resources/rpm/rpm.spec | 2 +- src/Base.php | 95 +++++++++++++++++++++++++++++++++++++++- src/Cell.php | 74 +------------------------------ src/Tcpdf.php | 3 ++ src/Text.php | 77 ++++++++++++++++++++++++++++++++ 8 files changed, 194 insertions(+), 93 deletions(-) diff --git a/VERSION b/VERSION index eea5d72..aca743a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -8.0.74 +8.0.75 diff --git a/examples/index.php b/examples/index.php index 7e82c22..92f4e89 100644 --- a/examples/index.php +++ b/examples/index.php @@ -49,6 +49,8 @@ $pdf->setViewerPreferences(['DisplayDocTitle' => true]); +$pdf->enableDefaultPageContent(); + // ---------- // Insert fonts @@ -58,7 +60,7 @@ // ---------- // Add first page -$page01 = $pdf->page->add(); +$page01 = $pdf->addPage(); $pdf->setBookmark('Images', '', 0, -1, 0, 0, 'B', 'blue'); // Add Images @@ -114,7 +116,7 @@ // ---------- // Add second page -$page02 = $pdf->page->add(); +$page02 = $pdf->addPage(); $pdf->setBookmark('Graphics', '', 0, -1, 0, 0, 'B', 'green'); $style1 = [ @@ -409,7 +411,7 @@ // ---------- // Add page 2 -$page03 = $pdf->page->add(); +$page03 = $pdf->addPage(); $pdf->setBookmark('Ellipse', '', 1); $pdf->graph->setPageWidth($page03['width']); @@ -443,7 +445,7 @@ // ---------- // Add page 4 -$page04 = $pdf->page->add(); +$page04 = $pdf->addPage(); $pdf->setBookmark('Pie Chart', '', 1); $pdf->graph->setPageWidth($page04['width']); @@ -466,7 +468,7 @@ // ---------- // Add page 5 -$page05 = $pdf->page->add(); +$page05 = $pdf->addPage(); $pdf->setBookmark('Crop Marks and Color Maps', '', 1); $pdf->graph->setPageWidth($page05['width']); @@ -678,7 +680,7 @@ // ---------- // Add page 6 -$page06 = $pdf->page->add(); +$page06 = $pdf->addPage(); $pdf->setBookmark('Color Gradients', '', 1); $pdf->graph->setPageWidth($page06['width']); @@ -739,7 +741,7 @@ // ---------- // Add page 7 -$page07 = $pdf->page->add(); +$page07 = $pdf->addPage(); $pdf->setBookmark('Color gradient mesh', '', 1); $pdf->graph->setPageWidth($page07['width']); @@ -827,7 +829,7 @@ // ---------- // Add page 8 -$page08 = $pdf->page->add(); +$page08 = $pdf->addPage(); $pdf->setBookmark('Transformations', '', 1); $pdf->graph->setPageWidth($page08['width']); @@ -955,7 +957,7 @@ // ---------- // Add page 9 -$page09 = $pdf->page->add(); +$page09 = $pdf->addPage(); $pdf->setBookmark('Barcodes', '', 0, -1, 0, 0, 'B', ''); $dest_barcode_page = $pdf->setNamedDestination('barcode'); @@ -1003,7 +1005,7 @@ // ---------- // Add page 10 -$page10 = $pdf->page->add(); +$page10 = $pdf->addPage(); $pdf->setBookmark('Image Clipping', '', 0, -1, 0, 0, 'B', ''); $pdf->graph->setPageWidth($page10['width']); @@ -1023,7 +1025,7 @@ // ---------- // Add page 11 -$page11 = $pdf->page->add(); +$page11 = $pdf->addPage(); $pdf->setBookmark('Text', '', 0, -1, 0, 0, 'B', ''); // Add an internal link to this page @@ -1469,7 +1471,7 @@ // Page signature -$pageC01 = $pdf->page->add(); +$pageC01 = $pdf->addPage(); $pdf->setBookmark('Signature', '', 0, -1, 0, 0, 'B', 'red'); /* @@ -1521,7 +1523,7 @@ // XOBject template -$pageC02 = $pdf->page->add(); +$pageC02 = $pdf->addPage(); $pdf->setBookmark('XOBject Template', '', 0, -1, 0, 0, 'B', ''); $tid = $pdf->newXObjectTemplate(80, 80, []); @@ -1551,7 +1553,7 @@ // Layers -$pageV01 = $pdf->page->add(); +$pageV01 = $pdf->addPage(); $pdf->setBookmark('Layers', '', 0, -1, 0, 0, 'B', ''); $pdf->page->addContent($bfont4['out']); @@ -1604,7 +1606,7 @@ // ---------- -$pageTOC = $pdf->page->add(); +$pageTOC = $pdf->addPage(); $pdf->setBookmark('TOC'); $pdf->page->addContent($bfont4['out']); diff --git a/resources/debian/control b/resources/debian/control index caeeb72..d8c4335 100644 --- a/resources/debian/control +++ b/resources/debian/control @@ -10,6 +10,6 @@ Vcs-Git: https://github.com/~#VENDOR#~/~#PROJECT#~.git Package: ~#PKGNAME#~ Provides: php-~#PROJECT#~ Architecture: all -Depends: php (>= 8.0.0), php-date, php-tecnickcom-tc-lib-barcode (<< 3.0.0), php-tecnickcom-tc-lib-barcode (>= 2.2.3), php-tecnickcom-tc-lib-color (<< 3.0.0), php-tecnickcom-tc-lib-color (>= 2.2.2), php-tecnickcom-tc-lib-pdf-image (<< 3.0.0), php-tecnickcom-tc-lib-pdf-image (>= 2.1.1), php-tecnickcom-tc-lib-pdf-font (<< 3.0.0), php-tecnickcom-tc-lib-pdf-font (>= 2.5.0), php-tecnickcom-tc-lib-file (<< 3.0.0), php-tecnickcom-tc-lib-file (>= 2.0.13), php-tecnickcom-tc-lib-pdf-encrypt (<< 3.0.0), php-tecnickcom-tc-lib-pdf-encrypt (>= 2.1.4), php-tecnickcom-tc-lib-unicode-data (<< 3.0.0), php-tecnickcom-tc-lib-unicode-data (>= 2.0.13), php-tecnickcom-tc-lib-unicode (<< 3.0.0), php-tecnickcom-tc-lib-unicode (>= 2.0.13), php-tecnickcom-tc-lib-pdf-page (<< 5.0.0), php-tecnickcom-tc-lib-pdf-page (>= 4.1.0), php-tecnickcom-tc-lib-pdf-graph (<< 3.0.0), php-tecnickcom-tc-lib-pdf-graph (>= 2.1.1), ${misc:Depends} +Depends: php (>= 8.0.0), php-date, php-tecnickcom-tc-lib-barcode (<< 3.0.0), php-tecnickcom-tc-lib-barcode (>= 2.2.3), php-tecnickcom-tc-lib-color (<< 3.0.0), php-tecnickcom-tc-lib-color (>= 2.2.2), php-tecnickcom-tc-lib-pdf-image (<< 3.0.0), php-tecnickcom-tc-lib-pdf-image (>= 2.1.1), php-tecnickcom-tc-lib-pdf-font (<< 3.0.0), php-tecnickcom-tc-lib-pdf-font (>= 2.5.0), php-tecnickcom-tc-lib-file (<< 3.0.0), php-tecnickcom-tc-lib-file (>= 2.0.13), php-tecnickcom-tc-lib-pdf-encrypt (<< 3.0.0), php-tecnickcom-tc-lib-pdf-encrypt (>= 2.1.4), php-tecnickcom-tc-lib-unicode-data (<< 3.0.0), php-tecnickcom-tc-lib-unicode-data (>= 2.0.13), php-tecnickcom-tc-lib-unicode (<< 3.0.0), php-tecnickcom-tc-lib-unicode (>= 2.0.13), php-tecnickcom-tc-lib-pdf-page (<< 5.0.0), php-tecnickcom-tc-lib-pdf-page (>= 4.1.1), php-tecnickcom-tc-lib-pdf-graph (<< 3.0.0), php-tecnickcom-tc-lib-pdf-graph (>= 2.1.1), ${misc:Depends} Description: PHP Barcode library This library includes PHP classes to generate PDF documents. diff --git a/resources/rpm/rpm.spec b/resources/rpm/rpm.spec index 0cc5135..213b863 100644 --- a/resources/rpm/rpm.spec +++ b/resources/rpm/rpm.spec @@ -36,7 +36,7 @@ Requires: php-composer(%{c_vendor}/tc-lib-unicode-data) >= 2.0.13 Requires: php-composer(%{c_vendor}/tc-lib-unicode) < 3.0.0 Requires: php-composer(%{c_vendor}/tc-lib-unicode) >= 2.0.13 Requires: php-composer(%{c_vendor}/tc-lib-pdf-page) < 5.0.0 -Requires: php-composer(%{c_vendor}/tc-lib-pdf-page) >= 4.1.0 +Requires: php-composer(%{c_vendor}/tc-lib-pdf-page) >= 4.1.1 Requires: php-composer(%{c_vendor}/tc-lib-pdf-graph) < 3.0.0 Requires: php-composer(%{c_vendor}/tc-lib-pdf-graph) >= 2.1.1 diff --git a/src/Base.php b/src/Base.php index d9a5b82..1218d82 100644 --- a/src/Base.php +++ b/src/Base.php @@ -40,6 +40,10 @@ * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) * @link https://github.com/tecnickcom/tc-lib-pdf * + * @phpstan-import-type PageInputData from \Com\Tecnick\Pdf\Page\Box + * @phpstan-import-type PageData from \Com\Tecnick\Pdf\Page\Box + * @phpstan-import-type TFontMetric from \Com\Tecnick\Pdf\Font\Stack + * * @phpstan-type TViewerPref array{ * 'HideToolbar'?: bool, * 'HideMenubar'?: bool, @@ -67,6 +71,22 @@ * 'h': float, * } * + * @phpstan-type TCellDef array{ + * 'margin': array{ + * 'T': float, + * 'R': float, + * 'B': float, + * 'L': float, + * }, + * 'padding': array{ + * 'T': float, + * 'R': float, + * 'B': float, + * 'L': float, + * }, + * 'borderpos': float, + * } + * * @phpstan-type TStackBBox array * * @phpstan-import-type TAnnot from Output @@ -136,7 +156,7 @@ abstract class Base /** * TCPDF version. */ - protected string $version = '8.0.74'; + protected string $version = '8.0.75'; /** * Time is seconds since EPOCH when the document was created. @@ -453,13 +473,72 @@ abstract class Base * * @var TStackBBox */ - protected $bbox = [[ + protected array $bbox = [[ 'x' => 0, 'y' => 0, 'w' => 0, 'h' => 0, ]]; + /** + * Set to true to enable the default page footer. + * + * @var bool + */ + protected bool $defPageContentEnabled = false; + + /** + * Default font for defautl page content. + * + * @var ?TFontMetric + */ + protected ?array $defaultfont = null; + /** + * The default relative position of the cell origin when + * the border is centered on the cell edge. + */ + public const BORDERPOS_DEFAULT = 0; + + /** + * The relative position of the cell origin when + * the border is external to the cell edge. + */ + public const BORDERPOS_EXTERNAL = -0.5; //-1/2 + + /** + * The relative position of the cell origin when + * the border is internal to the cell edge. + */ + public const BORDERPOS_INTERNAL = 0.5; // 1/2 + + /** + * Default values for cell. + * + * @const TCellDef + */ + public const ZEROCELL = [ + 'margin' => [ + 'T' => 0, + 'R' => 0, + 'B' => 0, + 'L' => 0, + ], + 'padding' => [ + 'T' => 0, + 'R' => 0, + 'B' => 0, + 'L' => 0, + ], + 'borderpos' => self::BORDERPOS_DEFAULT, + ]; + + /** + * Default values for cell. + * + * @var TCellDef + */ + protected $defcell = self::ZEROCELL; + /** * Convert user units to internal points unit. * @@ -505,4 +584,16 @@ public function toYUnit(float $pnt, float $pageh = -1): float $pageh = $pageh >= 0 ? $pageh : $this->page->getPage()['pheight']; return $this->toUnit($pageh - $pnt); } + + /** + * Enable or disable the default page content. + * + * @param bool $enable Enable or disable the default page content. + * + * @return void + */ + public function enableDefaultPageContent(bool $enable = true): void + { + $this->defPageContentEnabled = $enable; + } } diff --git a/src/Cell.php b/src/Cell.php index 1ab738e..d5f1319 100644 --- a/src/Cell.php +++ b/src/Cell.php @@ -30,71 +30,11 @@ * @link https://github.com/tecnickcom/tc-lib-pdf * * @phpstan-import-type StyleDataOpt from \Com\Tecnick\Pdf\Graph\Style + * @phpstan-import-type TCellDef from \Com\Tecnick\Pdf\Base * - * @phpstan-type TCellDef array{ - * 'margin': array{ - * 'T': float, - * 'R': float, - * 'B': float, - * 'L': float, - * }, - * 'padding': array{ - * 'T': float, - * 'R': float, - * 'B': float, - * 'L': float, - * }, - * 'borderpos': float, - * } */ abstract class Cell extends \Com\Tecnick\Pdf\Base { - /** - * The default relative position of the cell origin when - * the border is centered on the cell edge. - */ - public const BORDERPOS_DEFAULT = 0; - - /** - * The relative position of the cell origin when - * the border is external to the cell edge. - */ - public const BORDERPOS_EXTERNAL = -0.5; //-1/2 - - /** - * The relative position of the cell origin when - * the border is internal to the cell edge. - */ - public const BORDERPOS_INTERNAL = 0.5; // 1/2 - - /** - * Default values for cell. - * - * @const TCellDef - */ - public const ZEROCELL = [ - 'margin' => [ - 'T' => 0, - 'R' => 0, - 'B' => 0, - 'L' => 0, - ], - 'padding' => [ - 'T' => 0, - 'R' => 0, - 'B' => 0, - 'L' => 0, - ], - 'borderpos' => self::BORDERPOS_DEFAULT, - ]; - - /** - * Default values for cell. - * - * @var TCellDef - */ - protected $defcell = self::ZEROCELL; - /** * Set the default cell margin in user units. * @@ -604,18 +544,6 @@ protected function textMaxHeight( } } - /** - * Sets the page context by adding the previous page font and graphic settings. - * - * @param int $pid Page index. Omit or set it to -1 for the current page ID. - * - * @return void - */ - protected function setPageContext(int $pid = -1): void - { - $this->page->addContent($this->font->getOutCurrentFont(), $pid); - } - /** * Returns the PDF code to draw the text cell border and background. * diff --git a/src/Tcpdf.php b/src/Tcpdf.php index 64d2abc..b9d1645 100644 --- a/src/Tcpdf.php +++ b/src/Tcpdf.php @@ -35,6 +35,8 @@ * * @phpstan-import-type StyleDataOpt from \Com\Tecnick\Pdf\Graph\Base * @phpstan-import-type PageData from \Com\Tecnick\Pdf\Page\Box + * @phpstan-import-type PageInputData from \Com\Tecnick\Pdf\Page\Box + * @phpstan-import-type TFontMetric from \Com\Tecnick\Pdf\Font\Stack * * @phpstan-import-type TAnnotOpts from Output * @phpstan-import-type TSignature from Output @@ -1124,6 +1126,7 @@ public function addTOC( if (($posy + $cellSpaceT + $cellSpaceB + $font['height']) > $region['RH']) { $opage = $this->page->getNextRegion($pid); + $this->setPageContext($pid); $pid = $opage['pid']; $region = $this->page->getRegion($pid); $posy = 0; // $region['RY']; diff --git a/src/Text.php b/src/Text.php index b3cba8f..61472a1 100644 --- a/src/Text.php +++ b/src/Text.php @@ -37,6 +37,10 @@ * @phpstan-import-type StyleDataOpt from \Com\Tecnick\Pdf\Cell * @phpstan-import-type TCellDef from \Com\Tecnick\Pdf\Cell * + * @phpstan-import-type PageInputData from \Com\Tecnick\Pdf\Page\Box + * @phpstan-import-type PageData from \Com\Tecnick\Pdf\Page\Box + * @phpstan-import-type TFontMetric from \Com\Tecnick\Pdf\Font\Stack + * * * @phpstan-import-type TBBox from \Com\Tecnick\Pdf\Base * @phpstan-import-type TStackBBox from \Com\Tecnick\Pdf\Base @@ -1565,4 +1569,77 @@ protected function hyphenateWordOrdArr( return $ordarr; } + + // ===| PAGE |========================================================== + + /** + * Add a new page (wrapper function for $this->page->add()). + * + * @param PageInputData $data Page data. + * @return PageData Page data with additional Page ID property 'pid'. + */ + public function addPage(array $data = []): array + { + $ret = $this->page->add($data); + $this->setPageContext($ret['pid']); + return $ret; + } + + /** + * Sets the page context by adding the previous page font and graphic settings. + * + * @param int $pid Page index. Omit or set it to -1 for the current page ID. + * + * @return void + */ + protected function setPageContext(int $pid = -1): void + { + $this->page->addContent($this->font->getOutCurrentFont(), $pid); + if ($this->defPageContentEnabled) { + $this->page->addContent($this->defaultPageContent($pid), $pid); + } + } + + /** + * Sets the page common content like Header and Footer. + * Override this method to add custom content to all pages. + * + * @param int $pid Page index. Omit or set it to -1 for the current page ID. + * + * @return string PDF output code. + */ + public function defaultPageContent(int $pid = -1): string + { + if ($pid < 0) { + $pid = $this->page->getPageId(); + } + + if ($this->defaultfont === null) { + $this->defaultfont = $this->font->insert($this->pon, 'helvetica', '', 10); + } + + $region = $this->page->getRegion($pid); + + // print page number in the footer + $out = $this->graph->getStartTransform(); + $out .= $this->defaultfont['out']; + $out .= $this->color->getPdfColor('black'); + $prevcell = $this->defcell; + $this->defcell = $this::ZEROCELL; + + $out .= $this->getTextCell( + (string) ($pid + 1), + $this->toUnit($this->defaultfont['dw']), + $region['RH'] - (2 * $this->toUnit($this->defaultfont['height'])), + $region['RW'] - (2 * $this->toUnit($this->defaultfont['dw'])), + 0, + 0, + 0, + 'T', + ($this->rtl ? 'L' : 'R'), + ); + $out .= $this->graph->getStopTransform(); + $this->defcell = $prevcell; + return $out; + } }