You are here

function _print_pdf_file_access_images in Printer, email and PDF versions 6

Same name and namespace in other branches
  1. 5.4 print_pdf/print_pdf.pages.inc \_print_pdf_file_access_images()
  2. 7 print_pdf/print_pdf.pages.inc \_print_pdf_file_access_images()
  3. 5.x print_pdf/print_pdf.pages.inc \_print_pdf_file_access_images()

Convert image paths to the file:// protocol

In some Drupal setups, the use of the 'private' filesystem or Apache's configuration prevent access to the images of the page. This function tries to circumnvent those problems by accessing files in the local filesystem.

Parameters

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

See also

print_pdf_controller()

2 calls to _print_pdf_file_access_images()
_print_pdf_dompdf in print_pdf/print_pdf.pages.inc
Generate the PDF file using the dompdf library
_print_pdf_tcpdf in print_pdf/print_pdf.pages.inc
Generate the PDF file using the TCPDF library

File

print_pdf/print_pdf.pages.inc, line 140

Code

function _print_pdf_file_access_images($html) {
  global $base_url, $language;
  $print_pdf_images_via_file = variable_get('print_pdf_images_via_file', PRINT_PDF_IMAGES_VIA_FILE_DEFAULT);
  switch (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE)) {
    case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
    case LANGUAGE_NEGOTIATION_PATH:
      $lang = $language->language;
      break;
    default:
      $lang = '';
      break;
  }

  // Always convert private to local paths
  $file_downloads = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC);
  if ($file_downloads == FILE_DOWNLOADS_PRIVATE) {
    $pattern = "!(<img\\s[^>]*?src\\s*?=\\s*?['\"]?){$base_url}/(?:(?:index.php)?\\?q=)?(?:{$lang}/)?system/files/([^>]*?>)!is";
    $replacement = '$1file://' . realpath(file_directory_path()) . '/$2';
    $html = preg_replace($pattern, $replacement, $html);
  }
  elseif ($print_pdf_images_via_file) {
    $pattern = "!(<img\\s[^>]*?src\\s*?=\\s*?['\"]?){$base_url}/(?:(?:index.php)?\\?q=)?(?:{$lang}/)?([^>]*?>)!is";
    $replacement = '$1file://' . dirname($_SERVER['SCRIPT_FILENAME']) . '/$2';
    $html = preg_replace($pattern, $replacement, $html);
  }
  return $html;
}