Skip to content

Commit

Permalink
Add ability to define default page context like header/footer
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Aug 26, 2024
1 parent 4cd2891 commit 58de3e1
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 93 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8.0.74
8.0.75
32 changes: 17 additions & 15 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

$pdf->setViewerPreferences(['DisplayDocTitle' => true]);

$pdf->enableDefaultPageContent();

// ----------
// Insert fonts

Expand All @@ -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
Expand Down Expand Up @@ -114,7 +116,7 @@
// ----------
// Add second page

$page02 = $pdf->page->add();
$page02 = $pdf->addPage();
$pdf->setBookmark('Graphics', '', 0, -1, 0, 0, 'B', 'green');

$style1 = [
Expand Down Expand Up @@ -409,7 +411,7 @@
// ----------
// Add page 2

$page03 = $pdf->page->add();
$page03 = $pdf->addPage();
$pdf->setBookmark('Ellipse', '', 1);

$pdf->graph->setPageWidth($page03['width']);
Expand Down Expand Up @@ -443,7 +445,7 @@
// ----------
// Add page 4

$page04 = $pdf->page->add();
$page04 = $pdf->addPage();
$pdf->setBookmark('Pie Chart', '', 1);

$pdf->graph->setPageWidth($page04['width']);
Expand All @@ -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']);
Expand Down Expand Up @@ -678,7 +680,7 @@
// ----------
// Add page 6

$page06 = $pdf->page->add();
$page06 = $pdf->addPage();
$pdf->setBookmark('Color Gradients', '', 1);

$pdf->graph->setPageWidth($page06['width']);
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -827,7 +829,7 @@
// ----------
// Add page 8

$page08 = $pdf->page->add();
$page08 = $pdf->addPage();
$pdf->setBookmark('Transformations', '', 1);

$pdf->graph->setPageWidth($page08['width']);
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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']);
Expand All @@ -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
Expand Down Expand Up @@ -1469,7 +1471,7 @@

// Page signature

$pageC01 = $pdf->page->add();
$pageC01 = $pdf->addPage();
$pdf->setBookmark('Signature', '', 0, -1, 0, 0, 'B', 'red');

/*
Expand Down Expand Up @@ -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, []);
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -1604,7 +1606,7 @@

// ----------

$pageTOC = $pdf->page->add();
$pageTOC = $pdf->addPage();
$pdf->setBookmark('TOC');

$pdf->page->addContent($bfont4['out']);
Expand Down
2 changes: 1 addition & 1 deletion resources/debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion resources/rpm/rpm.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
95 changes: 93 additions & 2 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<int, TBBox>
*
* @phpstan-import-type TAnnot from Output
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
74 changes: 1 addition & 73 deletions src/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
Loading

0 comments on commit 58de3e1

Please sign in to comment.