Better code to output a pdf downloaded by the browser In a submit function of a custom form, I would like to output a pdf which can be downloaded by the user/browser when clicking on 'Get PDF'.
The following code is doing the job only once, because after clicking on the 'Get PDF' button, all the buttons of the form does not work anymore. Clicking on any of them doesn't produce any effect.
Any Idea?
  static public function submitPdf(array &amp;$form, FormStateInterface $form_state) {            
    if ($pdf_content = my_function_to_output_a_pdf_as_string()) {            
      $response = new Response($pdf_content);            
      $disposition = $response-&gt;headers-&gt;makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'my_pdf.pdf');            
      $response-&gt;headers-&gt;set('Content-type', &quot;application/pdf&quot;);            
      $response-&gt;headers-&gt;set('Content-Disposition', $disposition);            
      //$response-&gt;headers-&gt;set('Content-Transfer-Encoding', 'binary');            
      $response-&gt;headers-&gt;set('Content-length', strlen($pdf_content));            
      $form_state-&gt;setResponse($response);  
      //Thanks to kiamlaluno, it is working slighty better if I remove the following line (setRebuild())          
      //$form_state-&gt;setRebuild(TRUE);            
    }            
  }            

Edit to add in order to provide more context: The form is made of +/- 30 numeric fields and the user can play with them to produce some calculated results. If he wants, he can click at any time on the &quot;get pdf&quot; button to generate a pdf which will contains all his inputs and the calculated result. In other words, he can fill the form-&gt;get the pdf-&gt;continue to fill-&gt;get another pdf-&gt; etc
Provide a controller to download the PDF as discussed in the comments:
https://stefvanlooveren.me/blog/serve-pdf-download-controller-drupal-8

&lt;?php
namespace Drupal\MYMODULE\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

/**
 * Class PdfGeneratorController.
 */
class PdfGeneratorController extends ControllerBase {

  public function Render($uuid) {

    // In my case, I render a file by UUID
    // This actually does not matter too much here
    // String below contains the url of the file
    $uri = 'private://'.$uuid.'.pdf';

    $headers = array(
      'Content-Type'     =&gt; 'application/pdf',
      'Content-Disposition' =&gt; 'attachment;filename=&quot;download&quot;');

    return new BinaryFileResponse($uri, 200, $headers, true);
  }
}


Then add to the button link the HTML5 Download Attribute to download the file in the background without redirecting.
See https://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request
If the PDF file is created dynamically this can be integrated in a Drupal Ajax form workflow by generating the file on user request and providing the button link to the custom built PDF file via Ajax.