Now that we’re back and up for air, I’ve built up an example flow that manually injects the sample data from your screenshot as an array of comma-separated values, then filters out any blank entries, and formats it for pdfmake. There’s a few node packages, the one I picked is:
https://flows.nodered.org/node/@platmac/node-red-pdfbuilder
EDIT: Use node-red-contrib-pdfmake (node) - Node-RED as suggested by @Beno, for these purposes it is functionally the same and it installs cleanly on Node-RED v3.1.7; I updated the example below to use this node.
Here’s what the output PDF and flow look like:
The main thing was getting the msg.payload “content” format right, then have the pdfmake node output base64 and also use base64 in the write file node.
To build the “content” I turned the one-dimensional string table into a multi-dimensional array so that each row itself is an array of each of the columns — that made it an easy drop-in for the pdfmake format, and as part of that loop I skip over any blank entries in the original table.
I put the two-dimensional array in its own property msg.table
so you can see what that looks like in the debug node.
Here’s the code from the function node, plus the example flow export at the end of the post.
(The imported version of the code has comments.)
var raw = msg.payload;
var table = [];
for(var i in raw) {
if(raw[i] === "") continue;
else {
var row = raw[i].split(",");
table.push(row);
}
}
msg.table = table;
msg.payload = {
content: [
{ text: 'Example table PDF\n\n', style: 'header' },
{
style: 'tableExample',
table: {
widths: ['*', '*', '*'],
body: table
}
}
]
}
return msg;
[{"id":"dc7f2e84100d34c5","type":"function","z":"4be5ce8c7e250d04","name":"format PDF","func":"// Save the raw incoming payload on a local variable\nvar raw = msg.payload;\n// The output will be a two dimensional array of non-blank entries\nvar table = [];\n// Loop through all of the raw incoming data; each entry holds one row of data\nfor(var i in raw) {\n // If this row is blank, skip over it...\n if(raw[i] === \"\") continue;\n // Otherwise separate out the columns that make up this row\n else {\n // Each row is separated by a comma, so use that to split into an array\n var row = raw[i].split(\",\");\n // Add it to the end of the table\n table.push(row);\n }\n}\n// Make table a message property for easy debugging\nmsg.table = table;\n\n// Create the payload for pdfmake\nmsg.payload = {\n // The payload must follow the `content:[..]` format\n content: [\n // Define a header, title, and add any other necessary information\n { text: 'Example table PDF\\n\\n', style: 'header' },\n // Separate section for the table\n {\n style: 'tableExample',\n // Create the table object itself\n table: {\n // Widths for each of the columns, using '*' to take as much space as possible\n widths: ['*', '*', '*'],\n // The body is just the two dimensional table defined in the loop above\n body: table\n }\n }\n ]\n}\nreturn msg; ","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":380,"wires":[["1d34b30a9a1be780","b1ff6ab02405aac7"]]},{"id":"ea44361c0ab9d0e5","type":"file","z":"4be5ce8c7e250d04","name":"write file","filename":"/home/dev/unsecured/PDF_test.pdf","filenameType":"str","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"base64","x":440,"y":440,"wires":[[]]},{"id":"1d34b30a9a1be780","type":"debug","z":"4be5ce8c7e250d04","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","l":false,"x":395,"y":380,"wires":[]},{"id":"b1ff6ab02405aac7","type":"pdfmake","z":"4be5ce8c7e250d04","name":"","outputType":"base64","inputProperty":"payload","options":"{}","outputProperty":"payload","x":240,"y":440,"wires":[["ea44361c0ab9d0e5"]]},{"id":"8b63b34e3e81ceab","type":"debug","z":"4be5ce8c7e250d04","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","l":false,"x":395,"y":320,"wires":[]},{"id":"c2f8603117aa8a9f","type":"inject","z":"4be5ce8c7e250d04","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","x":230,"y":320,"wires":[["dc7f2e84100d34c5","8b63b34e3e81ceab"]]}]