使用带有 AngularJS 的 html2pdf 创建和下载 PDF
Create and download PDF using html2pdf with AngularJS
我正在为 Web 应用程序做后端,我正在使用 html2pdf class (http://html2pdf.fr/) 使用数据库中的现有数据创建 PDF 文件。
我的前端开发人员说他不能使用 "stream" 我用 API 交付 angular。我使用 ->Output('D') 方法提供文件。
有人知道如何使用这个 class 通过 angularjs 下载 PDF 文件吗?
要下载 PDF 文件的数据,请使用 responseType: "arraybuffer"
:
var config = { responseType: "arraybuffer" };
vm.fetch = function() {
$http.get(url, config).then(function(response) {
vm.result = "SUCCESS";
vm.length = response.data.byteLength + " bytes";
var blob = new Blob([response.data],
{type: "application/pdf"});
vm.data = URL.createObjectURL(blob);
}).catch(function(response) {
vm.result = "ERROR "+response.status;
});
};
});
将数据转换为 blob url 并使用下载按钮:
<a download="{{name}}.pdf" xd-href="data">
<button>Download data</button>
</a>
xd-href
指令
app.module("myApp").directive("xdHref", function() {
return function linkFn (scope, elem, attrs) {
scope.$watch(attrs.xdHref, function(newVal) {
newVal && elem.attr("href", newVal);
});
};
});
DEMO on PLNKR 下载http://demo.html2pdf.fr/examples/exemple07.php
我正在为 Web 应用程序做后端,我正在使用 html2pdf class (http://html2pdf.fr/) 使用数据库中的现有数据创建 PDF 文件。
我的前端开发人员说他不能使用 "stream" 我用 API 交付 angular。我使用 ->Output('D') 方法提供文件。
有人知道如何使用这个 class 通过 angularjs 下载 PDF 文件吗?
要下载 PDF 文件的数据,请使用 responseType: "arraybuffer"
:
var config = { responseType: "arraybuffer" };
vm.fetch = function() {
$http.get(url, config).then(function(response) {
vm.result = "SUCCESS";
vm.length = response.data.byteLength + " bytes";
var blob = new Blob([response.data],
{type: "application/pdf"});
vm.data = URL.createObjectURL(blob);
}).catch(function(response) {
vm.result = "ERROR "+response.status;
});
};
});
将数据转换为 blob url 并使用下载按钮:
<a download="{{name}}.pdf" xd-href="data">
<button>Download data</button>
</a>
xd-href
指令
app.module("myApp").directive("xdHref", function() {
return function linkFn (scope, elem, attrs) {
scope.$watch(attrs.xdHref, function(newVal) {
newVal && elem.attr("href", newVal);
});
};
});
DEMO on PLNKR 下载http://demo.html2pdf.fr/examples/exemple07.php