Skip to content

Commit

Permalink
Fix TOC multipage
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Aug 24, 2024
1 parent 8477ae9 commit 2c63d67
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,7 @@

$pdf->graph->add($style_cell_toc);

$pdf->addTOC(-1, 15, 15, 170, false);
$pdf->addTOC(-1, 15, 30, 170, false);


// =============================================================
Expand Down
31 changes: 18 additions & 13 deletions src/Tcpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] +
Expand All @@ -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'],
Expand All @@ -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'])) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit 2c63d67

Please sign in to comment.