1

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>
1
  • 2
    1) "I do not know if the above request mapping is right." use a REST Console/Postman/etc. to validate the server-side code, there's no much point proceeding until you do know that 2) you can't download a file via Ajax, search for "angularjs file download" on SO for alternative approaches. Commented Nov 17, 2015 at 23:14

1 Answer 1

1

You can use

headers.setContentType(MediaType.parseMediaType("application/pdf;charset=utf-8"));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.