从js应用程序

时间:2018-05-22 13:47:44

标签: javascript pdf ember.js laravel-5.2 tcpdf

我有SPA(ember js)和REST API服务 - php(laravel 5.2)。

我试图下载在服务器上生成的pdf文档

我是如何生成pdf

$tcpdf->Output({path}, 'I');

如果我直接在浏览器中访问API资源,一切正常,文件正常下载。

但是,当我试图在SPA中发现回复并开始下载时,我得到了空白的pdf。

我的js下载功能看起来像

this.download(results, 'test.pdf','application/pdf')
download(data, filename, mime) {
    var blob = new Blob([data], {type: mime || 'application/octet-stream'});
    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 blobURL = window.URL.createObjectURL(blob);
        var tempLink = document.createElement('a');
        tempLink.style.display = 'none';
        tempLink.href = blobURL;
        tempLink.setAttribute('download', filename);

        // Safari thinks _blank anchor are pop ups. We only want to set _blank
        // target if the browser does not support the HTML5 download attribute.
        // This allows you to download files in desktop safari if pop up blocking
        // is enabled.
        if (typeof tempLink.download === 'undefined') {
            tempLink.setAttribute('target', '_blank');
        }

        document.body.appendChild(tempLink);
        tempLink.click();
        document.body.removeChild(tempLink);
        window.URL.revokeObjectURL(blobURL);
    }
},

结果变量是从服务器返回的结果文件字符串。

有人可以指出这里出了什么问题,为什么我的pdf是空的?

0 个答案:

没有答案