I can’t download from the response. I don’t know what I’m doing wrong.
I tried changing content-type to octet-stream, application/pdf, tried not using “produces”. If i write it locally instead of downloading, it generates the pdf perfectly. But if i’m trying to download from the response, it doesnt work.
I also tried in multiple browsers just to be sure and that’s definitely not the case.
Network tab on chrome shows that content-type and content-disposition are correct (at least correct like i’m asking it to be)
Is there something wrong with my code?
This is my Controller
@RestController
@RequestMapping("/api/pdf")
public class PdfController {
private final PdfService pdfService;
PdfController(PdfService pdfService) {
this.pdfService = pdfService;
}
@PostMapping(value = "/generate", produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<InputStreamResource> createPdf(@RequestBody PendenciasModel pendenciasModel) throws IOException {
try {
ByteArrayInputStream bis = pdfService.createPdf(pendenciasModel);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PDF_VALUE);
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+ pendenciasModel.getClient().getCpf() +".pdf");
return ResponseEntity.ok()
.body(new InputStreamResource(bis));
}catch(IOException e){
e.printStackTrace();
return ResponseEntity.badRequest().body(null);
}
}
}