title: Display PDF in a webpage
body: <p>I'm using Spring MVC, <b>I want to display PDF file from my local to webpage.</b> I don't know what should I do to my controller to do this. I see some similar problem with an answer that returning a <code>ResponseEntity&lt;byte[]&gt;</code> like in this <a href="https://stackoverflow.com/questions/16652760/return-generated-pdf-using-spring-mvc">question</a> but this one is for <code>ajax</code> if I'm not mistaken, this is not my requirement.</p>
  
  <p><b>Update:</b></p>
  
  <p>This is what I tried so far:</p>
  
  <pre><code>&lt;a href="#" onClick="test();" &gt;test&lt;/a&gt;
  
  function test(){
        $.ajax({
      type: "POST",
      url: "../admin/module/id.do",
      data: '{ file_id }',
      success: function(response){
             alert(response);
               }
        });
  }
  </code></pre>
  
  <p>And the controller:</p>
  
  <pre><code>@RequestMapping( value = "/admin/module/id", method = RequestMethod.POST )
      public ResponseEntity&lt;byte[]&gt; getPDF( @PathVariable( "id" )
      int id, Model model )
      {
          System.out.println( "test" );
          Path path = Paths.get( "C:/Users/FORSAK~1/AppData/Local/Temp/spring_tutorial.pdf" );
          byte[] contents = null;
          try
          {
              contents = Files.readAllBytes( path );
          }
          catch( IOException e )
          {
  
              e.printStackTrace();
          }
          HttpHeaders headers = new HttpHeaders();
          headers.setContentType( MediaType.parseMediaType( "application/pdf" ) );
          String filename = "spring_tutorial.pdf";
          headers.setContentDispositionFormData( filename, filename );
          ResponseEntity&lt;byte[]&gt; response = new ResponseEntity&lt;byte[]&gt;( contents, headers, HttpStatus.OK );
          return response;
      }
  </code></pre>
  
  <p>the <code>alert(response)</code> doesn't work and also the <code>System.out.println( "test" );</code> </p>
  
  <p>The error from the firebug is <code>"NetworkError: 500 Internal Server Error - http://localhost:8080/ThesisProject/admin/module/id.do"</code></p>
  
  <p>Stacktrace:</p>
  
  <pre><code>java.lang.IllegalStateException: Could not find @PathVariable [id] in @RequestMapping
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.resolvePathVariable(AnnotationMethodHandlerAdapter.java:857)
      at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolvePathVariable(HandlerMethodInvoker.java:710)
      at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:360)
      at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:444)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:432)
      ....
  </code></pre>