You are here

function fillpdf_action_save_to_file in FillPDF 7

Save a PDF to a file.

_to_file

Parameters

object $fillpdf_object:

string $output_name:

bool $redirect:

string|null $destination_path_override: Allows overriding the destination directory for the PDF. Do not include the scheme in front. SECURITY WARNING: If you do not place the file under the private://fillpdf directory and you're using private files, access checking WILL NOT BE PERFORMED on your generated file! You will have to implement hook_file_download() yourself. See fillpdf_file_download() for code off which you can base your function.

Return value

false|object Nothing (if redirected), a file object (if saving the file succeeded), or FALSE (if it didn't).

2 calls to fillpdf_action_save_to_file()
FillPdfTestCase::testFileAccess in tests/FillPdfTestCase.test
Make sure that file access works properly.
fillpdf_merge_perform_pdf_action in ./fillpdf.module
Figure out what to do with the PDF and do it.

File

./fillpdf.module, line 1181

Code

function fillpdf_action_save_to_file($fillpdf_object, $output_name, $redirect = TRUE, $redirect_to_file = FALSE, $destination_path_override = NULL) {
  $pdf_info = $fillpdf_object->info;
  $token_objects = $fillpdf_object->token_objects;
  $pdf_data = $fillpdf_object->data;
  if (isset($destination_path_override) && empty($destination_path_override) === FALSE) {
    $destination_path = $destination_path_override;
  }
  elseif (empty($pdf_info->destination_path) && empty($destination_path_override)) {

    // If this function is called and the PDF isn't set up with a destination
    // path, give it one.
    $destination_path = 'fillpdf';
  }
  else {
    $destination_path = $pdf_info->destination_path;

    // Prepend the destination path with the fillpdf directory if the user is
    // using private files. When the caller overrides $destination_path, they're
    // on their own. Private file support WILL NOT WORK PROPERLY!
    if ($pdf_info->scheme === 'private') {
      $destination_path = "fillpdf/{$destination_path}";
    }
  }
  $resolved_destination_path = _fillpdf_process_destination_path($destination_path, $token_objects, $pdf_info->scheme);
  $path_exists = file_prepare_directory($resolved_destination_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  $saved_file = FALSE;
  if ($path_exists === FALSE) {
    watchdog('fillpdf', "The path %destination_path does not exist and could not be\n      automatically created. Therefore, the previous submission was not saved. If\n      the URL contained download=1, then the PDF was still sent to the user's browser.\n      If you were redirecting them to the PDF, they were sent to the homepage instead.\n      If the destination path looks wrong and you have used tokens, check that you have\n      used the correct token and that it is available to FillPDF at the time of PDF\n      generation.", array(
      '%destination_path' => $resolved_destination_path,
    ));
  }
  else {

    // Full steam ahead!
    $saved_file = file_save_data($pdf_data, "{$resolved_destination_path}/{$output_name}", FILE_EXISTS_RENAME);
    fillpdf_file_usage_add($saved_file, $fillpdf_object);
    if ($redirect === TRUE) {
      if (isset($_GET['destination']) === FALSE) {

        // Should we send the user directly to the saved PDF? If so, do that.
        if ($redirect_to_file) {
          drupal_goto(file_create_url($saved_file->uri));
        }
      }
    }
  }
  if ($redirect === TRUE && !drupal_is_cli()) {

    // Allow the "destination" query string parameter to be used
    // for example, fillpdf?nid=1&fid=1&destination=node/1
    // If no destination is provided, drupal_goto() will send the
    // user to the front page.
    drupal_goto();
  }
  return $saved_file;
}