You are here

protected function PdfTemplate::renderItem in Views PDF 7.3

3 calls to PdfTemplate::renderItem()
PdfTemplate::drawContent in ./views_pdf_template.php
This method draws a field on the PDF.
PdfTemplate::drawTable in ./views_pdf_template.php
This method draws a table on the PDF.
PdfTemplate::Header in ./views_pdf_template.php
Function override to output the header.

File

./views_pdf_template.php, line 469
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

protected function renderItem($x, $y, $content, $row, $options, &$view, $key, $printLabels = TRUE, $istable = FALSE, $isgrid = FALSE) {

  // Only render if not excluded, and not a header or footer field.
  if (empty($view->field[$key]->options['exclude']) && $options['position']['object'] !== 'header_footer') {
    $pageDim = $this
      ->getPageDimensions();

    // Apply the hyphenation patterns to the content:
    if (!isset($options['text']['hyphenate']) && is_object($view) && is_object($view->display_handler)) {
      $options['text']['hyphenate'] = $view->display_handler
        ->get_option('default_text_hyphenate');
    }
    if (isset($options['text']['hyphenate']) && $options['text']['hyphenate'] != 'none') {
      $patternFile = $options['text']['hyphenate'];
      if ($options['text']['hyphenate'] == 'auto' && is_object($row)) {

        // Workaround:
        // Since "$nodeLanguage = $row->node_language;" does not work anymore,
        // we using this:
        if (isset($row->_field_data['nid']['entity']->language)) {
          $nodeLanguage = $row->_field_data['nid']['entity']->language;
          foreach (self::getAvailableHyphenatePatterns() as $file => $pattern) {
            if (stristr($pattern, $nodeLanguage) !== FALSE) {
              $patternFile = $file;
              break;
            }
          }
        }
      }
      $patternFile = views_pdf_get_library('tcpdf') . '/hyphenate_patterns/' . $patternFile;
      if (file_exists($patternFile)) {
        if (method_exists('TCPDF_STATIC', 'getHyphenPatternsFromTEX')) {
          $hyphen_patterns = TCPDF_STATIC::getHyphenPatternsFromTEX($patternFile);
        }
        else {
          $hyphen_patterns = $this
            ->getHyphenPatternsFromTEX($patternFile);
        }

        // Bugfix if you like to print some html code to the PDF, we
        // need to prevent the replacement of this tags.
        $content = str_replace('>', '>', $content);
        $content = str_replace('<', '<', $content);
        $content = $this
          ->hyphenateText($content, $hyphen_patterns);
      }
    }

    // Set css variable
    if (is_object($view) && is_object($view->display_handler)) {
      $css_file = $view->display_handler
        ->get_option('css_file');
    }
    $font_size = empty($options['text']['font_size']) ? $this->defaultFontSize : $options['text']['font_size'];
    $font_family = $options['text']['font_family'] == 'default' || empty($options['text']['font_family']) ? $this->defaultFontFamily : $options['text']['font_family'];
    $font_style = is_array($options['text']['font_style']) ? $options['text']['font_style'] : $this->defaultFontStyle;
    $textColor = !empty($options['text']['color']) ? $this
      ->parseColor($options['text']['color']) : $this
      ->parseColor($this->defaultFontColor);
    $w = $options['position']['width'];
    $h = $options['position']['height'];
    $border = !empty($options['text']['border']) ? $options['text']['border'] : 0;
    $align = isset($options['text']['align']) ? $options['text']['align'] : $this->defaultTextAlign;
    $fill = 0;
    $ln = 1;
    $reseth = TRUE;
    $stretch = 0;
    $ishtml = $istable || $isgrid ? 0 : (isset($options['render']['is_html']) ? $options['render']['is_html'] : 1);
    $stripHTML = !$ishtml;
    $autopadding = TRUE;
    if ($istable) {

      // For table mode we use a precise height.
      $maxh = $h;
    }
    elseif ($isgrid) {

      // For grid mode we use auto-height, with max height defining the grid boundary.
      $maxh = $h;
      $h = 0;
    }
    else {
      $maxh = 0;
    }

    // Render Labels
    $prefix = '';
    if ($printLabels && !empty($view->field[$key]->options['label'])) {
      $prefix = $view->field[$key]->options['label'];
      if ($view->field[$key]->options['element_label_colon']) {
        $prefix .= ':';
      }
      $prefix .= ' ';
    }
    if (!empty($prefix) && !$stripHTML) {

      // If label HTML has been customised, add tag and classes as required.
      $label_info = $view->field[$key]->options;
      if ($tag = $label_info['element_label_type']) {
        $classes = array();
        if ($label_info['element_label_class']) {
          $classes[] = $label_info['element_label_class'];
        }
        if ($label_info['element_default_classes']) {
          $classes[] = 'views-label';
          $classes[] = "views-label-{$label_info['id']}";
        }
        $class = !empty($classes) ? ' class="' . implode(' ', $classes) . '"' : '';
        $prefix = "<{$tag}{$class}>{$prefix}</{$tag}>";
      }
    }

    // Run eval before.
    if (VIEWS_PDF_PHP && !empty($options['render']['eval_before'])) {
      if (empty($options['render']['bypass_eval_before'])) {
        $content = php_eval($options['render']['eval_before']);
      }
      else {
        eval($options['render']['eval_before']);
      }
    }
    if (!empty($options['render']['custom_layout'])) {

      // Custom layout hook.
      $layout_data = array(
        'x' => &$x,
        'y' => &$y,
        'h' => &$h,
        'w' => &$w,
        'content' => &$content,
        'key' => &$key,
        'view' => &$view,
        'this' => &$this,
        'border' => &$border,
        'color' => &$textColor,
        'font' => &$font_family,
        'font_style' => &$font_style,
        'font_size' => &$font_size,
      );
      drupal_alter('views_pdf_custom_layout', $layout_data);
    }

    // Add css if there is a css file set and stripHTML is not active.
    if (!empty($css_file) && is_string($css_file) && !$stripHTML && !empty($content)) {
      $content = '<link type="text/css" rel="stylesheet" media="all" href="' . $css_file . '" />' . PHP_EOL . $content;
    }

    // Set Text Color.
    $this
      ->SetTextColorArray($textColor);

    // Set font.
    $this
      ->SetFont($font_family, implode('', $font_style), $font_size);

    // Save the last page before starting writing, this
    // is needed to detect if we write over a page. Then we need
    // to reset the y coordinate for the 'last_writing' position option.
    $this->lastWritingPage = $this
      ->getPage();
    if ($stripHTML) {
      $content = html_entity_decode(strip_tags($content), ENT_QUOTES | ENT_HTML401);
    }

    // Write the content of a field to the pdf file:
    if ($istable) {
      $this->cell_padding['T'] = $this->cell_padding['B'] = !empty($options['text']['vpad']) ? $options['text']['vpad'] : 0;
    }
    $this
      ->MultiCell($w, $h, $prefix . $content, $border, $align, $fill, $ln, $x, $y, $reseth, $stretch, $ishtml, $autopadding, $maxh);

    // Post render.
    if (!empty($options['render']['custom_post'])) {
      drupal_alter('views_pdf_custom_post', $view);
    }

    // Run eval after.
    if (VIEWS_PDF_PHP && !empty($options['render']['eval_after'])) {

      // Questionable whether there's any point supporting php_eval()
      // after render. It has no access to the local context, and can
      // only return a value. But the output has already been written
      // so there's nothing that can be done with the return value!
      if (empty($options['render']['bypass_eval_after'])) {
        php_eval($options['render']['eval_after']);
      }
      else {
        eval($options['render']['eval_after']);
      }
    }

    // Write Coordinates of element.
    $this->elements[$key] = array(
      'x' => $x,
      'y' => $y,
      'width' => empty($w) ? $pageDim['wk'] - $this->rMargin - $x : $w,
      'height' => $this->y - $y,
      'page' => $this->lastWritingPage,
    );
    $this->lastWritingElement = $key;
  }
}