Working with PDF files on Nodejs server

Asked

Viewed 464 times

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)?

  • 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.

  • Just open this string with a PDF reader (this string would be in a file), put inside a <object> (HTML), something like that

  • I make an ajax for my service, and when including the response in an HTML is displayed the text in the HTML page

  • The displayed text is still in %PDF-1.3 n% n1 0 obj n<< n/Type /Catalog n/Pages 2 0 R n/Pagemode..."

  • 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

  • Reap the code of resolve(response.body);?

  • 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)

  • 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

  • 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..."

Show 5 more comments

1 answer

0


Workaround: Text was still displayed in string format due to request encoding

I removed Promise and did it right into the function, which called her. I added 'encoding': 'null' in the request and changed setHeader. The call was like this:

    try{
        request({
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer ' + token
            },
            url: url,
            method: 'GET',
            encoding: null
            }, function (error, response, body) {                    
            if (error) {
                logger.error("ERROR: " + error);
            } else {
                res.contentType("application/pdf");
                res.send(response.body);
            } 
        });
    }

To display in the browser, I used Xmlhttprequest() to make the Response to become a blob, from which URL.createObjectURL opened the pdf in a new tab

Browser other questions tagged

You are not signed in. Login or sign up in order to post.