简体   繁体   English

在新的浏览器标签中打开pdf内容

[英]Open a pdf content in a new browser tab

I have a GET Ajax Request, that returns me a content, coded in application/pdf form. 我有一个GET Ajax请求,它以应用程序/ pdf格式返回给我一个内容。 For example, my requests returns a string of this type: 例如,我的请求返回此类型的字符串:

%PDF-1.4 % 4 0 obj <>stream x endstream endobj 6 0 obj <>stream x ]o 0 + e{Q $ R N @- n wB?L$.JDL| >獝 [ >{ =?_ʻ (寒 1`ؚyW G XϽo 1 6 Ӳ. 4(?u C 8 j H l f6*k Fa^? e G= J0 } 7L z ͺ p H & ɴ ?L? Z %PDF-1.4% 4 0 obj <> streamx endstream endobj 6 0 obj <> streamx ]o 0 + e{Q $ R N。 @@- n wB?L $ .JDL | >獝 [ > { =?_ʻ(寒 1`ؚyW G。 XϽo 1 6 Ӳ. 4(?u C 8 j H l f6*k Fa^?e G= J0。 }7L z p H & L?Z

Excuse me for special characters rendered badly: the problem is just that. 对那些表现不好的特殊字符不好意思:问题就是这样。 I would like to take this response, obtained from my request, and print this content correctly in another browser tab. 我想从我的请求中获得此响应,并在另一个浏览器选项卡中正确打印此内容。 I wrote the code to do this: I used the Blob object. 我编写了代码来做到这一点:我使用了Blob对象。 The page for "reading the pdf" is properly opened. 正确打开了“阅读pdf”页面。 However, I see only a pdf document which is completely white, a sign of the fact that my response was not written on the document that has been created. 但是,我只看到一个完全白色的pdf文档,这表明我的回答并未写在已创建的文档上。

Below I show you the code of my request: 在下面,我向您显示我的请求的代码:

$.ajax({
    type: "GET",
    url:urll + parameter,
    async: true,
    crossDomain: true,
    accept: {
          pdf: 'application/pdf'
    },
    success:function(response, status, xhr){

        alert("Response: "+response);
        alert("Status: "+status);
        alert("Xhr: "+xhr);

        // check for a filename
        var filename = "";
        var disposition = xhr.getResponseHeader('Content-Disposition');
        if (disposition && disposition.indexOf('attachment') !== -1) {
            var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
            var matches = filenameRegex.exec(disposition);
            if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
        }

        var type = xhr.getResponseHeader('Content-Type');
        var blob = new Blob([response], { type: type });

        if (typeof window.navigator.msSaveBlob !== 'undefined') {
            // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
            window.navigator.msSaveBlob(blob, filename);
        } else {
            var URL = window.URL || window.webkitURL;
            var downloadUrl = URL.createObjectURL(blob);

            if (filename) {
                // use HTML5 a[download] attribute to specify filename
                var a = document.createElement("a");
                // safari doesn't support this yet
                if (typeof a.download === 'undefined') {
                    //window.location = downloadUrl;
                    window.open(downloadUrl, '_blank');
                } else {                        
                    a.href = downloadUrl;
                    a.download = filename;
                    document.body.appendChild(a);
                    a.setAttribute("target","_blank");
                    a.click();
                }
            } else {
                //window.location = downloadUrl;
                window.open(downloadUrl, '_blank');
            }

            setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
        }
    },
    error: function(xhr,status,error){
        alert("error "+xhr+" "+error+" "+status);
    }
});

As I understand, you use GET and need a pdf file in a new tab. 据我了解,您使用GET并在新标签中需要一个pdf文件。 Soo...Why don't you just open this url in a new window like this o ...为什么不像这样在新窗口中打开此网址

var fullUrl = urll + parameter;
window.open(fullUrl);

I had a similar problem using AngularJS (both $resource and $http). 我在使用AngularJS时遇到了类似的问题($ resource和$ http)。 The solution in my case was to set responseType: 'arraybuffer' . 我的解决方案是设置responseType: 'arraybuffer' This is part of the XMLHttpRequest (see https://xhr.spec.whatwg.org/#interface-xmlhttprequest ) but it is not exposed by jQuery. 这是XMLHttpRequest的一部分(请参阅https://xhr.spec.whatwg.org/#interface-xmlhttprequest ),但jQuery并未公开。

You can add it to jQuery using this plugin: https://github.com/henrya/js-jquery/tree/master/BinaryTransport 您可以使用以下插件将其添加到jQuery: https : //github.com/henrya/js-jquery/tree/master/BinaryTransport

As I said, that's what worked for me in AngularJS. 正如我所说的,这就是在AngularJS中为我工作的方式。 Hopefully it's the solution you're looking for as well :) 希望它也是您正在寻找的解决方案:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM