I'm trying to do the same as in Return generated pdf using spring MVC
But i'm using wkhtmltopdf So i modified the answer of the above question a bit until i get something like this.
@RequestMapping(value = "/createhtml2", method = RequestMethod.POST)
public ResponseEntity<byte[]> getPDF() throws IOException {
try {
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", " cd C:\\Program Files\\wkhtmltopdf\\bin && wkhtmltopdf.exe "
+ "http://google.com C:\\test\\Google.pdf");
pb.start();
} catch (Exception e) {
System.out.println(e);
}
Path pdfPath = Paths.get("C:\\test\\Google.pdf");
byte[] pdf = Files.readAllBytes(pdfPath);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
String filename = "output.pdf";
headers.setContentDispositionFormData(filename, filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(pdf, headers, HttpStatus.OK);
return response;
}
I do not know if the above request mapping is right. But at the moment the front end and the back end does not throw any error's. But how can i now let the user download this pdf file ?
This is the controller
(function () {
'use strict';
angular
.module('app')
.controller('DemoCtrl', DemoCtrl);
DemoCtrl.$inject = ['$scope', '$http', '$localStorage' ];
function DemoCtrl($scope, $http,$localStorage) {
var vm = this;
vm.add = add;
function add() {
$http.post('/api/createhtml2');
console.log("Create html page")
}
}
})();
The html page
<div ng-controller="DemoCtrl as vm">
<form ng-submit="vm.add()">
<button type="submit" class="btn btn-danger" >Create Html Page</button>
</form>
</div>
You can use
headers.setContentType(MediaType.parseMediaType("application/pdf;charset=utf-8"));