You are here

public function PdfTemplate::drawContent in Views PDF 7.2

Same name and namespace in other branches
  1. 6 views_pdf_template.php \PdfTemplate::drawContent()
  2. 7.3 views_pdf_template.php \PdfTemplate::drawContent()
  3. 7 views_pdf_template.php \PdfTemplate::drawContent()

This method draws a field on the PDF.

1 call to PdfTemplate::drawContent()
PdfTemplate::Close in ./views_pdf_template.php
Close the document. This is called automatically by TCPDF::Output().

File

./views_pdf_template.php, line 219
PDF Class to generate PDFs with native PHP. This class based on FPDF and FPDI.

Class

PdfTemplate
The main class to generate the PDF.

Code

public function drawContent($row, $options, &$view = NULL, $key = NULL, $printLabels = TRUE) {
  if (!is_array($options)) {
    $options = array();
  }

  // Set defaults:
  $options += array(
    'position' => array(),
    'text' => array(),
    'render' => array(),
  );
  $options['position'] += array(
    'corner' => 'top_left',
    'x' => 0,
    'y' => 0,
    'object' => 'last_position',
    'width' => 0,
    'height' => 0,
  );
  $options['text'] += array(
    'font_family' => 'default',
    'font_style' => '',
  );
  $options['render'] += array(
    'eval_before' => '',
    'eval_after' => '',
    'bypass_eval_before' => FALSE,
    'bypass_eval_after' => FALSE,
  );
  $x = $y = 0;

  // Get the page dimensions
  $pageDim = $this
    ->getPageDimensions();

  // Check if there is a minimum space defined. If so, then ensure
  // that we have enough space left on this page. If not force adding
  // a new one.
  if (isset($options['render']['minimal_space'])) {
    $enoughtSpace = $this->y + $this->bMargin + $options['render']['minimal_space'] < $pageDim['hk'];
  }
  else {
    $enoughtSpace = TRUE;
  }

  // Check if there is a page, if not add it:
  if (!$enoughtSpace or $this
    ->getPage() == 0 or $this->addNewPageBeforeNextContent == TRUE) {
    $this->addNewPageBeforeNextContent = FALSE;
    $this
      ->addPage();
  }

  // Get the page dimenstions again, because it can be that a new
  // page was added with new dimensions.
  $pageDim = $this
    ->getPageDimensions();

  // Determine the last writting y coordinate, if we are on a new
  // page we need to reset it back to the top margin.
  if ($this->lastWritingPage != $this
    ->getPage() or $this->y + $this->bMargin > $pageDim['hk']) {
    $last_writing_y_position = $this->tMargin;
  }
  else {
    $last_writing_y_position = $this->y;
  }

  // Determin the x and y coordinates
  if ($options['position']['object'] == 'last_position') {
    $x = $this->x + $options['position']['x'];
    $y = $this->y + $options['position']['y'];
  }
  elseif ($options['position']['object'] == 'page') {
    switch ($options['position']['corner']) {
      default:
      case 'top_left':
        $x = $options['position']['x'] + $this->lMargin;
        $y = $options['position']['y'] + $this->tMargin;
        break;
      case 'top_right':
        $x = $options['position']['x'] + $pageDim['wk'] - $this->rMargin;
        $y = $options['position']['y'] + $this->tMargin;
        break;
      case 'bottom_left':
        $x = $options['position']['x'] + $this->rMargin;
        $y = $options['position']['y'] + $pageDim['hk'] - $this->bMargin;
        break;
      case 'bottom_right':
        $x = $options['position']['x'] + $pageDim['wk'] - $this->rMargin;
        $y = $options['position']['y'] + $pageDim['hk'] - $this->bMargin;
        break;
    }
  }
  elseif ($options['position']['object'] == 'self' or preg_match('/field\\_(.*)/', $options['position']['object'], $rs)) {
    if ($options['position']['object'] == 'last') {
      $relative_to_element = $this->lastWritingElement;
    }
    elseif ($options['position']['object'] == 'self') {
      $relative_to_element = $key;
    }
    else {
      $relative_to_element = $rs[1];
    }
    if (isset($this->elements[$relative_to_element])) {
      switch ($options['position']['corner']) {
        default:
        case 'top_left':
          $x = $options['position']['x'] + $this->elements[$relative_to_element]['x'];
          $y = $options['position']['y'] + $this->elements[$relative_to_element]['y'];
          break;
        case 'top_right':
          $x = $options['position']['x'] + $this->elements[$relative_to_element]['x'] + $this->elements[$relative_to_element]['width'];
          $y = $options['position']['y'] + $this->elements[$relative_to_element]['y'];
          break;
        case 'bottom_left':
          $x = $options['position']['x'] + $this->elements[$relative_to_element]['x'];
          $y = $options['position']['y'] + $this->elements[$relative_to_element]['y'] + $this->elements[$relative_to_element]['height'];
          break;
        case 'bottom_right':
          $x = $options['position']['x'] + $this->elements[$relative_to_element]['x'] + $this->elements[$relative_to_element]['width'];
          $y = $options['position']['y'] + $this->elements[$relative_to_element]['y'] + $this->elements[$relative_to_element]['height'];
          break;
      }

      // Handle if the relative element is on another page. So using the
      // the last writing position instead for y.
      if ($this
        ->getPage() != $this->elements[$relative_to_element]['page'] && $options['position']['object'] != 'self') {
        $this
          ->setPage($this->elements[$relative_to_element]['page']);
      }
      elseif ($this
        ->getPage() != $this->elements[$relative_to_element]['page'] && $options['position']['object'] == 'self') {
        $y = $y - $this->elements[$relative_to_element]['y'] + $last_writing_y_position;
        $this
          ->SetPage($this->lastWritingPage);
      }
    }
    else {
      $x = $this->x;
      $y = $last_writing_y_position;
    }
  }
  else {

    // Render or return
    if (is_object($view) && $key != NULL) {
      $content = $view->field[$key]
        ->theme($row);
    }
    else {
      return;
    }
  }
  if ($key !== NULL && $view->field[$key]
    ->theme($row) || !empty($row)) {
    $this
      ->SetX($x);
    $this
      ->SetY($y);
    $this
      ->renderRow($x, $y, $row, $options, $view, $key, $printLabels);
  }
}