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

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

[{"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

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.

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

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.

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 :smiley:
Thanks and kind regards,
Davide

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).

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

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

my attempt for this flow

[{"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":[]}]
3 Likes

Thanks!
It works like a charm!

Kind regards and thanks again,

Davide

1 Like

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