I am trying to generate a PDF on the server side using node js and download it on the client side on angular 4 (something like Google Drive does when you press download as PDF).
The Node JS code looks something like this:
var pdf = require('html-pdf');
app.get('/pdf/contacts', function(req, res) {
Contact.find(function(error, response) {
res.render('pdfs/views/contacts', {name: 'Daniel Pacuraru', contacts: response}, function(error, thtml) {
var opts = {
"format": "A4",
"orientation": "portrait",
"border": "0.4in"
};
pdf.create(thtml, opts).toStream(function(err, stream){
res.attachment('pdfname.pdf');
stream.pipe(res);
});
});
});
});
On the angular 4 side i got something like this:
import * as FileSaver from 'file-saver';
downloadContacts(): Promise<any> {
return this.http
.get('http://localhost:3000/pdf/contacts')
.toPromise()
.then(response => {
FileSaver.saveAs(response, "testdata.pdf");
});
}
I get this error: Failed to execute 'createObjectURL' on 'URL' and if i change the line into this
FileSaver.saveAs(response.blob(), "testdata.pdf");
then i get this error: Error: The request body isn't either a blob or an array buffer
I can't figure out what am i doing wrong, btw is there a way to download this stream easy without any plugin like file-saver ? since if i just enter and api link on the browser it automatically downloads me that pdf file.
Thank you in advance, Daniel.