You are here

function fillpdf_merge_handle_pdf in FillPDF 7

Same name and namespace in other branches
  1. 7.2 fillpdf.module \fillpdf_merge_handle_pdf()

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

Parameters

object $pdf_info: An object containing the loaded record from {fillpdf_forms}.

string $pdf_data: A string containing the content of the merged PDF.

array $token_objects: An array|null of objects to be used in replacing tokens. Here, specifically, it's for generating the filename of the handled PDF.

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.

Throws

Exception

Deprecated

in fillpdf:7.x-1.10 and is removed from all future branches. Use fillpdf_merge_execute_pdf_action().

See also

https://www.drupal.org/project/fillpdf/issues/2538428

File

./fillpdf.deprecated.inc, line 34
Deprecated functions.

Code

function fillpdf_merge_handle_pdf($pdf_info, $pdf_data, $token_objects, $action = 'download', $force_download = FALSE) {
  if ($pdf_info->scheme === 'private') {
    throw new Exception('fillpdf_merge_handle_pdf() does not work properly with
    private files. Please use fillpdf_merge_execute_pdf_action() instead.');
  }
  if (in_array($action, array(
    'default',
    'download',
    'save',
    'redirect',
  )) === FALSE) {

    // Do nothing if the function is called with an invalid action.
    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) {
      $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_save_to_file($pdf_info, $pdf_data, $token_objects, $output_name, !$force_download, $redirect_to_file);

    // 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;
  }
}