Binary pdf corrupted in email attachment

I get a .pdf file in a base64 string from a web hook:
image

Using the SendGrid module, I’m trying to send an email and attaching this file as an attachment.

  • I’ve tried attaching the base64 I receive as is and I get a corrupted .pdf on in the email.

  • I’ve tried using the ‘toBinary(;base64)’ function within the email attachment field and I get a .txt file full of random characters in the email.
    image

Any other alternatives or explanations as to why I can’t get the .pdf out the other end?
Thanks!

@NicoSQ The binary data you get is containing the encoding parameter as well. You will have to remove the data:application/pdf;base64, part so it only leaves you with the actual binary data.
Use something like the replace() function for example to do this.
Let me know how that goes.

2 Likes

Thanks Bjorn.

Any recommendation on a smart way to do this when the encoding is dynamic? Depending on the file type, this will change.
image

How can I say "remove everything to the left of the first ‘comma’ "?
Appreciate the help!

@NicoSQ You can use RegEx:

{{replace("data:application/pdf;base64,s87shdfui32nuiwehf8ef"; "/.+base64\,/"; emptystring)}}

In this case replace the static base64 text with the variable you have.

See more about it here:

3 Likes

Thanks Bjorn, the regex worked as expected!

Nonetheless, I now get a .txt file with the entire binary in it:
image

How can I get this to be the .pdf I’m being sent?

Appreciate the help!

Take a look here @NicoSQ , see if that helps you:

2 Likes

I solved this by adding a ‘.pdf’ at the end of the file name!