You are here

public function GeneratePdf::generate in PDF using mPDF 8

Same name and namespace in other branches
  1. 8.2 src/Controller/GeneratePdf.php \Drupal\pdf_using_mpdf\Controller\GeneratePdf::generate()

Generate a PDF file of a node.

Parameters

\Drupal\node\NodeInterface $node: The node object.

Return value

array Response headers.

Throws

\Exception

1 string reference to 'GeneratePdf::generate'
pdf_using_mpdf.routing.yml in ./pdf_using_mpdf.routing.yml
pdf_using_mpdf.routing.yml

File

src/Controller/GeneratePdf.php, line 130

Class

GeneratePdf
Class GeneratePdf.

Namespace

Drupal\pdf_using_mpdf\Controller

Code

public function generate(NodeInterface $node) {
  $build = $this->entityTypeManager
    ->getViewBuilder('node')
    ->view($node, 'pdf_using_mpdf');
  $fileContent = $this->convert
    ->convertToString($this->renderer
    ->render($build));
  $response = new Response($fileContent);
  $disposition = $response->headers
    ->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $this->convert
    ->getFilename());
  $response->headers
    ->set('Content-Disposition', $disposition);
  $response->headers
    ->set('Content-type', 'application/pdf');
  return $response;
}