Better code to output a pdf downloaded by the browser
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.