You are here

function theme_pdf_formatter_thumbnail in PDF 7

Same name and namespace in other branches
  1. 6 pdf.module \theme_pdf_formatter_thumbnail()
1 theme call to theme_pdf_formatter_thumbnail()
pdf_field_formatter_view in includes/pdf.field.inc
Implements hook_field_formatter_view().

File

includes/pdf.field.inc, line 216

Code

function theme_pdf_formatter_thumbnail($variables) {
  if (isset($variables['file']->uri)) {
    $file_url = file_create_url($variables['file']->uri);
  }
  elseif (isset($variables['file']->url)) {
    $file_url = $variables['file']->url;
  }
  $library = libraries_load('pdf.js');
  if ($library['loaded'] == FALSE) {
    drupal_set_message($library['error message'], 'error');
    return 'Please download and install ' . l($library['name'], $library['download url']) . '!';
  }
  $worker_loader = file_create_url(libraries_get_path('pdf.js') . '/build/pdf.worker.js') . "?v={$library['version']}";
  if (version_compare(2, $library['version']) == 1) {
    $js = "PDFJS.workerSrc = '{$worker_loader}';";
  }
  else {
    $js = "pdfjsLib.workerSrc = '{$worker_loader}';";
  }
  $module_path = drupal_get_path('module', 'pdf');

  // drupal_add_css($module_path . '/css/pdf.css');
  // drupal_add_js($module_path . '/js/pdf.js', array('scope' => 'footer'));.
  drupal_add_js($js, array(
    'type' => 'inline',
    'scope' => 'footer',
  ));
  $canvas = array(
    '#type' => 'html_tag',
    '#tag' => 'canvas',
    // '#value' => $file_url,.
    '#attributes' => array(
      'class' => array(
        'pdf-thumbnail',
        'pdf-canvas',
      ),
      'scale' => $variables['scale'],
      'file' => $file_url,
      'style' => 'width:' . $variables['width'] . ';height:' . $variables['height'] . ';',
    ),
  );
  $html = array(
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#value' => render($canvas),
    '#attributes' => array(
      'class' => array(
        'pdf',
      ),
    ),
  );
  $html['#attached']['js'] = array(
    $module_path . '/js/pdf.js',
  );
  $html['#attached']['css'] = array(
    $module_path . '/css/pdf.css',
  );
  return render($html);
}