You are here

public function WkhtmltopdfController::generatePdf in wkhtmltopdf 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/WkhtmltopdfController.php \Drupal\wkhtmltopdf\Controller\WkhtmltopdfController::generatePdf()

Generate pdf file.

Return value

Symfony\Component\HttpFoundation\RedirectResponse Redirect

1 string reference to 'WkhtmltopdfController::generatePdf'
wkhtmltopdf.routing.yml in ./wkhtmltopdf.routing.yml
wkhtmltopdf.routing.yml

File

src/Controller/WkhtmltopdfController.php, line 49

Class

WkhtmltopdfController
Base for Controller.

Namespace

Drupal\wkhtmltopdf\Controller

Code

public function generatePdf() {
  $url = $this->request->query
    ->get('url');
  $binary = $this->settings
    ->get('wkhtmltopdf_bin');
  $path = $this->filesystem
    ->realpath('public://wkhtmltopdf');
  $parameters = '--javascript-delay 5000';
  $parameters .= $this->settings
    ->get('wkhtmltopdf_zoom') ? ' --zoom ' . $this->settings
    ->get('wkhtmltopdf_zoom') : '';
  $file_path = "public://wkhtmltopdf/" . urlencode($url) . '.pdf';
  if (!$this->filesystem
    ->prepareDirectory($file_path)) {
    $this->filesystem
      ->mkdir($path);
  }
  $filename = urlencode($url) . '.pdf';
  $file_path_physical = $path . '/' . $filename;
  $command = $binary . ' ' . $parameters . ' ' . $url . ' ' . $file_path_physical;
  $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__);
    }
  }
  $force_download = $this->settings
    ->get('wkhtmltopdf_download');
  if ($force_download) {
    $this
      ->download($file_path_physical, $filename);
    return new RedirectResponse($url);
  }
  $url_redirect = file_create_url($file_path);
  return new RedirectResponse($url_redirect);
}