Base64 pdf String to pdf binary and send as webhook response

Describe the problem/error/question

From an api I get an JSON with an pdf. The pdf is a base64 encoded string.
I want to respond to the webhook by sending the pdf to the user.
Problem:
I don’t know how to convert it to a binary.

What is the error message (if any)?

Things I tried:

  1. Encoding the base64 string and return a text response with appropiate headers
  • This gives me an empty pdf (but at least a pdf), but the pdf string form the API works. If I use an online converter to convert base64 to pdf, the pdf is shown correctly.
  1. I tried the Move Binary Data node:
    But this node give me an empty output:

Information on your n8n setup

  • n8n version: 1.3
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): cloud
  • Operating system:

Juhu solved it, the trick is returning the buffer - not a string from buffer so here are my two final nodes that do the trick:

hello @pfiadDi

you can use the Convert to File node with option “Move the base64 string to file”

1 Like

Thank you will take a look!

In my case the output comes straight from gotenberg

This gives me error The value in "data" is not set
If I remove “Convert to PDF 2”: The browser have a page full of binary characters like the there is no Content-Type. Please help …

That’s because you don’t have a base64 string in the data field.

Moreover, why you need the Convert to PDF node? The HTTP node already returns the file

@barn4k exactly I don’t need Convert to PDF I just need to pipe back the response from gotemberg that already have the right Content-Type. However this doesn’t work it returns back a page full of binary:

Have you tried yourself? I think is a bug…

I can’t test it as the site is not accessible for me.

I’ve also noticed one issue with the Convert to HTML node. You should enable the switch Data is base64. Otherwise you will save the base64 string as is in the file instead of saving the html content

I’ve changed Convert to HTML but makes no difference (the pdf is created correctly either way)

Gotemberg offers a demo api (https://demo.gotenberg.dev)
I’ve changed the url you can test yourself! unfortunately I can’t embed the code here it I got a 403 error if I try

I’ve embedded the workflow. The issue is with the HTML generate template node. You should remove the script part from the end.

As for the workflow, you need the different convert operation, as your content is not base64

Dear @barn4k, please, please, try the workflow you have posted and you’ll see the problems yourself! :

  1. If I use the Convert to html, as you suggest, the pdf comes mixed with the right rendered content and the html source in the body. The same happen if it goes to webhook response see here, so is not a pdf related issue is the Convert node that has a bug

  2. Using the Base64toFile is the only trick to correct create html for guteneberg and create a good pdf but I can’t send the pdf back with “respond to webhook” because it comes as page full of binary characters, but If I click view, or put a node to save to disk, the pdf is correct.
    gist here

My n8n version is 1.32.2

P.S: every time I embed the code on the bottom I see draft offline and cant post anymore with a 403 error, and say the post is edited in another window (I don’t) what is the trick???

screenshot of Convert to html with the source in the body

Ah, I see.

Fixed the workflow. It was messing with tags, so the proper way was to convert it to the text file instead of HTML

To avoid issues with attaching a workflow, please, remove that script part from the node:

And a complete example with webhooks.

Flow with Webhook

Flow to trigger the webhook

1 Like

Thanks barn,
this solve the issue of bad formatting in the html, and correctly returns the html to the webhook response, but if you attach webhook response to the output of the PDF request I still get a page full of binary, so sending back a pdf from a form is still not working.

Btw then I wonder what “Convert to HTML” is used for?

What is the issue with attaching a PDF? For me, everything works fine and I receive the proper response from the webhook

That one converts the JSON object to an HTML document. But as we have the Generate HTML node, that already converts our JSON object to HTML, we need only to save the response in the text file without any modifications.

If I use a webhook as a trigger, like in your example, and use an external form to POST and works! surprise :grinning:
So the bug shows up only with the integrated n8n Form Trigger, can you confirm?

I think the Form trigger is not intended to receive files
image

Maybe @Jon can shed some light? :slight_smile: