3
Hello,
I have a service that when performing a request via Postman for example, I get a file . pdf
I’m calling this service from a new service nodejs, but I’m not getting the . pdf returned in a call.
I get a string with content "%PDF-1.3 n% n1 0 obj n<< n/Type /Catalog n/Pages 2 0 R n/Pagemode..."
I perform communication between services using nodejs request
return new Promise( async (resolve,reject) => {
request({
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
},
url: url,
method: 'GET',
}, function (error, response, body) {
if (error) {
reject(error);
} else {
resolve(response.body);
}
});
});
}
And in my service I add the information in the header
.then((result) =>{
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'form-data; name="arquivo.pdf"; filename="arquivo.pdf"');
res.status(200).send(result) }
How can I make my service just the middle ground between the service that provides the . pdf and an ajax call?
I need to preferably display this . pdf in the browser, or perform the download.
What’s wrong with "%PDF-1.3 n% n1 0 obj n<< n/Type /Catalog n/Pages 2 0 R n/Pagemode..."? PDF is text, this text is not the PDF you want or you are not reading it with an "interpreter" (which should turn this string into a understandable PDF file)?
– Costamilam
How do I transfer "%PDF-1.3 n% n1 0 obj n<< n/Type /Catalog n/Pages 2 0 R n/Pagemode..." back to a PDF file? The pdf contains text and images.
– Eliardo Marini
Just open this string with a PDF reader (this string would be in a file), put inside a
<object>
(HTML), something like that– Costamilam
I make an ajax for my service, and when including the response in an HTML is displayed the text in the HTML page
– Eliardo Marini
The displayed text is still in %PDF-1.3 n% n1 0 obj n<< n/Type /Catalog n/Pages 2 0 R n/Pagemode..."
– Eliardo Marini
How did you do? When I say inside the
object
it’s actually something like<object data="url-pro-recurso.pdf"></object>
. If you are asynchronous you can turn to Base64 and put the url to Base64– Costamilam
Reap the code of
resolve(response.body);
?– mari
You should read this text with a PDF reader, how are you reading it? Postman? It doesn’t turn it into a PDF (I think)
– Costamilam
Calling by Postman it turns into PDF, but while opening I get the message that the file is corrupted. I need to call my service via ajax and display this pdf in the browser or download
– Eliardo Marini
I’m calling the service like this: $. ajax({ type: "GET", url: "http://localhost:3000/get-pdf", Success: Function(Response) { var ifrm = Document.createelement("iframe"); ifrm.setAttribute("src", Response); ifrm.style.width = "640px"; ifrm.le.height = "480px"; Document.body.appendchild(ifrm); } }); But the PDF is not displayed, what continues to be displayed is the message: "%PDF-1.3 n% n1 0 obj n<< n/Type /Catalog n/Pages 2 0 R n/Pagemode..."
– Eliardo Marini