OutputHandler.php in FillPDF 5.0.x
File
src/OutputHandler.php
View source
<?php
namespace Drupal\fillpdf;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\file\FileInterface;
use Drupal\fillpdf\Component\Utility\FillPdf;
use Drupal\fillpdf\Entity\FillPdfFileContext;
use Psr\Log\LoggerInterface;
use Drupal\Core\File\FileSystemInterface;
class OutputHandler implements OutputHandlerInterface {
use StringTranslationTrait;
protected $tokenResolver;
protected $logger;
protected $linkManipulator;
protected $fileSystem;
public function __construct(TokenResolverInterface $token_resolver, LoggerInterface $logger, FillPdfLinkManipulatorInterface $link_manipulator, FileSystemInterface $file_system) {
$this->tokenResolver = $token_resolver;
$this->logger = $logger;
$this->linkManipulator = $link_manipulator;
$this->fileSystem = $file_system;
}
public function savePdfToFile(array $configuration, $destination_path_override = NULL) {
$fillpdf_form = $configuration['form'];
$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 {
$saved_file = file_save_data($configuration['data'], "{$resolved_destination_path}/{$configuration['filename']}", FileSystemInterface::EXISTS_RENAME);
$this
->rememberFileContext($saved_file, $configuration['context']);
}
return $saved_file;
}
protected function processDestinationPath($destination_path, array $entities, $scheme = 'public') {
$destination_path = (string) $this->tokenResolver
->replace($destination_path, $entities, [
'content' => 'text',
]);
return FillPdf::buildFileUri($scheme, $destination_path);
}
protected function rememberFileContext(FileInterface $fillpdf_file, array $context) {
$fillpdf_link = $this->linkManipulator
->generateLink($context);
$fillpdf_file_context = FillPdfFileContext::create([
'file' => $fillpdf_file,
'context' => $fillpdf_link
->toUriString(),
]);
$fillpdf_file_context
->save();
}
}