From 10a0ea20acf1a190855d8fc5a25c9e80df71ee2a Mon Sep 17 00:00:00 2001 From: Mikhail Golenkov Date: Wed, 11 Nov 2020 10:49:58 +1100 Subject: [PATCH] MDL-70117 dataformat_pdf: Fix content overflow when headers are involved --- dataformat/pdf/classes/writer.php | 43 ++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/dataformat/pdf/classes/writer.php b/dataformat/pdf/classes/writer.php index 3f916b1a59d..a4b2127bfc0 100644 --- a/dataformat/pdf/classes/writer.php +++ b/dataformat/pdf/classes/writer.php @@ -90,7 +90,7 @@ class writer extends \core\dataformat\base { $this->colwidth = $pagewidth / count($columns); $this->columns = $columns; - $this->print_heading(); + $this->print_heading($this->pdf); } /** @@ -137,10 +137,12 @@ class writer extends \core\dataformat\base { $pdf2->startTransaction(); $numpages = $pdf2->getNumPages(); $pdf2->AddPage('L'); + $this->print_heading($pdf2); $pdf2->writeHTMLCell($this->colwidth, 0, '', '', $cell, 1, 1, false, true, 'L'); $pagesadded = $pdf2->getNumPages() - $numpages; - $pageheight = $pdf2->getPageHeight() - $pdf2->getMargins()['top'] - $pdf2->getMargins()['bottom']; - $cellheight = ($pagesadded - 1) * $pageheight + $pdf2->getLastH(); + $margins = $pdf2->getMargins(); + $pageheight = $pdf2->getPageHeight() - $margins['top'] - $margins['bottom']; + $cellheight = ($pagesadded - 1) * $pageheight + $pdf2->getY() - $margins['top'] - $this->get_heading_height(); $rowheight = max($rowheight, $cellheight); $pdf2->rollbackTransaction(); } @@ -149,7 +151,7 @@ class writer extends \core\dataformat\base { if ($this->pdf->getNumPages() > 1 && ($this->pdf->GetY() + $rowheight + $margins['bottom'] > $this->pdf->getPageHeight())) { $this->pdf->AddPage('L'); - $this->print_heading(); + $this->print_heading($this->pdf); } // Get the last key for this record. @@ -186,25 +188,36 @@ class writer extends \core\dataformat\base { } /** - * Prints the heading row. + * Prints the heading row for a given PDF. + * + * @param \pdf $pdf A pdf to print headings in */ - private function print_heading() { - $fontfamily = $this->pdf->getFontFamily(); - $fontstyle = $this->pdf->getFontStyle(); - $this->pdf->SetFont($fontfamily, 'B'); - $rowheight = 0; - foreach ($this->columns as $columns) { - $rowheight = max($rowheight, $this->pdf->getStringHeight($this->colwidth, $columns, false, true, '', 1)); - } + private function print_heading(\pdf $pdf) { + $fontfamily = $pdf->getFontFamily(); + $fontstyle = $pdf->getFontStyle(); + $pdf->SetFont($fontfamily, 'B'); $total = count($this->columns); $counter = 1; foreach ($this->columns as $columns) { $nextposition = ($counter == $total) ? 1 : 0; - $this->pdf->Multicell($this->colwidth, $rowheight, $columns, 1, 'C', true, $nextposition); + $pdf->Multicell($this->colwidth, $this->get_heading_height(), $columns, 1, 'C', true, $nextposition); $counter++; } - $this->pdf->SetFont($fontfamily, $fontstyle); + $pdf->SetFont($fontfamily, $fontstyle); + } + + /** + * Returns the heading height. + * + * @return int + */ + private function get_heading_height() { + $height = 0; + foreach ($this->columns as $columns) { + $height = max($height, $this->pdf->getStringHeight($this->colwidth, $columns, false, true, '', 1)); + } + return $height; } }