2

I'm trying to open a PDF file with print dialog in IE Edge, it works fine in chrome but not in IE

MVC code to return file using Evo Pdf tool:

var restClient = new RestClient(Request.Url.Scheme + "://" + Request.Url.Authority);
var restResponse = restClient.Execute(request);
if (restResponse.StatusCode == HttpStatusCode.OK)
 {
     htmlModel.HtmlString = restResponse.Content;
     byte[] pdfBytes = PdfUtil.GetEvoPdfBytes(htmlModel);
     if (pdfBytes != null)
        {
             return File(pdfBytes, System.Net.Mime.MediaTypeNames.Application.Pdf, htmlModel.PdfName + ".pdf");
        }
 }

Javascript code to open file with print dialog, below code works in chrome but not IE:

var req = new XMLHttpRequest();
        req.open("POST", "/api/HtmlToPdf", true);
        req.setRequestHeader("Content-Type", "application/json");
        req.responseType = "blob";

        req.onload = function (event) {
            var blob = req.response;
            console.log(blob.size);
            var lin = window.URL.createObjectURL(blob);
            // Works in chrome
            var mywindow = window.open(lin, "_blank");
            mywindow.focus();
            mywindow.print();
        };
        req.send(JSON.stringify(
            {
                htmlModel: {
                    ElementSelector: "#div",
                    PageOrientation: "Portrait",
                    PdfName: "abc"
                }
            }));
Abhishek Jain
  • 171
  • 1
  • 13

1 Answers1

0

IE11 does not support URL.createObjectURL(). So your code will not work for IE browser and you will not be able to open the blob with print dialog..

As a work around, you need to use msSaveBlob or msSaveOrOpenBlob for Internet Explorer browser.

These methods allow a user to save the file on the client as if the file had been downloaded from the Internet.

var blobObject = new Blob(["This is sample text..."]);

window.navigator.msSaveOrOpenBlob (blobObject, 'msSaveOrOpenBlob_testFile.txt');  

References:

(1) Download a blob from HTTP URL in IE 11

(2) Saving files locally using Blob and msSaveBlob

(3) Blob download is not working in IE

Deepak-MSFT
  • 10,379
  • 1
  • 12
  • 19