You are here

protected function WkhtmltopdfController::createFile in wkhtmltopdf 2.0.x

Actually sends the shell command, and returns after the pdf file has been created

Parameters

string $url: The absolute url that we're generating a pdf of

Return value

string The drupal filepath in public://

Throws

\Symfony\Component\HttpKernel\Exception\HttpException If the wkhtmltopdf subdirectory can't be created in the public filesystem, just give up

1 call to WkhtmltopdfController::createFile()
WkhtmltopdfController::generatePdf in src/Controller/WkhtmltopdfController.php
Generate pdf file.

File

src/Controller/WkhtmltopdfController.php, line 127

Class

WkhtmltopdfController
A Controller to generate PDFs and return them as a binary reponse.

Namespace

Drupal\wkhtmltopdf\Controller

Code

protected function createFile($url) {
  $wkhtmltopdf_directory = 'public://wkhtmltopdf';
  if (!$this->filesystem
    ->prepareDirectory($wkhtmltopdf_directory, FileSystemInterface::CREATE_DIRECTORY)) {

    // folder could not be created
    $this->logger
      ->error('wkhtmltopdf subdirectory could not be created in the public filesystem');
    throw new HttpException(500, 'An error occurred generating the pdf.');
  }

  // create a likely to be unique filename for generating the pdf
  $filename = uniqid('wkhtmltopdf_', TRUE) . '.pdf';
  $drupal_file_path = $wkhtmltopdf_directory . '/' . $filename;
  $command = $this
    ->buildShellCommand($url, $drupal_file_path);
  $lock = \Drupal::lock();
  if ($lock
    ->acquire(__FILE__) !== FALSE) {
    shell_exec($command);
    $lock
      ->release(__FILE__);
  }
  else {
    while ($lock
      ->acquire(__FILE__) === FALSE) {
      $lock
        ->wait(__FILE__, 3);
    }
    if ($lock
      ->acquire(__FILE__) !== FALSE) {
      shell_exec($command);
      $lock
        ->release(__FILE__);
    }
  }
  return $drupal_file_path;
}