简体   繁体   English

在Internet Explorer中从二进制文件打开PDF

[英]Open PDF from binary in Internet Explorer

Related to Need to open blob/PDF in IE window and Setting window.location or window.open in AngularJS gives "access is denied" in IE 11 . 需要在IE窗口中打开blob / PDF在AngularJS中设置window.location或window.open有关,在IE 11中会给出“拒绝访问”的信息

I need to open a PDF which comes from a byte array in my service: 我需要打开来自服务中字节数组的PDF:

@RequestMapping("/{dokumentId}")
public ResponseEntity<byte[]> getDoc(@PathVariable Integer docId, HttpServletRequest request, HttpServletResponse response) throws IOException {       
    byte[] contents = // .. get document 

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    headers.add("Content-Disposition", "inline;filename=file");
    headers.add("Cache-Control","private, must-revalidate, post-check=0, pre-check=0, max-age=1");
    headers.add("Pragma", "no-cache");
    headers.setContentLength(contents.length);

    return new ResponseEntity<>(contents, headers, HttpStatus.OK);
}

which I'm trying to open in Internet Explorer 11 with the following JS: 我正在尝试使用以下JS在Internet Explorer 11中打开它:

$scope.openPdf = function(doc){
    var winlogicalname = "document_" + doc.docId;
    var winparams = 'dependent=yes,locationbar=no,scrollbars=yes,menubar=yes,resizable,screenX=50,screenY=50,width=850,height=1050';

    var ieEDGE = navigator.userAgent.match(/Edge/g);
    var ie = navigator.userAgent.match(/.NET/g); // IE 11+
    var oldIE = navigator.userAgent.match(/MSIE/g); 

    if(!ie && !oldIE && !ieEDGE){
        // Open PDF in new browser window
        var detailWindow = window.open("", winlogicalname, winparams);
    }

    xxxService.getDoc(doc).success(function(data){
        //for browser compatibility  
        if (ie || oldIE || ieEDGE) {
            var blob = new window.Blob([data], { type: 'application/pdf' });    
            window.navigator.msSaveOrOpenBlob(blob);
        } else {
            var binaryAsString = ArrayBufferToString(data).replace(/"/g,"");
            var pdfAsDataUri = "data:application/pdf;base64," + binaryAsString;
            var htmlText = '<embed width=100% height=100% type="application/pdf" src="'+ pdfAsDataUri + '"></embed>'; 
            detailWindow.document.write(htmlText);
            detailWindow.document.close();
        }   
    }).error(function(data, status, headers, config){
        console.log(data, status, headers, config);
    });
};

This works in Chrome and Firefox (opens in new window, constructed directly from binary with <embed> . However, this way does not seem to work in IE, hence msSaveOrOpenBlob . But when saving/opening the PDF in IE, I get an error in Adobe Reader: " Adobe Acrobat Reader DC could not open 'file.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded). ". When I try to drag and drop the PDF in to IE, it says " File does not begin with '%PDF-'. ". 这在Chrome和Firefox中有效(在新窗口中打开,直接使用<embed>从二进制构造。)但是,这种方法在IE中似乎不起作用,因此msSaveOrOpenBlob 。但是在IE中保存/打开PDF时,出现错误在Adobe Reader中:“ Adobe Acrobat Reader DC could not open 'file.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).Adobe Acrobat Reader DC could not open 'file.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded). “。当我尝试将PDF拖放到IE时,它说“ File does not begin with '%PDF-'. ”。

Any ideas on how to open a PDF in IE11 using either byte array directly or constructing a Blob ? 关于如何直接使用字节数组或构造Blob在IE11中打开PDF的任何想法?

I changed my web service to this: 我将我的网络服务更改为:

byte[] contents = // get contents

response.getOutputStream().write(contents);
response.setContentType("application/pdf");
response.setContentLength(contents.length);
response.addHeader("Content-Disposition", "inline;filename="+filename);
response.addHeader("Cache-Control", "private, must-revalidate, post-check=0, pre-check=0, max-age=1");
response.addHeader("Pragma", "public");

and then consumed on client side with: 然后在客户端使用:

Service.getDocument(docId).success(function(data){
    var file = new Blob([data], {type: 'application/pdf'});
    if(window.navigator.msSaveOrOpenBlob){
        window.navigator.msSaveOrOpenBlob(file, name + ".pdf");
    } else {
        var fileURL = URL.createObjectURL(file);
        $scope.renderPDF(fileURL, detailWindow.document.getElementById('canvas'));
    } 
}

where $scope.renderPDF uses PDFJS to render the PDF in a new window. 其中$scope.renderPDF使用PDFJS在新窗口中呈现PDF。 Remember to set {responseType:'arraybuffer'}) on the $http.get (or post ) method. 请记住在$http.get (或post )方法上设置{responseType:'arraybuffer'})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从二进制字符串或base64在Internet Explorer 11中显示嵌入的PDF - Display embedded PDF in Internet Explorer 11 from binary string or base64 从Intranet上的Internet Explorer打开Word文档 - Open Word document from Internet Explorer on intranet 弹出以打开pdf在Internet Explorer上不起作用 - pop up to open pdf does not work on Internet explorer 使用“运行方式”运行Internet Explorer无法打开新的PDF窗口 - Running Internet Explorer with “Run as” fails to open a new PDF window Internet Explorer PDF阅读器 - Internet Explorer PDF Reader 从Internet Explorer中的javascript打印方法设置pdf的文件名不起作用 - Setting filename of pdf from javascript print method in Internet Explorer not working 在Internet Explorer浏览器中从角度Blob打开PDF - Opening PDF from angular blob in Internet explorer browser VBA / Internet Explorer / Javascript:如何从同一Internet Explorer打开JavaScript窗口? - VBA / Internet Explorer / Javascript: How to open a javascript window from same internet explorer? 在Internet Explorer中PDF导出打印 - PDF export printing in Internet Explorer 从 Java 客户端服务器应用程序打开本地 Internet Explorer - Open Local Internet explorer from Java Client Server Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM