You are here

function pdf_to_imagefield_generate_page in PDF to ImageField 7

Same name and namespace in other branches
  1. 6.2 pdf_to_imagefield.module \pdf_to_imagefield_generate_page()

Generate a single page for the given pdf file-source, according to the field settings.

Parameters

$pdf a file data object:

$node the field is attached to:

$source filefiled a field definition:

Return value

a managed jpeg file, already inserted into the files table.

2 calls to pdf_to_imagefield_generate_page()
pdf_to_imagefield_generate_page_batched in ./pdf_to_imagefield.module
Generate a single page for the given pdf file-source, according to the field settings.
pdf_to_imagefield_node_presave in ./pdf_to_imagefield.module
hook_node_presave() nodapi subroutine

File

./pdf_to_imagefield.module, line 342
PDF to ImageField core hooks and menu callbacks.

Code

function pdf_to_imagefield_generate_page($pdf, $source_filefield, $page_number = 0) {
  if (empty($pdf['filepath']) || !file_exists($pdf['filepath'])) {
    return NULL;
  }

  // Security - ensure no nasty characters make it through to the commandline due to configuration passthroughs.
  $density_x = preg_replace('/^([0-9]+)x[0-9]+$/', '\\1', $source_filefield['widget']['density']);
  $density_y = preg_replace('/^[0-9]+x([0-9]+)$/', '\\1', $source_filefield['widget']['density']);
  $density = "-density {$density_x}x{$density_y}";
  $path = pdf_to_imagefield_widget_file_path($source_filefield);

  // Adding [n] to the end of the pdf filename generates just the numbered page.
  $source = $pdf['filepath'] . "[{$page_number}]";
  $image_filename = $pdf['fid'] . '-' . $page_number . '.jpg';
  $image_filepath = $path . '/' . $image_filename;
  pdf_to_imagefield_convert_pdf($source, $image_filepath, array(), array(
    $density,
  ));

  // This will have created a single file named after the file id.
  if (file_exists($image_filepath)) {

    // Insert file into files table
    $image = pdf_to_imagefield_file_placeholder($image_filepath);
    drupal_write_record('files', $image);
    return $image;
  }

  // if no file, something must have failed. Too bad.
}