Skip to content

Commit ad806ec

Browse files
authored
Merge pull request #1 from joaovdiasb/feat/page-counter-centered
feat: page counter centered option added
2 parents 481b34a + 679809d commit ad806ec

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

src/PdfManager.php

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class PdfManager
2525
$pageCounterX = null,
2626
$pageCounterY = null,
2727
$pageCounterFontSize = null;
28-
private bool $pageCounter = false;
28+
private bool $pageCounter = false,
29+
$pageCounterCentered = false;
2930
public const PDF_STREAM = 'pdf_stream',
3031
PDF_DOWNLOAD = 'pdf_download',
3132
PDF_CONTENT = 'pdf_content',
@@ -96,15 +97,34 @@ public function setPaper(string $pageSize, string $pageOrientation): self
9697
return $this;
9798
}
9899

99-
public function setPageCounter(?int $pageCounterX = null, ?int $pageCounterY = null): self
100+
public function setPageCounter(): self
101+
{
102+
$this->pageCounter = true;
103+
104+
return $this;
105+
}
106+
107+
public function setPageCounterX(float $pageCounterX): self
100108
{
101-
$this->pageCounter = true;
102109
$this->pageCounterX = $pageCounterX;
110+
111+
return $this;
112+
}
113+
114+
public function setPageCounterY(float $pageCounterY): self
115+
{
103116
$this->pageCounterY = $pageCounterY;
104117

105118
return $this;
106119
}
107120

121+
public function setPageCounterCentered(): self
122+
{
123+
$this->pageCounterCentered = true;
124+
125+
return $this;
126+
}
127+
108128
public function setPageCounterText(?string $pageCounterText = null): self
109129
{
110130
$this->pageCounterText = $pageCounterText;
@@ -218,8 +238,14 @@ private function getViewContent(): string
218238
private function insertPageCounter(\Barryvdh\DomPDF\PDF $pdf): void
219239
{
220240
$pdf->output();
221-
$domPdf = $pdf->getDomPDF();
222-
$canvas = $domPdf->getCanvas();
241+
$domPdf = $pdf->getDomPDF();
242+
$canvas = $domPdf->getCanvas();
243+
$fontMetrics = $canvas->get_dompdf()->getFontMetrics();
244+
245+
if ($this->pageCounterCentered) {
246+
$this->pageCounterCentered($pdf, $fontMetrics, $canvas->get_width());
247+
}
248+
223249
$canvas->page_text(
224250
$this->getPageCounterX(),
225251
$this->getPageCounterY(),
@@ -229,6 +255,17 @@ private function insertPageCounter(\Barryvdh\DomPDF\PDF $pdf): void
229255
[0, 0, 0]);
230256
}
231257

258+
private function pageCounterCentered(\Barryvdh\DomPDF\PDF $pdf, \Dompdf\FontMetrics $fontMetrics, float $pdfWidth): void
259+
{
260+
$textWidth = $fontMetrics->getTextWidth(
261+
str_replace(['{PAGE_COUNT}', '{PAGE_NUM}'], '', $this->getPageCounterText()),
262+
null,
263+
$this->getPageCounterFontSize()
264+
);
265+
266+
$this->pageCounterX = ($pdfWidth - $textWidth) / 2;
267+
}
268+
232269
private function parseView(string $view): \Barryvdh\DomPDF\PDF
233270
{
234271
$pdf = Pdf::loadHTML($view)
@@ -253,4 +290,4 @@ private function replaces(?string $text): ?string
253290
$text ?? ''
254291
);
255292
}
256-
}
293+
}

0 commit comments

Comments
 (0)