Skip to content

Commit

Permalink
MDL-70117 dataformat_pdf: Fix content overflow when headers are involved
Browse files Browse the repository at this point in the history
  • Loading branch information
golenkovm committed Nov 10, 2020
1 parent d330035 commit 8d025b4
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions dataformat/pdf/classes/writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function start_sheet($columns) {
$this->colwidth = $pagewidth / count($columns);
$this->columns = $columns;

$this->print_heading();
$this->print_heading($this->pdf);
}

/**
Expand Down Expand Up @@ -137,10 +137,12 @@ public function write_record($record, $rownum) {
$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();
}
Expand All @@ -149,7 +151,7 @@ public function write_record($record, $rownum) {
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.
Expand Down Expand Up @@ -186,25 +188,36 @@ public function close_output_to_file(): bool {
}

/**
* 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;
}
}

0 comments on commit 8d025b4

Please sign in to comment.