使用blob将html表导出为pdf

时间:2016-02-02 06:15:07

标签: angularjs pdf blob

我想知道如何从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"&lt; ======此linein pdf?

提前致谢:)

1 个答案:

答案 0 :(得分:0)

更新

您可以通过set responseType:

将pdf内容检索为ArrayBuffer
  $http({
                url: url,
                method: method,
                headers: {
                    'Content-type': _type
                },
                responseType: 'arraybuffer' //set response type
            })

注意:

type - &gt; &#39;应用/ PDF&#39;

fileContent - &gt;应该是一个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);
        }
    }
}