title: Create PDF and return it in ResponseEntity
body: <p>I'm trying to create a PDF and then download it automatically. I'm using PDFBox to create the PDF and it saves locally just fine but as soon as I return it via <code>ResponseEntity</code> or <code>byte[]</code> it becomes blank. I want to use a post because I want to send a body of parameters that I need for the PDF.</p>
  
  <p>Here's my controller</p>
  
  <pre><code>    @PostMapping(value="/documents/generate")
      ResponseEntity&lt;?&gt; generateSampleTag(@RequestBody SampleTag sampleTag) {
          log.info("inside generatePdfFromHtml method in DocumentController");
          try(ByteArrayOutputStream byteArrayOutputStream = freePdfService.generatePdf(sampleTag)) {
              HttpHeaders headers = new HttpHeaders();
              headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PDF_VALUE);
              headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=sampleTag.pdf");
              headers.add("Expires", "0");
              headers.setCacheControl(CacheControl.noCache());
              headers.add("Pragma", "public");
              ByteArrayResource resource = new ByteArrayResource(byteArrayOutputStream.toByteArray());
              return new ResponseEntity&lt;&gt;(resource, headers, HttpStatus.OK);
          } catch (Exception e) {
              log.error(e.getMessage());
              return new ResponseEntity&lt;&gt;(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
          }
  
      }
  </code></pre>
  
  <p>I've been trying a variety of input/output streams and header values and I'm just guessing at this point. Thanks for the help!</p>
  
  <p>I answered below but my issue was Swagger couldn't download it correctly. Postman worked.</p>