From d1e71248515f4b619468d4f9033b85cd3458b01e Mon Sep 17 00:00:00 2001 From: "norman.yang" Date: Wed, 25 Sep 2024 17:18:16 +0800 Subject: [PATCH] Remove the lines and content of the default header and footer in TCPDF, automatically call and display the header and footer set in XML, and also automatically call when paging the page content, but the header and footer cannot have too much content beyond the page range. --- app/Report/PdfRenderer.php | 57 ++++++++++++++++++++++++++------- app/Report/ReportPdfCell.php | 11 ++++--- app/Report/ReportPdfImage.php | 7 ++-- app/Report/ReportPdfTextBox.php | 12 ++++--- 4 files changed, 63 insertions(+), 24 deletions(-) diff --git a/app/Report/PdfRenderer.php b/app/Report/PdfRenderer.php index bbe261242b4..5c219e8e1c5 100644 --- a/app/Report/PdfRenderer.php +++ b/app/Report/PdfRenderer.php @@ -67,22 +67,31 @@ class PdfRenderer extends AbstractRenderer // The last pictures page number public int $lastpicpage = 0; + // The HEADER OR FOOTER to render + public bool $isHeaderOrFooter = false; + + //The breakpage header y + public float $breakPageHeaderHeight = 0.0; + /** * PDF Header -PDF * * @return void */ - public function header(): void + public function header(): void //sfqas { - foreach ($this->headerElements as $element) { + $isHeaderOrFooter = true; + foreach ($this->headerElements as $element) { if ($element instanceof ReportBaseElement) { - $element->render($this); + $element->render($this,$isHeaderOrFooter); } elseif ($element === 'footnotetexts') { $this->footnotes(); } elseif ($element === 'addpage') { $this->newPage(); } } + $isHeaderOrFooter = false; + $this->breakPageHeaderHeight =$this->tcpdf->GetY(); } /** @@ -90,10 +99,10 @@ public function header(): void * * @return void */ - public function body(): void + public function body(): void // //sfqas { - $this->tcpdf->AddPage(); - + // $this->tcpdf->AddPage(); + $this->newPage(); foreach ($this->bodyElements as $element) { if ($element instanceof ReportBaseElement) { $element->render($this); @@ -130,17 +139,19 @@ public function footnotes(): void * * @return void */ - public function footer(): void + public function footer(): void //sfqas { - foreach ($this->footerElements as $element) { + $isHeaderOrFooter = true; + foreach ($this->footerElements as $element) { if ($element instanceof ReportBaseElement) { - $element->render($this); + $element->render($this,$isHeaderOrFooter); } elseif ($element === 'footnotetexts') { $this->footnotes(); } elseif ($element === 'addpage') { $this->newPage(); } } + $isHeaderOrFooter = false; } /** @@ -317,12 +328,18 @@ public function checkFootnote(ReportPdfFootnote $footnote) * * @return void */ - public function newPage(): void + public function newPage(): void // // //sfqas { if ($this->lastpicpage > $this->tcpdf->getPage()) { $this->tcpdf->setPage($this->lastpicpage); } $this->tcpdf->AddPage(); + + $this->header(); + // $this->tcpdf->setY($this->tcpdf->getPageHeight() - 75); + $this->tcpdf->setY($this->tcpdf->getPageHeight()-$this->footer_margin-55); + $this->footer(); + $this->tcpdf->setY($this->breakPageHeaderHeight); } /** @@ -332,9 +349,22 @@ public function newPage(): void * * @return bool true in case of page break, false otherwise */ - public function checkPageBreakPDF(float $height): bool + public function checkPageBreakPDF(float $height): bool // // //sfqas { - return $this->tcpdf->checkPageBreak($height); + // return $this->tcpdf->checkPageBreak($height); + $isPageBreaked = false; + if(!$this->isHeaderOrFooter){ + $isPageBreaked = $this->tcpdf->checkPageBreak($height); + $this->tcpdf->setY($this->header_margin); + $this->header(); + // $this->tcpdf->setY($this->tcpdf->getPageHeight() - 75); + $this->tcpdf->setY($this->tcpdf->getPageHeight()-$this->footer_margin-55); + $this->footer(); + $this->tcpdf->setY($this->breakPageHeaderHeight); + return $isPageBreaked ; + }else{ + return false; + } } /** @@ -378,6 +408,9 @@ public function setup(): void $this->tcpdf->setKeywords($this->rkeywords); $this->tcpdf->setHeaderData('', 0, $this->title); $this->tcpdf->setHeaderFont([$this->default_font, '', $this->default_font_size]); + // // remove default header/footer + $this->tcpdf->setPrintHeader(false); + $this->tcpdf->setPrintFooter(false); if ($this->show_generated_by) { // The default style name for Generated by.... is 'genby' diff --git a/app/Report/ReportPdfCell.php b/app/Report/ReportPdfCell.php index 35667a7aca4..4169f2e8173 100644 --- a/app/Report/ReportPdfCell.php +++ b/app/Report/ReportPdfCell.php @@ -36,7 +36,7 @@ class ReportPdfCell extends ReportBaseCell * * @return void */ - public function render($renderer): void + public function render($renderer, bool $headerorfoot=false): void //sfqas { $temptext = str_replace('#PAGENUM#', (string) $renderer->tcpdf->PageNo(), $this->text); // underline «title» part of Source item @@ -125,9 +125,12 @@ public function render($renderer): void $cHT += $cM['cell'] * 2; } // Add a new page if needed - if ($renderer->checkPageBreakPDF($cHT)) { - $this->top = $renderer->tcpdf->GetY(); - } + //sfqas + if ($renderer->tcpdf->checkPageBreak($cHT,null,false) and !$headerorfoot){ + if ($renderer->checkPageBreakPDF($cHT)) { + $this->top = $renderer->tcpdf->GetY(); + } + } $temptext = RightToLeftSupport::spanLtrRtl($temptext); } // HTML ready - last value is true diff --git a/app/Report/ReportPdfImage.php b/app/Report/ReportPdfImage.php index 3bf6ded97de..c26af9d158b 100644 --- a/app/Report/ReportPdfImage.php +++ b/app/Report/ReportPdfImage.php @@ -31,13 +31,14 @@ class ReportPdfImage extends ReportBaseImage * * @return void */ - public function render($renderer): void + public function render($renderer,bool $headerorfoot=false): void //sfqas { static $lastpicbottom, $lastpicpage, $lastpicleft, $lastpicright; // Check for a pagebreak first - if ($renderer->checkPageBreakPDF($this->height + 5)) { - $this->y = $renderer->tcpdf->GetY(); + if ($renderer->tcpdf->checkPageBreak($this->height + 5,null,false) and !$headerorfoot){ //sfqas + $renderer->checkPageBreakPDF($this->height + 5); + $this->y = $renderer->tcpdf->GetY(); } $curx = $renderer->tcpdf->GetX(); diff --git a/app/Report/ReportPdfTextBox.php b/app/Report/ReportPdfTextBox.php index 48130940783..03c249d8002 100644 --- a/app/Report/ReportPdfTextBox.php +++ b/app/Report/ReportPdfTextBox.php @@ -40,7 +40,7 @@ class ReportPdfTextBox extends ReportBaseTextbox * * @return void */ - public function render($renderer): void + public function render($renderer,bool $headerorfoot=false): void //sfqas { $newelements = []; $lastelement = ''; @@ -224,11 +224,13 @@ public function render($renderer): void } // Add a new page if needed if ($this->pagecheck) { - // Reset last cell height or Header/Footer will inherit it, in case of pagebreak - $renderer->lastCellHeight = 0; - if ($renderer->checkPageBreakPDF($cH)) { - $cY = $renderer->tcpdf->GetY(); + if ($renderer->tcpdf->checkPageBreak($cH,null,false) and !$headerorfoot){ + // Reset last cell height or Header/Footer will inherit it, in case of pagebreak + $renderer->lastCellHeight = 0; + if ($renderer->checkPageBreakPDF($cH)) { + $cY = $renderer->tcpdf->GetY(); } + } } // Setup the border and background color