You are here

function _print_pdf_tcpdf in Printer, email and PDF versions 7

Same name and namespace in other branches
  1. 5.4 print_pdf/print_pdf.pages.inc \_print_pdf_tcpdf()
  2. 5.3 print_pdf/print_pdf.pages.inc \_print_pdf_tcpdf()
  3. 6 print_pdf/print_pdf.pages.inc \_print_pdf_tcpdf()
  4. 5.x print_pdf/print_pdf.pages.inc \_print_pdf_tcpdf()

Generate the PDF file using the TCPDF library

Parameters

$print: array containing the configured data

$html: contents of the post-processed template already with the node data

$filename: name of the PDF file to be generated

See also

print_pdf_controller()

1 call to _print_pdf_tcpdf()
print_pdf_generate_html in print_pdf/print_pdf.pages.inc

File

print_pdf/print_pdf.pages.inc, line 269

Code

function _print_pdf_tcpdf($print, $html, $filename = NULL) {
  global $base_url, $language;
  $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
  $print_pdf_paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
  $print_pdf_page_orientation = variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT);
  $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
  $pdf_tool_path = realpath(dirname($print_pdf_pdf_tool));
  if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
    if (!defined('K_TCPDF_EXTERNAL_CONFIG')) {
      define('K_TCPDF_EXTERNAL_CONFIG', TRUE);
    }
    if (!defined('K_PATH_MAIN')) {
      define('K_PATH_MAIN', dirname($_SERVER['SCRIPT_FILENAME']));
    }
    if (!defined('K_PATH_URL')) {
      define('K_PATH_URL', $base_url);
    }
    if (!defined('K_PATH_FONTS')) {
      define('K_PATH_FONTS', $pdf_tool_path . '/fonts/');
    }
    if (!defined('K_PATH_CACHE')) {
      define('K_PATH_CACHE', drupal_realpath('public://' . PRINT_PDF_TCPDF_CACHE_DIR_DEFAULT . '/cache') . '/');
    }
    if (!defined('K_PATH_IMAGES')) {
      define('K_PATH_IMAGES', '');
    }
    if (!defined('K_BLANK_IMAGE')) {
      define('K_BLANK_IMAGE', $pdf_tool_path . '/images/_blank.png');
    }
    if (!defined('K_CELL_HEIGHT_RATIO')) {
      define('K_CELL_HEIGHT_RATIO', 1.25);
    }
    if (!defined('K_SMALL_RATIO')) {
      define('K_SMALL_RATIO', 2 / 3);
    }
  }

  // Try to use local file access for image files
  $html = _print_pdf_file_access_images($html);

  // Decode HTML entities in image filenames
  $pattern = "!<img\\s[^>]*?src\\s*?=\\s*?['\"]?{$base_url}[^>]*?>!is";
  $html = preg_replace_callback($pattern, create_function('$matches', 'return html_entity_decode($matches[0], ENT_QUOTES);'), $html);

  // Remove queries from the image URL
  $pattern = "!(<img\\s[^>]*?src\\s*?=\\s*?['\"]?{$base_url}[^>]*?)(?:%3F|\\?)[^\\s'\"]+([^>]*?>)!is";
  $html = preg_replace($pattern, '$1$2', $html);
  require_once DRUPAL_ROOT . '/' . $print_pdf_pdf_tool;
  module_load_include('inc', 'print_pdf', 'print_pdf.class');
  $font = array(
    check_plain(variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT)),
    '',
    check_plain(variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT)),
  );
  $orientation = drupal_strtoupper($print_pdf_page_orientation[0]);

  // create new PDF document
  $pdf = new PrintTCPDF($orientation, 'mm', $print_pdf_paper_size, TRUE);

  // set document information
  if (property_exists($print['node'], 'name')) {
    $pdf
      ->SetAuthor(strip_tags($print['node']->name));
  }
  $pdf
    ->SetCreator(variable_get('site_name', 'Drupal'));
  $pdf
    ->SetTitle(html_entity_decode($print['title'], ENT_QUOTES, 'UTF-8'));
  $pdf
    ->setPDFVersion('1.6');
  $pdf
    ->setFontSubsetting(variable_get('print_pdf_font_subsetting', PRINT_PDF_FONT_SUBSETTING_DEFAULT));
  $pdf
    ->setTcpdfLink(false);
  if ($language->direction == LANGUAGE_RTL) {
    $pdf
      ->setRTL(TRUE);
  }
  $pdf = theme('print_pdf_tcpdf_header', array(
    'pdf' => $pdf,
    'html' => $html,
    'font' => $font,
  ));
  $pdf = theme('print_pdf_tcpdf_footer', array(
    'pdf' => $pdf,
    'html' => $html,
    'font' => $font,
  ));
  $pdf = theme('print_pdf_tcpdf_page', array(
    'pdf' => $pdf,
  ));

  // add a page
  $pdf
    ->AddPage();
  $pdf = theme('print_pdf_tcpdf_content', array(
    'pdf' => $pdf,
    'html' => $html,
    'font' => $font,
  ));

  // reset pointer to the last page
  $pdf
    ->lastPage();

  // try to recover from any warning/error
  ob_clean();
  if ($filename) {

    // Close and output PDF document
    $output_dest = $print_pdf_content_disposition == 2 ? 'D' : 'I';
    $pdf
      ->Output($filename, $output_dest);
    return TRUE;
  }
  else {
    return $pdf = $pdf
      ->Output('', 'S');
  }
}