0
I have an Angularjs+Node(Express) application totally Restless. The back end only serves to validate requests and route to other applications, holders of business rules.
At some point, my front sends a request to generate a pdf, something like:
const callbackSucesso = function(resposta){
if(resposta && resposta.data){
let headers = resposta.headers();
let blob = new Blob([resposta.data],{type: headers['content-type']} );
saveAs(blob, "download.pdf");
}else{
//coisas que não importam aqui
}
};
$http({
method: 'POST',
url: '/api/pdf',
data: JSON.stringify(dto)
}).then(callbackSucesso,callbackErro);
However, when I open the generated file, I notice that there is information that is distorted, for example: DESCRIPTION => Description ¿½ï¿½
When performing by Postman, the return of the API is normally generated as a file.
I’ve found answers that talk to add type information to the Blob object - like this: Generate angular pdf using javascript - but the error remained.
When I tried to do it in the back end, the exits weren’t that different. Then I thought I’d intercept the Ode, generate the file, and redirect to the front.
const fs = require('fs');
...
request(options, function(error, resposta, corpo){
if(error) {
//não interessa para o contexto
}else{
fs.writeFileSync("10111.pdf", corpo,'binary');
//aqui eu enviaria o link pro front baixar
}
next();
});
I followed that example here: https://stackoverflow.com/questions/31040014/how-to-save-pdf-in-proper-encoding-via-nodejs
From now on I thank those who can clear my road(!)