in a custom module, I created a custom form. On this form, I have an ajax button to open a specific file (.pdf).

Here is my code in a custom submit function :

  $url = 'myfileUrl';
  $response = new AjaxResponse();
  $command = new RedirectCommand($url);

It's ok and the file is opened when I click on the button. But, the page is closed to open the file. How can I specify an attribute "target" to open the file in a new window? If not possible, is there another ajax command to open an external URL?


  • @kevin, I don't see a clear response in your link. Can you explain me if I can use a target attribute in a RedirectCommand ajax? e.g. $command = newRedirectCommand($url, 'target=_blank', or newOpenUrl...). Thanks. – user28220 Aug 16 '19 at 18:54
  • According to RedirectCommand Documentation RedirectCommand set window.location , and it's not possible to redirect with target _blank with window.location, only with window.open(url,'_blank'); can redirect to new window, see the answer in the link commented by @kevin in the first comment. – berramou Aug 17 '19 at 17:58
  • @berramou, I understand the behavior of the RedirectCommand function. But, what should I use to open a new window (and open a file) if I click on a button? I tried to use the OpenDialogCommand without sucess. Or, should I use a custom submit and forget the ajax functions? Thanks. – user28220 Aug 19 '19 at 15:51

You can open a new window which can be achieved using javascript. Call function like below using on click method. You have to pass the URL to open and title for the window.

    function openWindow(url, title){
      var left = (screen.width/2)-(700/2);
      var top = (screen.height/2)-(500/2);
      var win = window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=700, height=500, top='+top+', left='+left);
      //setTimeout(function () { win.close(); }, 10000); <- You can give timeout also so window will get automatically closed

Hope this will help to open your pdf file in new window, I will post another answer if I found to open a new tab on redirect.

| improve this answer | |

I found a solution to resolve my problem. After having readed several posts, i have decided to create a custom Ajax Command to open my files (pdf/csv).

Create a custom command - link

In my custom module, I have built the required files for this and call in my CallBack function this new ajax command ('OpenFileCommand') :

    $ajax_response = new AjaxResponse();
    $command = new OpenFileCommand($file_url);

Here is the simple code for this command:

use Drupal\Core\Ajax\CommandInterface;

 * Class OpenFileCommand.
class OpenFileCommand implements CommandInterface {

  // Create a new property.    
  protected $file_url;

  // Construct the File Url property.
  public function __construct($file_url) {
    $this->file_url = $file_url;

  * Render custom ajax command.
  * @return ajax command function
  public function render() {
    return [
      'command' => 'OpenFileCommand',
      'file_url' => $this->file_url


Now, I can open a file when I click on a ajax button.

Thanks for all for your help. This solution can help someone...

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.