# \[Solved\] Having trouble creating a valid PDF from a buffer that is returning the contents of a PDF (API call)

**URL:** <https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989>\
**Category:** General\
**Created:** [6 June 2023 12:56 UTC](https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989 "2023-06-06T12:56:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [6 June 2023 12:56 UTC](https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989/1 "2023-06-06T12:56:01Z")

</div>

Hi,

I have an HTTP Request node successfully bringing in the data that is the contents of a PDF (according to that vendor's API). The data successfully arrives as a Buffer.

What I need to do now is create a PDF from the buffer.

So I connected a Write file node to take the buffer (msg.payload) from the HTTP Request node, but I am not understanding what encoding should be used. I have tried: default, utf8 base64 binary and through they each create a PDF file, the PDF is not valid. It seems that I am misunderstanding this process.

In node, you can write a buffer to a file without encoding. In Node-Red, I need to use the Write file node because this flow is going to be published on FlowForge Cloud which doesn't allow writes to the OS and has a modified Write file node, specific to their environment.

Any tips would be appreciated.

 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/3/73237f368185da3c9fa71b5781c8d7607fc54705.png)  
 ![3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/042957ddc603c932be9e99b60019f58f7fb2e2bb.png)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 June 2023 13:39 UTC](https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989/2 "2023-06-06T13:39:28Z")

</div>

Hi, the buffer is already encoded so the setting is a slight misnomer. I think that if you just set it to "binary" it should work. If not, it is possible that the file out node needs some tweaking. It is certainly possible to use Node.js to output a buffer direct to a pdf.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 June 2023 13:46 UTC](https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989/3 "2023-06-06T13:46:01Z")

</div>

Just tested and output as binary buffer and save as default encoding should work.  
e.g.

```auto
[{"id":"f0cd7ce36ab5cfdb","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":4400,"wires":[["1445a4e9a50fd329"]]},{"id":"1445a4e9a50fd329","type":"http request","z":"b9860b4b9de8c8da","name":"","method":"GET","ret":"bin","paytoqs":"ignore","url":"https://freetestdata.com/wp-content/uploads/2021/09/Free_Test_Data_100KB_PDF.pdf","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":370,"y":4400,"wires":[["7bfa4529bb0a5eb9"]]},{"id":"7bfa4529bb0a5eb9","type":"file","z":"b9860b4b9de8c8da","name":"","filename":"test.pdf","filenameType":"str","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":500,"y":4440,"wires":[["44dd6e4d8dc4ac44"]]},{"id":"44dd6e4d8dc4ac44","type":"debug","z":"b9860b4b9de8c8da","name":"debug 331","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":4400,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [6 June 2023 14:21 UTC](https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989/4 "2023-06-06T14:21:03Z")

</div>

Were you able to open the resulting PDF?  
I ran your flow and the PDF was not valid.  
I'm running node-red 2.04

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 June 2023 14:25 UTC](https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989/5 "2023-06-06T14:25:57Z")

</div>

Yes, displayed 3 page pdf when i opened the test.pdf. V3.0.2

---

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [6 June 2023 14:27 UTC](https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989/6 "2023-06-06T14:27:22Z")

</div>

Interesting! So I have something else going on. Thanks for the reply!

---

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [6 June 2023 14:31 UTC](https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989/7 "2023-06-06T14:31:08Z")

</div>

I appear to be having some other problem going on as solution from @E1cid produces a valid PDF whereas my implementation does not.

And I agree that the Encoding prompt, when the msg.payload is a buffer might need a "do not encode" option in the dropdown list, but I can see that being a bit confusing, as that happens at runtime. Maybe just a not in the docs for the node?

---

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [6 June 2023 14:39 UTC](https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989/8 "2023-06-06T14:39:42Z")

</div>

I see 3 pages and the file name is correct, but the pages are blank. Somehow, the content has been missed or changed/corrupted. Hmmm.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 June 2023 14:49 UTC](https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989/9 "2023-06-06T14:49:57Z")

</div>

I only see blank pages if I output as utf8 from request node, Otherwise the pages have text and an image when output is binary.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 June 2023 15:01 UTC](https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989/10 "2023-06-06T15:01:46Z")

</div>

What is the content of the PDF - is it encoded as images (bitmap) or text? Both are valid within a PDF but might possibly be treated differently as it goes into the buffer?

---

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [6 June 2023 15:26 UTC](https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989/11 "2023-06-06T15:26:29Z")

</div>

I have a solution thanks to both @E1cid and @TotallyInformation !

My node-red instance is on a Digital Ocean droplet and so I cannot easily view the file created by your flow. Instead, I have a html file I add the following to:

```auto
<html>
  <body>
    <a
      href="https://curious-skylark-1637.flowforge.cloud/SPT-get-pdf"
      download="cs.pdf"
      >Download the pdf (from Curious Skylark)</a
    >
    <br />
    <br />
    <a href="https://{{myserver}}.com/SPT-get-pdf" download="test.pdf"
      >Download the pdf</a
    >
  </body>
</html>

```

 ![4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9ff3fe5b5e0289a2e9df265877ec3921e6c5958d.png)

And the HTTP in node + a Read file node + http response node (see below) send it back as a file.

What I had done wrongly is set the Output of the Read file node to "a single utf8 string". When I set that to a "single Buffer object" it all worked perfectly

Incidentally, and just for completeness, I set the Header in the Http response node:  
"Content-Disposition" set to "attachment; filename=222.pdf"  
as this forces the browser to interpret the file as something to save with that name

---

<div class="post-metadata">

**Author:** ![UmbraLimi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/umbralimi/32/64111_2.png) [@UmbraLimi](https://discourse.nodered.org/u/UmbraLimi)\
**Post date:** [6 June 2023 15:27 UTC](https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989/12 "2023-06-06T15:27:22Z")

</div>

The content is an image and text, just like yours. It displays perfectly!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 June 2023 15:28 UTC](https://discourse.nodered.org/t/solved-having-trouble-creating-a-valid-pdf-from-a-buffer-that-is-returning-the-contents-of-a-pdf-api-call/78989/13 "2023-06-20T15:28:01Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
