This是一个笨蛋链接。我使用 Node 运行pdfSample.js
,pdfsample.html
是要转换为pdf的实际数据,generatepdf.html
用于获取它。
当我运行它时,它显示一个错误无法加载 Pdf 文档
。我推荐了this sample 。我没有遇到任何问题,为什么它不起作用?
$http.get(url, {responseType: 'arraybuffer'}).then(function successCallback(response){
console.log(response);
var file = new Blob([response], {type: 'application/pdf'});
console.log(file);
var fileURL = URL.createObjectURL(file);
$scope.loading = false;
q.resolve(fileURL);
},
function errorCallback(response){
$scope.loading = false;
q.reject(response);
});
/* .success(function (response) {
})
.error(function (err) {
});*/
return q.promise;
};
最佳答案
您需要为实际数据而不是响应对象创建 blob。
您需要new Blob([response.data], {type: 'application/pdf'})
但请确保当您点击http://localhost:8081/api/printpdf1
时您会得到pdf而不是错误。
$http.get(url, {responseType: 'arraybuffer'}).then(function successCallback(response){
console.log(response);
var file = new Blob([response.data], {type: 'application/pdf'});
console.log(file);
var fileURL = URL.createObjectURL(file);
$scope.loading = false;
q.resolve(fileURL);
},
function errorCallback(response){
$scope.loading = false;
q.reject(response);
});
/* .success(function (response) {
})
.error(function (err) {
});*/
return q.promise;
};
关于node.js - 无法加载 PDF 文档 html-pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47750561/