Hi guys,
I’ve a trouble with download of a PDF file created with pdfmake. I have created and viewed the pdf made from charts.js, but now I would like to download the file.
I tried to use ngCordova:
…
var blob = new Blob([pdf], {type: ‘application/pdf’});
…
$scope.pdfUrl = URL.createObjectURL(blob);
…
$cordovaFile.writeFile(cordova.file.dataDirectory,‘fileName.pdf’,$scope.pdfUrl, true).then(function(result){
alert(‘Success! Survey created!’);
}, function(err) {
console.log(“ERROR”);
})
But I got an error 1: NOT_FOUND_ERR
What’s wrong? Please help me…I’m going crazy!! Thank you all guys
Hello
Thnx for your code it’s very interesant.
I resolved de problem uninstall de plugins.
The next problem is with new Blob.
My code:
apiRoutes.post(’/getPDFBill’, passport.authenticate(‘jwt’, { session: false}), function(req, res) {
exec('curl -X POST --insecure -d "Param" URL > /home//tmp/facturaOut.pdf', function(err,out,code){
var filePath = "/home/tmp/facturaOut.pdf";
fs.readFile(filePath , function (err,data){
res.contentType("arraybuffer");
res.send(data);
});
});
});
In my controller:
APIService.GetPDFBill()
.then(function(result) {
var blob = new Blob(result.data, {type: ‘application/pdf’}); --> Error The code stop
Your result.data must be the base64 result of pdfMake library output.
I mean :
PDFService.createPdf(myData).then(function(pdf) {
var blob = new Blob([pdf], {type:'application/pdf'});
...
Where:
function PDFService($q,ReportSrv,GRAPHTYPE) {
function createPdf(myData) {
return $q(function(resolve, reject) {
var dd = createDocumentDefinition(myData); // JSON version of our PDF
var pdf = pdfMake.createPdf(dd);
pdf.getBase64(function (output) {
resolve(base64ToUint8Array(output));
});
});
}
return {
createPdf: createPdf
};
}
function base64ToUint8Array(base64) {
var raw = atob(base64);
var uint8Array = new Uint8Array(raw.length);
for (var i = 0; i < raw.length; i++) {
uint8Array[i] = raw.charCodeAt(i);
}
return uint8Array;
}