title: Open ResponseEntity PDF in new browser tab
body: <p>I came across a helpful PDF generation code to show the file to the client in a Spring MVC application (<a href="https://stackoverflow.com/a/16656382/3287503">"<em>Return generated PDF using Spring MVC</em>"</a>):</p>
  
  <pre><code>@RequestMapping(value = "/form/pdf", produces = "application/pdf")
  public ResponseEntity&lt;byte[]&gt; showPdf(DomainModel domain, ModelMap model) {
      createPdf(domain, model);
  
      Path path = Paths.get(PATH_FILE);
      byte[] pdfContents = null;
      try {
          pdfContents = Files.readAllBytes(path);
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.parseMediaType("application/pdf"));
      String filename = NAME_PDF;
      headers.setContentDispositionFormData(filename, filename);
      headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
      ResponseEntity&lt;byte[]&gt; response = new ResponseEntity&lt;byte[]&gt;(
              pdfContents, headers, HttpStatus.OK);
      return response;
  }
  </code></pre>
  
  <p>I added a declaration that the method returns a PDF file (<a href="https://stackoverflow.com/a/11804233/3287503">"<em>Spring 3.0 Java REST return PDF document</em>"</a>): <code>produces = "application/pdf"</code>.</p>
  
  <p>My problem is that when the code above is executed, it immediately asks the client to save the PDF file. I want the PDF file to be viewed first in the browser so that the client can decide whether to save it or not.</p>
  
  <p>I found <a href="https://stackoverflow.com/a/9401602/3287503">"<em>How to get PDF content (served from a Spring MVC controller method) to appear in a new window</em>"</a> that suggests to add <code>target="_blank"</code> in the Spring form tag. I tested it and as expected, it showed a new tab but the save prompt appeared again.</p>
  
  <p>Another is <a href="https://stackoverflow.com/a/5040830/3287503">"<em>I can't open a .pdf in my browser by Java</em>"</a>'s method to add <code>httpServletResponse.setHeader("Content-Disposition",  "inline");</code> but I don't use <code>HttpServletRequest</code> to serve my PDF file.</p>
  
  <p>How can I open the PDF file in a new tab given my code/situation?</p>