You are here

public function OutputHandler::savePdfToFile in FillPDF 5.0.x

Same name and namespace in other branches
  1. 8.4 src/OutputHandler.php \Drupal\fillpdf\OutputHandler::savePdfToFile()

Saves merged PDF data to the filesystem.

@todo: Rename 'token_objects' to 'entities' in FillPDF 5.x. Webform submissions are now entities, too.

Parameters

array $configuration: An array of configuration as originally passed from HandlePdfController::handlePopulatedPdf() to the FillPdfActionPlugin, containing the following properties: form: The FillPdfForm object from which the PDF was generated. context: The FillPDF request context as returned by FillPdfLinkManipulatorInterface::parseLink(). token_objects: The token data from which the PDF was generated. data: The populated PDF data itself. filename: The filename (not including path) with which the PDF should be presented.

string $destination_path_override: (optional) A destination path to override the one given by the FillPdfForm.

Return value

\Drupal\file\FileInterface|false The file entity, or FALSE on error.

Overrides OutputHandlerInterface::savePdfToFile

See also

\Drupal\fillpdf\FillPdfLinkManipulatorInterface::parseLink()

\Drupal\fillpdf\Plugin\FillPdfActionPlugin\FillPdfSaveAction::savePdf()

File

src/OutputHandler.php, line 71

Class

OutputHandler
Class OutputHandler.

Namespace

Drupal\fillpdf

Code

public function savePdfToFile(array $configuration, $destination_path_override = NULL) {

  /** @var \Drupal\fillpdf\Entity\FillPdfForm $fillpdf_form */
  $fillpdf_form = $configuration['form'];

  // @todo: Rename 'token_objects' to 'entities' in FillPDF 5.x. Webform
  // submissions are now entities, too.

  /** @var \Drupal\Core\Entity\EntityInterface[] $entities */
  $entities = $configuration['token_objects'];
  $destination_path = 'fillpdf';
  if (!empty($destination_path_override)) {
    $destination_path .= "/{$destination_path_override}";
  }
  elseif (!empty($fillpdf_form->destination_path->value)) {
    $destination_path .= "/{$fillpdf_form->destination_path->value}";
  }
  $resolved_destination_path = $this
    ->processDestinationPath(trim($destination_path), $entities, $fillpdf_form->scheme->value);
  $path_exists = $this->fileSystem
    ->prepareDirectory($resolved_destination_path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
  $saved_file = FALSE;
  if ($path_exists === FALSE) {
    $this->logger
      ->critical($this
      ->t("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.", [
      '%destination_path' => $resolved_destination_path,
    ]));
  }
  else {

    // Full steam ahead!
    $saved_file = file_save_data($configuration['data'], "{$resolved_destination_path}/{$configuration['filename']}", FileSystemInterface::EXISTS_RENAME);
    $this
      ->rememberFileContext($saved_file, $configuration['context']);
  }
  return $saved_file;
}