You are here

public function PdfTemplate::drawContent in Views PDF 7.3

Same name and namespace in other branches
  1. 6 views_pdf_template.php \PdfTemplate::drawContent()
  2. 7 views_pdf_template.php \PdfTemplate::drawContent()
  3. 7.2 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 265
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 (empty($view) && empty($key)) {
    return;
  }
  $content = $view->field[$key]
    ->theme($row);
  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,
    'custom_layout' => FALSE,
    'custom_post' => FALSE,
  );

  // Grid-mode flag, true if grid options are provided.
  $isgrid = !empty($options['grid']);

  // 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'])) {
    $enoughSpace = $this->y + $this->bMargin + $options['render']['minimal_space'] < $pageDim['hk'];
  }
  else {
    $enoughSpace = TRUE;
  }

  // Check if there is a page, if not add it:
  if (!$enoughSpace or $this
    ->getPage() == 0 or $this->addNewPageBeforeNextContent) {
    $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();

  // Calculate pseudo-margins, in grid mode these define the limits of the cell.
  $lBound = (double) $this->lMargin;
  $tBound = (double) $this->tMargin;

  // For a grid cell, increase margin by cell offset.
  if ($isgrid) {
    if ($options['grid']['new_cell']) {

      // Temporarily set the left margin to the cell boundary,
      // as this is the position the X co-ordinate automatically resets to.
      $lBound = (double) $this->original_lMargin + $options['grid']['x'];
      $this
        ->SetLeftMargin($lBound);
      $this
        ->SetY($tBound);
    }
    $tBound += $options['grid']['y'];
    $rBound = $lBound + $options['grid']['w'];
    $bBound = $tBound + $options['grid']['h'];
  }
  else {
    $rBound = (double) $pageDim['wk'] - (double) $this->rMargin;
    $bBound = (double) $pageDim['hk'] - (double) $this->bMargin;
  }

  // Determine the last writing 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;
  }

  // Determine the x and y coordinates
  if ($options['position']['object'] == 'last_position') {
    $x = (double) $this->x + (double) $options['position']['x'];
    $y = (double) $this->y + (double) $options['position']['y'];
  }
  elseif ($options['position']['object'] == 'page') {
    switch ($options['position']['corner']) {
      default:
      case 'top_left':
        $x = (double) $options['position']['x'] + $lBound;
        $y = (double) $options['position']['y'] + $tBound;
        break;
      case 'top_right':
        $x = (double) $options['position']['x'] + $rBound;
        $y = (double) $options['position']['y'] + $tBound;
        break;
      case 'bottom_left':
        $x = (double) $options['position']['x'] + $lBound;
        $y = (double) $options['position']['y'] + $bBound;
        break;
      case 'bottom_right':
        $x = (double) $options['position']['x'] + $rBound;
        $y = (double) $options['position']['y'] + $bBound;
        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 = (double) $options['position']['x'] + (double) $this->elements[$relative_to_element]['x'];
          $y = (double) $options['position']['y'] + (double) $this->elements[$relative_to_element]['y'];
          break;
        case 'top_right':
          $x = (double) $options['position']['x'] + (double) $this->elements[$relative_to_element]['x'] + (double) $this->elements[$relative_to_element]['width'];
          $y = (double) $options['position']['y'] + (double) $this->elements[$relative_to_element]['y'];
          break;
        case 'bottom_left':
          $x = (double) $options['position']['x'] + (double) $this->elements[$relative_to_element]['x'];
          $y = (double) $options['position']['y'] + (double) $this->elements[$relative_to_element]['y'] + (double) $this->elements[$relative_to_element]['height'];
          break;
        case 'bottom_right':
          $x = (double) $options['position']['x'] + (double) $this->elements[$relative_to_element]['x'] + (double) $this->elements[$relative_to_element]['width'];
          $y = (double) $options['position']['y'] + (double) $this->elements[$relative_to_element]['y'] + (double) $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 -= $this->elements[$relative_to_element]['y'] + $last_writing_y_position;
        $this
          ->SetPage($this->lastWritingPage);
      }
    }
    else {
      $x = (double) $this->x;
      $y = (double) $last_writing_y_position;
    }
  }
  else {
    return;
  }

  // In grid mode, set width and height not to exceed edge of grid cell.
  if ($isgrid) {

    // If start point is outside the cell, just return.
    if ($x >= $rBound || $y >= $bBound) {
      return;
    }
    $maxw = $rBound - $x;
    $maxh = $bBound - $y;
    $options['position']['width'] = $options['position']['width'] == 0 ? $maxw : min($options['position']['width'], $maxw);
    $options['position']['height'] = $options['position']['height'] == 0 ? $maxh : min($options['position']['height'], $maxh);
  }
  $this
    ->SetX($x);
  $this
    ->SetY($y);
  $this
    ->renderItem($x, $y, $content, $row, $options, $view, $key, $printLabels, FALSE, $isgrid);
}