OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Transform binary response to pdf in expressjs

  • Thread starter Thread starter Pavel Perevezencev
  • Start date Start date
P

Pavel Perevezencev

Guest
I'm trying to use the Browserless PDF API, and for this I wrote the following code:

Code:
app.get("/api/pdf", async (request, response) => {
    try {
        const { data } = await axios.post("http://localhost:3001/pdf", {
            url: "https://www.google.com/",
        })

        response.setHeader("content-type", "application/pdf")
        response.contentType("application/pdf")
        response.send(Buffer.from(data, "binary"))
    } catch (error) {
        console.log(error.message)
    }
})

in browser its look like this: enter image description here

but if I fulfill the equal curl request

Code:
curl -X POST  \
  http://localhost:3001/pdf \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/json' \
  -d '{
  "url": "https://google.com/"
}'  --output 'file.pdf'


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 49148    0 49114  100    34  38039     26  0:00:01  0:00:01 --:--:-- 38040

its look like this

enter image description here

what's wrong with my code?

<p>I'm trying to use the <code>Browserless PDF API</code>, and for this I wrote the following code:</p>
<pre><code>app.get("/api/pdf", async (request, response) => {
try {
const { data } = await axios.post("http://localhost:3001/pdf", {
url: "https://www.google.com/",
})

response.setHeader("content-type", "application/pdf")
response.contentType("application/pdf")
response.send(Buffer.from(data, "binary"))
} catch (error) {
console.log(error.message)
}
})
</code></pre>
<p>in browser its look like this:
<a href="https://i.sstatic.net/GP1swesQ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/GP1swesQ.png" alt="enter image description here" /></a></p>
<p>but if I fulfill the equal curl request</p>
<pre><code>curl -X POST \
http://localhost:3001/pdf \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{
"url": "https://google.com/"
}' --output 'file.pdf'


% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 49148 0 49114 100 34 38039 26 0:00:01 0:00:01 --:--:-- 38040
</code></pre>
<p>its look like this</p>
<p><a href="https://i.sstatic.net/oRsVgvA4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/oRsVgvA4.png" alt="enter image description here" /></a></p>
<p>what's wrong with my code?</p>
 

Latest posts

Top