Uri format for Pdf to Sendgrid

Hey!
I’m working on a workflow in weweb and I have a problem. Let me explain the context:
When a button is clicked, my workflow must generate a pdf from weweb and send it to a user.
I have a dynamic template on sendgrid that is connected to xano.
I have an xano endpoint that allows me to send data from weweb.
This endpoint expects an email, a template-is and a JSON object named data.
For now, the first step is to generate the pdf and return the URI in the “pdfToSend” variable using the “html2pdf” library.

Then I do a “change variable value” and set the value of action 1 to “action.result.data”.

Finally, I use the endpoint. I’ve filled in the mail, the template id and I’ve tested it, it works with the firstname and lastname variables. My problem lies, I think, in the configuration of the data object and the uri format in attachments. I’ve read that the uri must be encoded in base 64, so it works like this:

Blockquote// Récupération des variables
let firstnameValue = variables[‘62d75fe9-d0c9-4e88-b371-b9bf4f291442’]?.[‘first_name’];
let lastnameValue = variables[‘62d75fe9-d0c9-4e88-b371-b9bf4f291442’]?.[‘last_name’];
let contentValue = variables[‘0879c04c-e772-4a46-bd39-8ef8096c1b4d’];

// Encode contentValue en base64
let encodedContentValue;
try {
encodedContentValue = btoa(unescape(encodeURIComponent(contentValue)));
} catch (e) {
console.error(“Erreur lors de l’encodage en base64:”, e);
}

// Création de l’objet JSON avec les propriétés
let jsonObject = {
firstname: firstnameValue,
lastname: lastnameValue,
attachments: [
{
content: encodedContentValue,
filename: “Planning du mois truc”,
type: “application/pdf”
}
]
};

// Retour de l’objet JSON
return jsonObject;

Blockquote

My problem is that I can only test in staging, once the application has been deployed. As a result, it’s impossible to debug properly and see what the uri format looks like. For the moment, on click, it generates the pdf in my downloads folder and closes the window (Don’t know why and so I can’t even make a consol.log in consol) and it does send an email with the first and last name in dynamic. But no pdf attachment…

I think the problem is the library or tool that you’re using. How are you embedding html2pdf?

I use a script on header

But the pdf is generated…

So does the PDF get generated? Because as WeWeb states, any scripts will probably only work on the published project - not in the Editor. My best bet is that your custom JavaScript Action doesnt work, indeed I see it is red.

Screenshot 2023-09-06 at 17.39.48

Screenshot 2023-09-06 at 17.37.47

I’m quite confused how you generate the PDFs but I still think that the problem is in the embed. You might want to check how to embed JS in the editor as well, you can do this with the Statechange utility.

Yes the PDF is generated inStaging, not in the editor. The pdf downloads to my PC but is not sent as an attachment in the rest of the workflow. It’s difficult to debug because I can’t see anything in the editor, so I have to publish each change to be able to test it.

Could you maybe record a video with Loom.com? Explaining how it all works?

In your first screenshot your code will always returns undefined as value to use in the next step.
You need to await your value to return it in the same js action (remember that any weweb js action is an async function).
It requires knowing how to use promises

1 Like

Thank you @dorilama ! It works and I can now get the URI to send on my last step but I’m still stocked with this one.
The e-mail is sent but without attachments. I checked the sendgrid doc, the uri is in base64…
Any idea?

Hi @AdrienB

Did you manage to attach the pdf in sendgrid? I’m struggling with the same issue, I have followed all documentation on the web and nothing worked.

https://www.twilio.com/blog/sending-email-attachments-with-sendgrid

Even with the same example on sendgrid api documentation:
https://docs.sendgrid.com/api-reference/mail-send/mail-send

I assigned the encode64 data to a string variable, so I can test it without deploying… but no luck yet.

Just found the error, “attachments” object needs to be sent next to “personalizations”, not inside.

hope this helps.

Rgds.
Patrick

1 Like