How do I pass variables to a view builder's renderer? I'm creating a module that will render a few nodes before getting back the result and print it as PDF. (I can't use wkhtmltopdf; I wish I could.)

So far, I managed to make my controller/action use the template I wanted, and anything I write in the template file is showed with die($html). However, I can't find any example of how to pass variables to the view; I tried a few things, but I got nothing, excepted 500 errors.

This is the code I am using.

public function nodeAction(NodeInterface $node) {
  // Here, I define a variable named $responses that contains a lot of datas as arrays
  $view_builder = \Drupal::entityTypeManager()-&gt;getViewBuilder('node');
  $build = $view_builder-&gt;view($node, 'cnas_pdf');
  $html = \Drupal::service('renderer')-&gt;renderPlain($build, ['var' =&gt; $response]);

  // No error in this configuration, but "var" is nowhere to be found
  die($html);
}
This is not possible to add parameters to renderPlain(). What you can do depends. If you want to use a variable inside of the node, then do this in a node preprocess hook. But more likely you want to add additional elements to the build array before you render it, for example:

$build = [];
$build['top'] = ['#markup' =&gt; 'abc'];
$build['node'] = $view_builder-&gt;view($node, 'cnas_pdf');
$build['footer'] = ['#markup' =&gt; 'xyz'];