You are here

public function PdfFormat::getResponse in Printer and PDF versions for Drupal 8+ 8

Same name and namespace in other branches
  1. 2.x modules/printable_pdf/src/Plugin/PrintableFormat/PdfFormat.php \Drupal\printable_pdf\Plugin\PrintableFormat\PdfFormat::getResponse()

Returns the response object for this format plugin.

Return value

\Symfony\Component\HttpFoundation\Response The response object.

Overrides PrintableFormatBase::getResponse

File

modules/printable_pdf/src/Plugin/PrintableFormat/PdfFormat.php, line 280

Class

PdfFormat
Provides a plugin to display a PDF version of a page.

Namespace

Drupal\printable_pdf\Plugin\PrintableFormat

Code

public function getResponse() {
  $paper_size = (string) $this->configFactory
    ->get('printable.settings')
    ->get('paper_size');
  $paper_orientation = $this->configFactory
    ->get('printable.settings')
    ->get('page_orientation');
  $path_to_binary = $this->configFactory
    ->get('printable.settings')
    ->get('path_to_binary');
  $save_pdf = $this->configFactory
    ->get('printable.settings')
    ->get('save_pdf');
  $pdf_location = $this->configFactory
    ->get('printable.settings')
    ->get('pdf_location');
  $raw_content = $this
    ->buildPdfContent();
  $pdf_content = $this
    ->removeImageTokens($raw_content);
  $footer_content = $this
    ->getFooterContent();
  $header_content = $this
    ->getHeaderContent();

  // $this->formattedHeaderFooter();
  $this->pdfGenerator
    ->setter($pdf_content, $pdf_location, $save_pdf, $paper_orientation, $paper_size, $footer_content, $header_content, $path_to_binary);
  if (empty($pdf_location)) {
    $pdf_location = str_replace("/", "_", $this->pathCurrent
      ->getPath()) . '.pdf';
    $pdf_location = substr($pdf_location, 1);
  }
  $this->filename = DRUPAL_ROOT . '/' . $pdf_location;
  $this->pdfGenerator
    ->save($this->filename);
  if ($this->pdfGenerator
    ->displayErrors()) {
    $source_url = $this->requestStack
      ->getCurrentRequest()
      ->getRequestUri();
    $pos = strpos($source_url, "printable");
    $source_url = substr($source_url, 0, $pos - 1);
    return new RedirectResponse($source_url);
  }
  return (new BinaryFileResponse($this->filename, 200, $this
    ->getHeaders($pdf_location, $save_pdf)))
    ->deleteFileAfterSend(true);
}