From 2c63d673e4bc40f6e97bda9883c0438634904be0 Mon Sep 17 00:00:00 2001 From: nicolaasuni Date: Sat, 24 Aug 2024 15:37:58 +0100 Subject: [PATCH] Fix TOC multipage --- examples/index.php | 2 +- src/Tcpdf.php | 31 ++++++++++++++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/examples/index.php b/examples/index.php index e731b31..3b77ec2 100644 --- a/examples/index.php +++ b/examples/index.php @@ -1624,7 +1624,7 @@ $pdf->graph->add($style_cell_toc); -$pdf->addTOC(-1, 15, 15, 170, false); +$pdf->addTOC(-1, 15, 30, 170, false); // ============================================================= diff --git a/src/Tcpdf.php b/src/Tcpdf.php index 391b72d..64d2abc 100644 --- a/src/Tcpdf.php +++ b/src/Tcpdf.php @@ -1082,10 +1082,14 @@ public function addTOC( $numwidth = 9 * $chrw; // maximum 9 digits to print the page number. $txtwidth = ($width - $numwidth); - $cellSpaceTop = $this->toUnit( + $cellSpaceT = $this->toUnit( $this->defcell['margin']['T'] + $this->defcell['padding']['T'] ); + $cellSpaceB = $this->toUnit( + $this->defcell['margin']['B'] + + $this->defcell['padding']['B'] + ); $cellSpaceH = $chrw + $this->toUnit( $this->defcell['margin']['L'] + $this->defcell['margin']['L'] + @@ -1107,16 +1111,6 @@ public function addTOC( $pid = ($page < 0) ? $this->page->getPageID() : $page; foreach ($this->outlines as $bmrk) { - $region = $this->page->getRegion($pid); - if ($posy > $region['RH']) { - $opage = $this->page->getNextRegion($pid); - $pid = $opage['pid']; - $region = $this->page->getRegion($pid); - $posy = 0; // $region['RY']; - } - - $this->page->addContent($this->graph->getStartTransform(), $pid); - $font = $this->font->cloneFont( $this->pon, $curfont['idx'], @@ -1125,6 +1119,17 @@ public function addTOC( $curfont['spacing'], $curfont['stretching'], ); + + $region = $this->page->getRegion($pid); + + if (($posy + $cellSpaceT + $cellSpaceB + $font['height']) > $region['RH']) { + $opage = $this->page->getNextRegion($pid); + $pid = $opage['pid']; + $region = $this->page->getRegion($pid); + $posy = 0; // $region['RY']; + } + + $this->page->addContent($this->graph->getStartTransform(), $pid); $this->page->addContent($font['out'], $pid); if (! empty($bmrk['c'])) { @@ -1170,7 +1175,7 @@ public function addTOC( $this->page->addContent($font['out'], $pid); } - $posy = $bbox['y'] - $cellSpaceTop; // align number with the last line of the text + $posy = $bbox['y'] - $cellSpaceT; // align number with the last line of the text // add page number $this->addTextCell( @@ -1213,7 +1218,7 @@ public function addTOC( $this->page->addContent($this->graph->getStopTransform(), $pid); // Move to the next line. - $posy = $bbox['y'] + $bbox['h']; + $posy = $bbox['y'] + $bbox['h'] + $cellSpaceB; } } }