【问题标题】:Export html table into pdf using blob使用 blob 将 html 表导出为 pdf
【发布时间】:2016-05-10 20:04:56
【问题描述】:

我想知道如何从 angularjs 导出 pdf 文件。我已经导出了 excel 文件,它工作正常。

以下是excel的代码

view.html

<button ng-click="vm.exportData()" class="btn btn-info"><i class="glyphicon glyphicon-download"></i>&nbsp; Download as pdf</button>

controller.js

 $scope.exportData = function () {
        debugger;
        var blob = new Blob([document.getElementById('export').innerHTML], {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
        });
        window.saveAs(blob, "Report.xls");
    },

怎么做 type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"

提前致谢:)

【问题讨论】:

  • 你试过'type': 'application/pdf'
  • 正如@CodingNinja 所说,'type': 'application/pdf' 应该可以工作
  • 是的...我试过了,文件被下载,但是当我尝试打开它时出现错误“无法加载 pdf 内容”..

标签: angularjs pdf blob


【解决方案1】:

更新:

您可以通过设置 responseType 将 pdf 内容检索为 ArrayBuffer:

  $http({
                url: url,
                method: method,
                headers: {
                    'Content-type': _type
                },
                responseType: 'arraybuffer' //set response type
            })

注意:

类型 -> '应用程序/pdf'

fileContent -> 应该是一个 ArrayBuffer

downloadFile(filename, 'application/pdf', fileContent);

function downloadFile(filename, type, fileContent) {
    var blob = new Blob([fileContent], {type: type});
    if (navigator.msSaveBlob) { // IE 10+
        navigator.msSaveBlob(blob, filename);
    } else {
        var link = document.createElement('a');
        if (link.download !== undefined) { // feature detection
            // Browsers that support HTML5 download attribute
            var url = URL.createObjectURL(blob);
            link.setAttribute('href', url);
            link.setAttribute('download', filename);
            link.style.visibility = 'hidden';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }
    }
}

【讨论】:

  • 好吧,我不确定我应该在哪里使用响应类型作为“数组缓冲区”,因为我已经在其控制器内部的按钮单击上定义了该函数,而不是通过任何 http 链接调用它..跨度>
  • 如果你的内容不是来自服务器的二进制数据,你可以检查这个库:code.google.com/archive/p/jspdf
猜你喜欢
  • 2014-05-26
  • 1970-01-01
  • 2015-09-25
  • 2013-08-25
  • 1970-01-01
  • 1970-01-01
  • 2022-12-24
  • 1970-01-01
  • 2016-09-19
相关资源
最近更新 更多