public function ViewsPdfBase::drawTable in Views PDF 8
This method draws a table on the PDF.
File
- src/
ViewsPdfBase.php, line 658 - Contains \Drupal\views_pdf\ViewsPdfTemplate.
Class
- ViewsPdfBase
- The main class to generate the PDF.
Namespace
Drupal\views_pdfCode
public function drawTable(&$view, $options) {
$rows = $view->result;
$columns = $view->field;
$pageDim = $this
->getPageDimensions();
// Set draw point to the indicated position:
if (empty($options['position']['x'])) {
$options['position']['x'] = 0;
}
if (empty($options['position']['y'])) {
$options['position']['y'] = 0;
}
if (isset($options['position']['last_writing_position']) && $options['position']['last_writing_position']) {
$y = $options['position']['y'] + $this->y;
$x = $options['position']['x'] + $this->x;
}
else {
$y = $options['position']['y'];
$x = $options['position']['x'];
}
if (isset($options['position']['width']) && !empty($options['position']['width'])) {
$width = $options['position']['width'];
}
else {
$width = $pageDim['wk'] - $this->rMargin - $x;
}
$sumWidth = 0;
$numberOfColumnsWithoutWidth = 0;
// Set the definitiv width of a column
foreach ($columns as $id => $columnName) {
if (isset($options['info'][$id]['position']['width']) && !empty($options['info'][$id]['position']['width'])) {
$sumWidth += $options['info'][$id]['position']['width'];
}
else {
$numberOfColumnsWithoutWidth++;
}
}
if ($numberOfColumnsWithoutWidth > 0) {
$defaultColumnWidth = ($width - $sumWidth) / $numberOfColumnsWithoutWidth;
}
else {
$defaultColumnWidth = 0;
}
// Print header:
$rowX = $x;
$page = $this
->getPage();
if ($page == 0) {
$this
->addPage();
$page = $this
->getPage();
}
if (!isset($options['position']['row_height']) || empty($options['position']['row_height'])) {
$options['position']['row_height'] = 0;
}
foreach ($columns as $id => $column) {
if (!empty($column->options['exclude'])) {
continue;
}
if (!is_array($options['info'][$id])) {
$options['info'][$id] = [];
}
$options['info'][$id] += [
'header_style' => [],
'body_style' => [],
];
$options['info'][$id]['header_style'] += [
'position' => [],
'text' => [],
'render' => [],
];
$options['info'][$id]['header_style']['position'] += [
'corner' => 'top_left',
'x' => NULL,
'y' => NULL,
'object' => '',
'width' => NULL,
'height' => NULL,
];
$options['info'][$id]['header_style']['text'] += [
'font_family' => 'default',
'font_style' => '',
];
$options['info'][$id]['header_style']['text'] += [
'eval_before' => '',
'eval_after' => '',
];
$options['info'][$id]['body_style'] += [
'position' => [],
'text' => [],
'render' => [],
];
$options['info'][$id]['body_style']['position'] += [
'corner' => 'top_left',
'x' => NULL,
'y' => NULL,
'object' => '',
'width' => NULL,
'height' => NULL,
];
$options['info'][$id]['body_style']['text'] += [
'font_family' => 'default',
'font_style' => '',
];
$options['info'][$id]['body_style']['text'] += [
'eval_before' => '',
'eval_after' => '',
];
$headerOptions = $options['info'][$id]['header_style'];
if (isset($options['info'][$id]['position']['width']) && !empty($options['info'][$id]['position']['width'])) {
$headerOptions['position']['width'] = $options['info'][$id]['position']['width'];
}
else {
$headerOptions['position']['width'] = $defaultColumnWidth;
}
$headerOptions['position']['object'] = 'last_position_without_reset';
$this
->SetY($y);
$this
->SetX($x);
$this
->setPage($page);
$this
->renderRow($x, $y, $column->options['label'], $headerOptions);
$x += $headerOptions['position']['width'];
}
$rowY = $this->y;
if (!isset($options['position']['row_height']) || empty($options['position']['row_height'])) {
$options['position']['row_height'] = 0;
}
foreach ($rows as $row) {
$x = $rowX;
// Get the page dimensions
$pageDim = $this
->getPageDimensions();
if ($rowY + $this->bMargin + $options['position']['row_height'] > $pageDim['hk']) {
$rowY = $this->tMargin;
$this
->addPage();
}
if ($this->lastWritingPage != $this
->getPage()) {
$rowY = $this->y;
// $rowY - $pageDim['hk']
}
$y = $rowY;
$page = $this
->getPage();
foreach ($columns as $id => $column) {
if (!empty($column->options['exclude']) && is_object($view->field[$id])) {
// Render the element, but dont print the output. This
// is required to allow the use of tokens in other fields.
$view->field[$id]
->theme($row);
continue;
}
$bodyOptions = $options['info'][$id]['body_style'];
if (isset($options['info'][$id]['position']['width']) && !empty($options['info'][$id]['position']['width'])) {
$bodyOptions['position']['width'] = $options['info'][$id]['position']['width'];
}
else {
$bodyOptions['position']['width'] = $defaultColumnWidth;
}
$bodyOptions['position']['object'] = 'last_position';
$this
->setPage($page);
$this
->SetY($y);
$this
->SetX($x);
$bodyOptions['position']['height'] = 0;
$this
->renderRow($x, $y, $row, $bodyOptions, $view, $id, FALSE);
$x += $bodyOptions['position']['width'];
// If the cell is writting over the row, we need to adjust the
// row y position.
if ($rowY + $options['position']['row_height'] < $this->y) {
$rowY = $this->y - $options['position']['row_height'];
}
}
$rowY += $options['position']['row_height'];
}
$this
->SetY($rowY + $options['position']['row_height']);
}