Why isn't this functionality in submitForm firing? I'm in the process of writing a module. The module works very well. In the module, I generate a form which consists of one submit button, and in the .module file, I'm calling that form and the submitForm works too. I was asked to move that button into a block, so I did. submitForm still works, but the main functionality doesn't. How come?

&lt;?php

namespace Drupal\my_module\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
* Generate button.
*/
class myModuleForm extends FormBase {

  /**
  * {@inheritdoc}.
  */
  public function getFormId() {
    return 'my_module_form';
  }

  /**
  * {@inheritdoc}.
  */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['my_module_btn'] = [
      '#type' =&gt; 'submit',
      '#value' =&gt; 'Do something'        ];
    return $form;
  }

  /**
  * {@inheritdoc}.
  */
  public function validateForm(array &amp;$form, FormStateInterface $form_state) {
  }

  /**
  * {@inheritdoc}.
  */
  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    $mpdf = new \Mpdf\Mpdf(['tempDir' =&gt; 'sites/default/files/tmp', 'default_font' =&gt; 'roboto', 'margin_left' =&gt; 6, 'margin_top' =&gt; 6, 'margin_right' =&gt; 6, 'margin_bottom' =&gt; 11]);
    ...
    ...
    ...
    $mpdf-&gt;Output($title."-".date("Ymd-His").".pdf", 'D');
  }
}
I googled the Mpdf library and it supports a string output. So you can use the string output to build a response and return it via $form_state:

use Symfony\Component\HttpFoundation\Response;

  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    $content = '%PDF-1.3...';
    $headers = [
      'Content-Type' =&gt; 'application/pdf',
    ];
    $response = new Response($content, 200, $headers);
    $form_state-&gt;setResponse($response);
  }