# Uri format for Pdf to Sendgrid

**URL:** <https://community.weweb.io/t/uri-format-for-pdf-to-sendgrid/4229>\
**Category:** Ask us anything\
**Created:** [September 6, 2023, 2:41pm UTC](https://community.weweb.io/t/uri-format-for-pdf-to-sendgrid/4229 "2023-09-06T14:41:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdrienB](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@AdrienB](https://community.weweb.io/u/AdrienB)\
**Post date:** [September 6, 2023, 2:41pm UTC](https://community.weweb.io/t/uri-format-for-pdf-to-sendgrid/4229/1 "2023-09-06T14:41:42Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/0/056c08679aae519b17dd5bb9f74432efbdf6b0a2.png)

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

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/0/0c3324a7d3f9420f004ce7344fd08d9c2e1a6603.png)

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
> 
> ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/5/5d4d7a9c5de577421fd7261cf2c9e2116cbfca6c.png)

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…

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [September 6, 2023, 3:09pm UTC](https://community.weweb.io/t/uri-format-for-pdf-to-sendgrid/4229/2 "2023-09-06T15:09:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AdrienB](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@AdrienB](https://community.weweb.io/u/AdrienB)\
**Post date:** [September 6, 2023, 3:35pm UTC](https://community.weweb.io/t/uri-format-for-pdf-to-sendgrid/4229/3 "2023-09-06T15:35:55Z")

</div>

I use a script on header

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/b/b6c6d0212c6f5afd70a0579dbb3f460479b64adf.png)

But the pdf is generated…

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [September 6, 2023, 3:39pm UTC](https://community.weweb.io/t/uri-format-for-pdf-to-sendgrid/4229/4 "2023-09-06T15:39:15Z")

</div>

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](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/c/c5956b40fe5233e8f1987179745b640d4afabbd4.png)

![Screenshot 2023-09-06 at 17.37.47](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/c/c398e363e30ee10c26f7d4b74769b8b6f57b7d53.png)

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [September 6, 2023, 3:41pm UTC](https://community.weweb.io/t/uri-format-for-pdf-to-sendgrid/4229/5 "2023-09-06T15:41:11Z")

</div>

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.

> [@Integrate Google Maps via Script? (To get more parameters)](https://community.weweb.io/t/integrate-google-maps-via-script-to-get-more-parameters/3511/8):
>
> Works perfectly. I was able to integrate the map, use own markers, and even draw a calculated route on the map after 3 hours of playing around. Reallycool.

---

<div class="post-metadata">

**Author:** ![AdrienB](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@AdrienB](https://community.weweb.io/u/AdrienB)\
**Post date:** [September 6, 2023, 3:42pm UTC](https://community.weweb.io/t/uri-format-for-pdf-to-sendgrid/4229/6 "2023-09-06T15:42:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [September 6, 2023, 3:42pm UTC](https://community.weweb.io/t/uri-format-for-pdf-to-sendgrid/4229/7 "2023-09-06T15:42:48Z")

</div>

Could you maybe record a video with [Loom.com](http://Loom.com)? Explaining how it all works?

---

<div class="post-metadata">

**Author:** ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)\
**Post date:** [September 6, 2023, 5:45pm UTC](https://community.weweb.io/t/uri-format-for-pdf-to-sendgrid/4229/8 "2023-09-06T17:45:16Z")

</div>

In your first screenshot your code will always returns `undefined` as value to use in the next step.  
You need to [await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)

---

<div class="post-metadata">

**Author:** ![AdrienB](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@AdrienB](https://community.weweb.io/u/AdrienB)\
**Post date:** [September 7, 2023, 2:58pm UTC](https://community.weweb.io/t/uri-format-for-pdf-to-sendgrid/4229/9 "2023-09-07T14:58:55Z")

</div>

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?

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/a/a24062d3ffde7e8671a9f4695dec1c7a8805186c.png)

---

<div class="post-metadata">

**Author:** ![plucky00](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/plucky00/32/4726_2.png) [@plucky00](https://community.weweb.io/u/plucky00)\
**Post date:** [October 2, 2023, 1:58pm UTC](https://community.weweb.io/t/uri-format-for-pdf-to-sendgrid/4229/10 "2023-10-02T13:58:56Z")

</div>

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](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](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.

 ![Sample Sendgrid](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/6/63784f2f5bea12c0aa7d13ef9e8b15169f8ff7be.png)

---

<div class="post-metadata">

**Author:** ![plucky00](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/plucky00/32/4726_2.png) [@plucky00](https://community.weweb.io/u/plucky00)\
**Post date:** [October 3, 2023, 11:24am UTC](https://community.weweb.io/t/uri-format-for-pdf-to-sendgrid/4229/11 "2023-10-03T11:24:20Z")

</div>

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

hope this helps.

Rgds.  
Patrick
