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

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.


3

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.

1 Like

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

[{"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":[]}]
1 Like

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

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

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

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?

1 Like

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.

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.

1 Like

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?

1 Like

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:

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

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

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

1 Like

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