# How can I put a pdf file (or more) into a zip archive and send it via email?

**URL:** <https://discourse.nodered.org/t/how-can-i-put-a-pdf-file-or-more-into-a-zip-archive-and-send-it-via-email/75576>\
**Category:** General\
**Created:** [22 February 2023 09:58 UTC](https://discourse.nodered.org/t/how-can-i-put-a-pdf-file-or-more-into-a-zip-archive-and-send-it-via-email/75576 "2023-02-22T09:58:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DidoMarchet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/didomarchet/32/73199_2.png) [@DidoMarchet](https://discourse.nodered.org/u/DidoMarchet)\
**Post date:** [22 February 2023 09:58 UTC](https://discourse.nodered.org/t/how-can-i-put-a-pdf-file-or-more-into-a-zip-archive-and-send-it-via-email/75576/1 "2023-02-22T09:58:01Z")

</div>

Hi guys,  
I've a flow that generates a pdf and sends it as attachment via email.  
I would like to put this file into a zip archive using [node-red-contrib-zip (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-zip) and send the Zip via email (in the future will be more then 1 pdf).  
Do you know how can do it? Because I tried but with no success.  
If you have an example or any doc I'm reale thankful.  
Below the flow I use if it can be useful.

```auto
[{"id":"ac068b5c39f0e4a4","type":"template","z":"1917cb7f61a6fd19","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<!DOCTYPE html>\n<html>\n<head>\n\t<title>Fattura</title> \n</head>\n<body>\n\tMY CONTENT\n</body>\n</html>","output":"str","x":360,"y":640,"wires":[["d1c14d2c2167243d"]]},{"id":"1730ae45f578c58e","type":"inject","z":"1917cb7f61a6fd19","name":"Test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":640,"wires":[["ac068b5c39f0e4a4"]]},{"id":"7d6c780e12468f5a","type":"e-mail","z":"1917cb7f61a6fd19","server":"mail.smtp.com","port":"465","secure":true,"tls":true,"name":"mail@domain.com","dname":"Send Email","x":1270,"y":640,"wires":[]},{"id":"d1c14d2c2167243d","type":"function","z":"1917cb7f61a6fd19","name":"HTML to docDef","func":"const { JSDOM } = jsdom;\nconst { window } = new JSDOM(\"\");\n\nconst html = htmlToPdfmake(msg.payload, { \n tableAutoSize:true, \n window: window\n});\n\nconst docDefinition = {\n // a string or { width: number, height: number }\n pageSize: msg.pageSize || 'A4',\n // by default we use portrait, you can change it to landscape if you wish\n pageOrientation: msg.pageOrientation || 'portrait',\n\n // [left, top, right, bottom] or [horizontal, vertical] or just a number for equal margins\n pageMargins: [40, 40, 40, 40],\n\n content: [\n html\n],\n \n}\n\nmsg.filename = 'receipt.pdf'\n\nmsg.payload = docDefinition\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"htmlToPdfmake","module":"html-to-pdfmake"},{"var":"jsdom","module":"jsdom"}],"x":550,"y":640,"wires":[["9f9e45629034f036"]]},{"id":"9f9e45629034f036","type":"pdfmake","z":"1917cb7f61a6fd19","name":"","outputType":"Buffer","inputProperty":"payload","options":"{}","outputProperty":"payload","x":740,"y":640,"wires":[["d655251d175dd0fc"]]},{"id":"d655251d175dd0fc","type":"file","z":"1917cb7f61a6fd19","name":"","filename":"receipt.pdf","appendNewline":true,"createDir":false,"overwriteFile":"true","encoding":"none","x":910,"y":640,"wires":[["595d595c1ec44a20"]]},{"id":"595d595c1ec44a20","type":"function","z":"1917cb7f61a6fd19","name":"Set email","func":"msg.from = 'from@domain.com'\nmsg.topic = 'Email topic'\nmsg.payload = 'Email payload'\nmsg.attachments = [{ // use URL as an attachment\n filename: 'receipt.pdf',\n path: 'https://mydomain.com/receipt.pdf'\n}]\n \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1080,"y":640,"wires":[["7d6c780e12468f5a"]]}]

```

Thanks in advance and kind regards,

Davide

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [22 February 2023 10:42 UTC](https://discourse.nodered.org/t/how-can-i-put-a-pdf-file-or-more-into-a-zip-archive-and-send-it-via-email/75576/2 "2023-02-22T10:42:36Z")

</div>

Here's part of my Node-RED flow I use to send a single PDF via email.  
I've not tried to ZIP a set of PDFs, so cannot answer that part of your question.

 ![send_pdf](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/a/faba33247025e53eaa82b533c176252303d93bd4.png)

This is the JavaScript in the function node. You will need to alter the email-address, filename and path.

```auto
node.send(
    {
        payload: "Top line of your email body\n\n",
        topic: "This is your email subject heading",
        to: "fred.bloggs@somewhere.com",
        attachments: [{ filename: "newsletter_demo.pdf", path: "/home/pi/pdf/newsletter_demo.pdf" }]
    }
);

return null;

```

Here's a node to create ZIP files. I've not tried it myself, but I suspect other forum members probably have.

> **[node-red-contrib-zip](https://flows.nodered.org/node/node-red-contrib-zip)**
>
> Node-RED node to create and read ZIP files

---

<div class="post-metadata">

**Author:** ![DidoMarchet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/didomarchet/32/73199_2.png) [@DidoMarchet](https://discourse.nodered.org/u/DidoMarchet)\
**Post date:** [22 February 2023 13:06 UTC](https://discourse.nodered.org/t/how-can-i-put-a-pdf-file-or-more-into-a-zip-archive-and-send-it-via-email/75576/3 "2023-02-22T13:06:53Z")

</div>

Hi, thanks for the reply.  
I'm already sending PDF via email so I'm wait if someone has experience with ZIP.  
I'm going to edit my question because it's not clear 😃  
Thanks and kind regards,  
Davide

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [22 February 2023 13:17 UTC](https://discourse.nodered.org/t/how-can-i-put-a-pdf-file-or-more-into-a-zip-archive-and-send-it-via-email/75576/4 "2023-02-22T13:17:22Z")

</div>

Ok - no problem.  
Note: In the flow you posted I couldn't see how you prepared the file as an email attachment.  
I think you may have left a node out of the sequence (between receipt.pdf and Send Email).

 ![flow_sequnce](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/4644d1184349aff5bbfcc0f4038d540fb45f4672.png)

---

<div class="post-metadata">

**Author:** ![DidoMarchet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/didomarchet/32/73199_2.png) [@DidoMarchet](https://discourse.nodered.org/u/DidoMarchet)\
**Post date:** [22 February 2023 13:34 UTC](https://discourse.nodered.org/t/how-can-i-put-a-pdf-file-or-more-into-a-zip-archive-and-send-it-via-email/75576/5 "2023-02-22T13:34:58Z")

</div>

Thanks, you're right!  
I updated the code.  
Thanks again!

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [22 February 2023 15:25 UTC](https://discourse.nodered.org/t/how-can-i-put-a-pdf-file-or-more-into-a-zip-archive-and-send-it-via-email/75576/6 "2023-02-22T15:25:40Z")

</div>

> [@DidoMarchet](#):
>
> and send the Zip via email (in the future will be more then 1 pdf).

here is an example of how 2 x pdf files are passed through the zip node,  
in order to create the attachments array for the email node.

at the moment the **Join node** is set to create an array of the 2 msgs but if you dont know the amount of files you could change that to "After a timeout following the first message" `x` seconds

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/ea6c110d86e5a89e3419dcdc913502457e3ad4a5.png)

my attempt for this flow

```auto
[{"id":"7d6c780e12468f5a","type":"e-mail","z":"54efb553244c241f","server":"mail.smtp.com","port":"465","secure":true,"tls":true,"name":"mail@domain.com","dname":"Send Email","x":790,"y":2160,"wires":[]},{"id":"d655251d175dd0fc","type":"file","z":"54efb553244c241f","name":"","filename":"c:\\Share\\test1.pdf","filenameType":"str","appendNewline":true,"createDir":false,"overwriteFile":"true","encoding":"none","x":490,"y":1920,"wires":[["53f9a47c5fef7a1f","d4fb98a324ebc1e1"]]},{"id":"595d595c1ec44a20","type":"function","z":"54efb553244c241f","name":"Set email","func":"let zip = msg.payload; // buffer of zip\n\nmsg.from = 'from@domain.com'\nmsg.topic = 'Email topic'\nmsg.payload = 'Email payload'\n\nmsg.attachments = [{ \n filename: 'receipt.pdf',\n content: zip \n}]\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":2160,"wires":[["7d6c780e12468f5a","2f4b2adcc32f3d24"]]},{"id":"0ddd9c8d8bb7d428","type":"file","z":"54efb553244c241f","name":"","filename":"c:\\Share\\test2.pdf","filenameType":"str","appendNewline":true,"createDir":false,"overwriteFile":"true","encoding":"none","x":490,"y":2000,"wires":[["476d2190af712e42"]]},{"id":"40aa0652.d1bf88","type":"pdfmake","z":"54efb553244c241f","name":"","outputType":"Buffer","inputProperty":"payload","options":"{}","outputProperty":"payload","x":280,"y":1920,"wires":[["d655251d175dd0fc"]]},{"id":"30e9a280.04a94e","type":"inject","z":"54efb553244c241f","name":"test1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test1","payload":"{\"content\":[\"First pdf\",\"Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines\"]}","payloadType":"json","x":130,"y":1920,"wires":[["40aa0652.d1bf88"]]},{"id":"3e4b441c36ef29e5","type":"zip","z":"54efb553244c241f","name":"","mode":"compress","filename":"","compressionlevel":6,"outasstring":false,"x":340,"y":2160,"wires":[["9e2b13ead543ba29","595d595c1ec44a20"]]},{"id":"53f9a47c5fef7a1f","type":"debug","z":"54efb553244c241f","name":"debug 26","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":640,"y":1860,"wires":[]},{"id":"6d597e87a7e36af5","type":"join","z":"54efb553244c241f","name":"","mode":"custom","build":"array","property":"","propertyType":"full","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":970,"y":1960,"wires":[["6f597e739867c0e0","3e4b441c36ef29e5"]]},{"id":"58b83317ad3296a8","type":"pdfmake","z":"54efb553244c241f","name":"","outputType":"Buffer","inputProperty":"payload","options":"{}","outputProperty":"payload","x":280,"y":2000,"wires":[["0ddd9c8d8bb7d428"]]},{"id":"90cd35b7d17f4e98","type":"inject","z":"54efb553244c241f","name":"test2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test2","payload":"{\"content\":[\"Second pdf\",\"Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines\"]}","payloadType":"json","x":130,"y":2000,"wires":[["58b83317ad3296a8"]]},{"id":"6f597e739867c0e0","type":"debug","z":"54efb553244c241f","name":"debug 27","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1140,"y":1920,"wires":[]},{"id":"9e2b13ead543ba29","type":"debug","z":"54efb553244c241f","name":"debug 28","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":440,"y":2240,"wires":[]},{"id":"d4fb98a324ebc1e1","type":"change","z":"54efb553244c241f","name":"","rules":[{"t":"set","p":"filename","pt":"msg","to":"test1.pdf","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":1920,"wires":[["6d597e87a7e36af5"]]},{"id":"476d2190af712e42","type":"change","z":"54efb553244c241f","name":"","rules":[{"t":"set","p":"filename","pt":"msg","to":"test2.pdf","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":2000,"wires":[["6d597e87a7e36af5"]]},{"id":"2f4b2adcc32f3d24","type":"debug","z":"54efb553244c241f","name":"debug 29","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":740,"y":2240,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![DidoMarchet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/didomarchet/32/73199_2.png) [@DidoMarchet](https://discourse.nodered.org/u/DidoMarchet)\
**Post date:** [22 February 2023 15:58 UTC](https://discourse.nodered.org/t/how-can-i-put-a-pdf-file-or-more-into-a-zip-archive-and-send-it-via-email/75576/7 "2023-02-22T15:58:06Z")

</div>

Thanks!  
It works like a charm!

Kind regards and thanks again,

Davide

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [8 March 2023 15:58 UTC](https://discourse.nodered.org/t/how-can-i-put-a-pdf-file-or-more-into-a-zip-archive-and-send-it-via-email/75576/8 "2023-03-08T15:58:32Z")

</div>

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