最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create downloable pdf file from JSON - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 2

PDF files aren't the same as text (or json) files, try using a library like JSPDF to make client sided PDF files.

发布评论

评论列表(0)

  1. 暂无评论