You are here

public function FrxPrincePDF::render in Forena Reports 7.3

Same name and namespace in other branches
  1. 7.4 docformats/FrxPrincePDF.inc \FrxPrincePDF::render()

Overrides FrxDocument::render

File

docformats/FrxPrincePDF.inc, line 22
FrxPrincePDF PDF document via Prince XML @author davidmetzler

Class

FrxPrincePDF
@file FrxPrincePDF PDF document via Prince XML @author davidmetzler

Code

public function render($r, $format, $options = array()) {
  $disable_links = variable_get('forena_pdf_disable_links', TRUE);
  $html = $this
    ->check_markup($r->html);
  if ($disable_links) {
    $html = preg_replace('/<a href=\\"(.*?)\\">(.*?)<\\/a>/', "\\2", $html);
  }
  $link_class = $disable_links ? 'prince-disable-links' : '';
  $output = '<html><head>';
  $output .= '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>';
  if (@$options['css'] || isset($r->rpt_xml->head->style)) {
    $output .= '<style type="text/css">';
    $output .= $css;
    if (isset($r->rpt_xml->head->style) || isset($r->rpt_xml->head->style)) {
      $sheet = (string) $r->rpt_xml->head->style;
      $output .= $sheet;
    }
    $output .= '</style>';
  }
  $output .= '<title>' . $r->title . "</title></head><body class='forena-report {$link_class}'><h1>" . $r->title . '</h1>' . $html . '</body></html>';
  $prince_css = drupal_get_path('module', 'forena_pdf') . '/forena_pdf_prince.css';

  // Generate the document
  if ($this->p) {
    $p = $this->p;
    foreach (Frx::Skin()->stylesheets as $type => $sheets) {
      foreach ($sheets as $sheet) {
        switch ($type) {
          case 'all':
          case 'print':
          case 'screen':
          case 'pdf':
            $p
              ->addStyleSheet($sheet);
            break;
        }
      }
    }
    $msg = array();
    $pdf_file = tempnam(file_directory_temp(), 'prince_pdf');
    if ($p
      ->convert_string_to_file($output, $pdf_file, $msg)) {
      $output = file_get_contents($pdf_file);
    }
    else {
      drupal_set_message('Could not generate PDF File', 'Error');
      watchdog('pdf error', print_r($msg, 1));
      $output = '';
    }

    // We don't care if this fails because it's temproary.
    @unlink($pdf_file);
    return $output;
  }
  else {
    drupal_set_message(t('Prince XML Not Properly Installed'), 'error');
    return '';
  }
}