Hi everyone,
@bakman2 helped me using a npm module in a function node, which is now working (Useage of contrib-npm)
I now have to dynamically change the files which need to be merged. Therefore I'm using some change nodes and a template to generate the code:
[
{
"id": "45c5e484f9cc2a79",
"type": "function",
"z": "4b453fb457938d95",
"name": "merge",
"func": "return msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [
{
"var": "pdfMergerJs",
"module": "pdf-merger-js"
},
{
"var": "pdfLib",
"module": "pdf-lib"
}
],
"x": 1110,
"y": 1000,
"wires": [
[
"8539b0d2158b0dfa"
]
]
},
{
"id": "8539b0d2158b0dfa",
"type": "debug",
"z": "4b453fb457938d95",
"name": "debug 41",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1140,
"y": 1080,
"wires": []
},
{
"id": "033b5c54563992b4",
"type": "template",
"z": "4b453fb457938d95",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "const merger = new pdfMergerJs();\n\n(async () => {\n{{files}};\n\n await merger.save('/media/qsl/merged.pdf'); //save under given name and reset the internal document\n})();\nreturn msg;",
"output": "str",
"x": 840,
"y": 980,
"wires": [
[
"45c5e484f9cc2a79",
"8539b0d2158b0dfa"
]
]
},
{
"id": "0ee65a7f2c1c0d23",
"type": "join",
"z": "4b453fb457938d95",
"name": "",
"mode": "custom",
"build": "string",
"property": "files",
"propertyType": "msg",
"key": "topic",
"joiner": "\\n",
"joinerType": "str",
"accumulate": false,
"timeout": "5",
"count": "",
"reduceRight": false,
"reduceExp": "",
"reduceInit": "",
"reduceInitType": "num",
"reduceFixup": "",
"x": 710,
"y": 820,
"wires": [
[
"033b5c54563992b4"
]
]
},
{
"id": "c1cc0d67422f0205",
"type": "change",
"z": "4b453fb457938d95",
"name": "",
"rules": [
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "\"file_\" & $.payload",
"tot": "jsonata"
},
{
"t": "set",
"p": "files",
"pt": "msg",
"to": "\" await merger.add('\" & $.payload & \"');\"",
"tot": "jsonata"
},
{
"t": "delete",
"p": "payload",
"pt": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 500,
"y": 820,
"wires": [
[
"0ee65a7f2c1c0d23"
]
]
},
{
"id": "de4390fe21e4fcc3",
"type": "fs-file-lister",
"z": "4b453fb457938d95",
"name": "",
"start": "/media/qsl/",
"pattern": "*.*",
"folders": "*",
"hidden": true,
"lstype": "files",
"path": true,
"single": false,
"depth": 0,
"stat": false,
"showWarnings": true,
"x": 280,
"y": 820,
"wires": [
[
"c1cc0d67422f0205"
]
]
},
{
"id": "a1f0df561ff43be4",
"type": "inject",
"z": "4b453fb457938d95",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 140,
"y": 820,
"wires": [
[
"de4390fe21e4fcc3"
]
]
}
]
However, this is not working.
My first question is, if it's even possible to work as I did it (putting out the code generated in the template node as a string and then just doing "return msg;" in the function node, where the required npm nodes are set).
The other question is, why do I get the following output from the template (all signs which are loaded from a mustache variable are not shown correct):
const merger = new pdfMergerJs();
(async () => {
await merger.add('/media/qsl/qsl1.pdf');
await merger.add('/media/qsl/qsl2.pdf');;
await merger.save('/media/qsl/merged.pdf'); //save under given name and reset the internal document
})();
return msg
Is this an encoding issue or do I have to change the template?
Thanks
EDIT: format of the template output is correct after using three { & } around the mustache variable instead of just two.
But the function is still not working.
EDIT2: changing the function to var payload = msg.payload; return msg;
creates an output from the function node, but it's not generating the merged pdf.