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.