1

I need to print a PDF file which I obtain through a GET request in JavaScript. In order to do the actual printing I use PrintJS, which can be used to print from a specific PDF URL.

My code looks something like this:

printChart() {
  var req = new XMLHttpRequest();
  req.open('GET', 'http://localhost:8080/test.pdf', true);
  req.responseType = 'blob';

  req.onload = function (event) {
    var blob = req.response;
    var blobURL = window.URL.createObjectURL(blob);

    printJS(blobURL);
  };

  req.send();
}

This works fine in Chrome, but the problem is it does not print anything in Internet Explorer (tested in IE 11). Apparently, window.URL.createObjectURL does not work properly in IE.

Although there is a msSaveOrOpenBlob method in IE but this doesn't help me very much, since I need to print the PDF, not to save it.

crabbly
  • 5,106
  • 1
  • 23
  • 46
Tudor Ciotlos
  • 1,805
  • 4
  • 29
  • 47
  • Try googling for "createObjectURL polyfill" – George Kagan Nov 07 '16 at 13:27
  • Is there any reason not to simply hand the printJS function the original url, `http://localhost:8080/test.pdf`. I mean, it sounds odd to get a file from a url, turn it into a blob then hand that to a function expecting a url. – enhzflep Nov 07 '16 at 14:45
  • @enhzflep Yes, you have a point, but the thing is I will not print a static resource like in the example above. I will receive a generated PDF file from the backend through an AJAX request (I don't know specific implementation details yet). The file I receive will probably be available as a Blob. – Tudor Ciotlos Nov 07 '16 at 14:50
  • try open it as dataUrl (base64) – Endless Nov 07 '16 at 15:45

0 Answers0