Lorsque l'utilisateur cliquera sur un bouton, il y aura appel sur le serveur qui génèrera un PDF.
Le fichier ouvrira dans un autre onglet.
$("#memberContractPrintReport").on('click', function (e) {
openDocument(getHostName() + "/report/document/contracts");
});
function openDocument(urlServer) {
var win = window.open('_blank');
downloadFile(urlServer, function (blob) {
var url = URL.createObjectURL(blob);
win.location = url;
});
}
function downloadFile(url, success) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.setRequestHeader("Authorization", "Basic " + $.cookie('norcoption-authorization'));
xhr.responseType = "blob";
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (success)
success(xhr.response);
}
};
xhr.send(null);
}
Maintenant la partie Java
@GetMapping(value = "/report/document/contracts", produces = "application/pdf")
public ResponseEntity<byte[]> getContractReport() throws IOException {
byte[] content = reportService.generateContractReport();
return prepareReport(content);
}
private ResponseEntity<byte[]> prepareReport(byte[] content) throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
String filename = "report.pdf";
headers.setContentDispositionFormData(filename, filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(content, headers, HttpStatus.OK);
return response;
}
Aucun commentaire:
Enregistrer un commentaire