I have an AngularJs interface and a NodeJS database. I am trying to create a PDF on NodeJS based on some parameters from AngularJS and return the PDF in response.
The code I use works fine when I make a GET request from AngularJS, but it returns an empty PDF when I make a POST request. I need to make a POST request since I have to send some specific data.
First, I save the file to disk, and then send it to the external interface so that I can see that the PDF is being created correctly. This is either sent incorrectly or read correctly on FrontEnd.
Below is my AngularJS code
var url = {{My Node Server URL}};
(Note: Works when I make a Get request, without sending post params)
var $promise = $http.post(encodeURI(url), {
responseType: 'arraybuffer',
competitors: competitors,
channels: channels
});
$promise.then(
function success(response) {
var blob = new Blob([response.data], { type : 'application/pdf' });
var pdfLink = (window.URL || window.webkitURL).createObjectURL( blob );
window.open(
pdfLink,
'_blank'
);
cb(pdfLink);
}, function error(response) {
--Error Handling--
}
)
Below is my NodeJS code
wkhtmltopdf(
html,
{
output: pdfName
},
function (code, signal) {
fs.readFile(pdfName, function (err, data) {
if (err) {res.writeHead(400); res.end("" + err); return;}
res.writeHead(
200,
{
"content-type" : "application/pdf",
"Content-Disposition": "attachment; filename=Best.pdf "
}
);
res.end(data);
});
}
);
, . // . , , .