Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SpringMVC和angularjs返回生成的pdf_Angularjs_Spring_Spring Mvc_Pdf_Wkhtmltopdf - Fatal编程技术网

使用SpringMVC和angularjs返回生成的pdf

使用SpringMVC和angularjs返回生成的pdf,angularjs,spring,spring-mvc,pdf,wkhtmltopdf,Angularjs,Spring,Spring Mvc,Pdf,Wkhtmltopdf,我正试着做和你一样的事 但是我使用的是wkhtmltopdf,所以我对上面问题的答案做了一些修改,直到得到这样的答案 @RequestMapping(value = "/createhtml2", method = RequestMethod.POST) public ResponseEntity<byte[]> getPDF() throws IOException { try { ProcessBuilder pb = new ProcessBuil

我正试着做和你一样的事

但是我使用的是wkhtmltopdf,所以我对上面问题的答案做了一些修改,直到得到这样的答案

@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;
}
html页面

<div ng-controller="DemoCtrl as vm">

    <form ng-submit="vm.add()">

        <button  type="submit" class="btn btn-danger" >Create Html Page</button>

    </form>

</div>

创建Html页面
您可以使用

headers.setContentType(MediaType.parseMediaType("application/pdf;charset=utf-8"));
1) “我不知道上面的请求映射是否正确。”使用REST控制台/Postman/等来验证服务器端代码,除非您知道2)您无法通过Ajax下载文件,否则没有什么意义,请搜索“angularjs文件下载”等,以寻找替代方法。
headers.setContentType(MediaType.parseMediaType("application/pdf;charset=utf-8"));