admin管理员组文章数量:1391969
Basically, the issue I'm experiencing is I have queried to the server to return a pdf file which is returned in JSON format.
I have tried to create a HTML element to auto download the pdf (this is not the issue), the issue is the pdf downloaded can not be opened because it fails and reports an error, for example in Adobe Reader:
"Adobe reader could not open 'test.pdf' because is either not supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)"
I'm working with AngularJS to make the ajax call to the server to get the JSON object and then try to format it and download it:
...
var pdf = pdfservice.get({id:pdfId});
console.log(pdf);
pdf.$promise.then(function(data){
var element = angular.element('<a/>');
element.attr({
href: 'data:attachment/pdf;charset=utf-8,' + encodeURI(data),
target: '_self',
download:'test.pdf'
})[0].click();
});
...
UPDATE
Using the $http service from Angular and with the same code, it downloads the pdf but it's empty, so maybe it could be an issue of encoding or formating the json object...
...
$http({ method: 'GET', url: '')
.success(function(data) {
console.log(data);
var element = angular.element('<a/>');
element.attr({
href: 'data:attachment/pdf;charset=utf-8,' + encodeURI(data),
target: '_self',
download:'test.pdf'
})[0].click();
});
...
Basically, the issue I'm experiencing is I have queried to the server to return a pdf file which is returned in JSON format.
I have tried to create a HTML element to auto download the pdf (this is not the issue), the issue is the pdf downloaded can not be opened because it fails and reports an error, for example in Adobe Reader:
"Adobe reader could not open 'test.pdf' because is either not supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)"
I'm working with AngularJS to make the ajax call to the server to get the JSON object and then try to format it and download it:
...
var pdf = pdfservice.get({id:pdfId});
console.log(pdf);
pdf.$promise.then(function(data){
var element = angular.element('<a/>');
element.attr({
href: 'data:attachment/pdf;charset=utf-8,' + encodeURI(data),
target: '_self',
download:'test.pdf'
})[0].click();
});
...
UPDATE
Using the $http service from Angular and with the same code, it downloads the pdf but it's empty, so maybe it could be an issue of encoding or formating the json object...
...
$http({ method: 'GET', url: 'http://www.testweb./pdf/1')
.success(function(data) {
console.log(data);
var element = angular.element('<a/>');
element.attr({
href: 'data:attachment/pdf;charset=utf-8,' + encodeURI(data),
target: '_self',
download:'test.pdf'
})[0].click();
});
...
Share
Improve this question
edited Oct 31, 2014 at 15:36
Joe Lewis
asked Oct 31, 2014 at 15:13
Joe LewisJoe Lewis
9785 gold badges18 silver badges34 bronze badges
1 Answer
Reset to default 2PDF files aren't the same as text (or json) files, try using a library like JSPDF to make client sided PDF files.
本文标签: javascriptCreate downloable pdf file from JSONStack Overflow
版权声明:本文标题:javascript - Create downloable pdf file from JSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744743094a2622726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论