You are here

function fillpdf_merge_perform_pdf_action in FillPDF 7

Figure out what to do with the PDF and do it.

Normally, this calls drupal_exit(). In error conditions, it returns void.

Parameters

object $fillpdf_object: Metadata object, usually generated by _fillpdf_build_options_object().

string $action: One of the following keywords: default, download, save, redirect. These correspond to performing the configured action (from admin/structure/fillpdf/%), sending the PDF to the user's browser, saving it to a file, and saving it to a file and then redirecting the user's browser to the saved file.

bool $force_download: If set, this function will always end the request by sending the filled PDF to the user's browser.

Here, specifically, it's for generating the filename of the handled PDF.

2 calls to fillpdf_merge_perform_pdf_action()
fillpdf_merge_pdf in ./fillpdf.module
Constructs a page and sends it to the browser or saves it.
fillpdf_rules_action_handle_default in ./fillpdf.rules.inc
Perform the default action on the PDF.

File

./fillpdf.module, line 1097

Code

function fillpdf_merge_perform_pdf_action($fillpdf_object, $action = 'download', $force_download = FALSE) {
  $pdf_info = $fillpdf_object->info;
  $token_objects = $fillpdf_object->token_objects;
  $pdf_data = $fillpdf_object->data;
  if (in_array($action, array(
    'default',
    'download',
    'save',
    'redirect',
  )) === FALSE) {

    // Do nothing if the function is called with an invalid action.
    return;
  }

  // If the PDF is empty, return.
  if (!$pdf_data) {
    return;
  }

  // Generate the filename of downloaded PDF from title of the PDF set in
  // admin/structure/fillpdf/%fid.
  $output_name = _fillpdf_process_filename($pdf_info->title, $token_objects);
  if ($action == 'default') {

    // Determine the default action, then re-set $action to that.
    if (empty($pdf_info->destination_path) === FALSE) {
      if ($pdf_info->destination_redirect) {
        $action = 'redirect';
      }
      else {
        $action = 'save';
      }
    }
    else {
      $action = 'download';
    }
  }

  // Initialize variable containing whether or not we send the user's browser to
  // the saved PDF after saving it (if we are).
  $redirect_to_file = FALSE;

  // Get a load of this switch...they all just fall through!
  switch ($action) {
    case 'redirect':
      $redirect_to_file = $pdf_info->destination_redirect;
    case 'save':
      fillpdf_action_save_to_file($fillpdf_object, $output_name, !$force_download, $redirect_to_file);
      if (drupal_is_cli()) {
        break;
      }

    // FillPDF classic!
    case 'download':
      drupal_add_http_header("Pragma", "public");
      drupal_add_http_header('Expires', 0);
      drupal_add_http_header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
      drupal_add_http_header('Content-type', 'application-download');

      // This must be strlen(), not drupal_strlen() because the length in bytes,
      // not in characters, is what is needed here.
      drupal_add_http_header('Content-Length', strlen($pdf_data));
      drupal_add_http_header('Content-disposition', 'attachment; filename="' . $output_name . '"');
      drupal_add_http_header('Content-Transfer-Encoding', 'binary');
      echo $pdf_data;
      drupal_exit();
      break;
  }
}