首页 > 解决方案 > JS PDF Blob 空白页

问题描述

我正在使用外部 Rest API 来获取包裹的 spedition 标签。作为回应,我有:

%PDF-1.4\n%����\n6 0 obj\n<</ColorSpace[/Indexed/DeviceRGB 255(\0\0\0 [...]

现在我需要在浏览器中显示 PDF

var xhr = new XMLHttpRequest();
    xhr.open('POST', '/Order/CreateShipmentInpost', true);
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr.responseType = 'arraybuffer';
    xhr.onload = function (e) {
        if (this.status == 200) {
            var blob = new Blob([this.response], { type: "application/pdf" });
            var link = document.createElement('a');
            link.href = window.URL.createObjectURL(blob);
            link.download = "Report.pdf";
            link.click();
        }
    };
    xhr.send($.param(data));

在生成的 PDF 文件中只有空白页。我不知道为什么内容没有显示。

PDF空白页

我正在使用responseType = 'arraybuffer'. 控制器返回:

 return File(Encoding.UTF8.GetBytes(data, MediaTypeNames.Application.Pdf);

我也尝试返回 json 或字符串,但 PDF 始终为空。

标签: javascriptc#pdfblob

解决方案


推荐阅读