public function PdfFormat::getResponse in Printer and PDF versions for Drupal 8+ 2.x
Same name and namespace in other branches
- 8 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 317
Class
- PdfFormat
- Provides a plugin to display a PDF version of a page.
Namespace
Drupal\printable_pdf\Plugin\PrintableFormatCode
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();
$basepath = \Drupal::request()
->getBasePath();
if ($basepath) {
$raw_content = str_replace('src="' . $basepath, 'src=', $raw_content);
}
$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';
$public_dir = $this->publicStream
->getDirectoryPath();
$pdf_location = trim($public_dir, '/') . '/' . substr($pdf_location, 1);
}
else {
// @TODO: A token per source entity type? Seems overkill so I'll wait
// until requested.
$token_service = \Drupal::token();
$url_parts = explode('/', $this->pathCurrent
->getPath());
$entity = \Drupal::routeMatch()
->getParameter('entity');
$entity_type = count($url_parts) > 2 ? $url_parts[1] : NULL;
$pdf_location = $token_service
->replace($pdf_location, [
$entity_type => $entity,
]);
}
$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);
}