Generate PDF from HTML, no data without refreshing the page

Hey

Currently I am working on exporting data to a PDF file.
Creating the file is no problem. When I press the button "generate report", the file is created.
However, it does not contain any data (name, first name).

If I visit the template (localhost: 1880 / report-test), and reload the page manually, the data is written out as desired.

Is it possible to read in the data after pressing the button, so that the data is placed in the PDF when generating the file? I can't find a solution.

Note: I copied the "buffer" from a manual, I don't know if there are better options. (I only have limited (to no) knowledge of Javascript)

Thank you very much!

A part of my flow is shown below. This is only part of the entire flow. In my flow more data is added to the buffer / HTML, but the principle remains the same.

[{"id":"6bf9122b.78d5ac","type":"http in","z":"24d31ef4.bdf632","name":"Web: Rapport","url":"/rapport-Test","method":"get","upload":false,"swaggerDoc":"","x":330,"y":2460,"wires":[["67b06ed2.b6407"]]},{"id":"67b06ed2.b6407","type":"function","z":"24d31ef4.bdf632","name":"buffer some data","func":"context.rapport = context.rapport || new Object();\nswitch (msg.topic) {\n    case \"voornaam\":\n        context.rapport.voornaam = msg.payload;\n        msg = null;\n        break;\n    case \"achternaam\":\n        context.rapport.achternaam = msg.payload;\n        msg = null;\n        break;\n    default:\n    \tmsg.rapport = context.rapport;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":530,"y":2460,"wires":[["bfd31967.74f428"]]},{"id":"bfd31967.74f428","type":"template","z":"24d31ef4.bdf632","name":"HTML-template Rapport","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<!doctype html>\n\n<html lang=\"nl\">\n<head>\n  <meta charset=\"utf-8\">\n\n  <title>Hydroland: Rapport</title>\n  <meta name=\"Omschrijving\" content=\"Test Flow\">\n  <meta name=\"Auteur\" content=\"Arne\">\n\n<style>\n\tbody{\n\t  height: 200mm;\n\t  width: 190mm;\n\t  margin: 5mm 10mm 10mm 10mm;\n\t}\n\t\t\n\ttable {\n\t  width:100%;\n\t  border: 3 px;\n\t}\n\ttable, th, td {\n\t  border: 2px solid black;\n\t  border-collapse: collapse;\n\t}\n\tth, td {\n\t  padding: 15px;\n\t  text-align: left;\n\t  border: 0px;\n\t  \n\t}\n\t#rapport tr:nth-child(even) {\n\t  background-color: #eee;\n\t  border: 3px;\n\t}\n\t#.raport rapport tr:nth-child(odd) {\n\t background-color: #fff;\n\t border: 3px;\n\t}\n\t#rapport th {\n\t  background-color: black;\n\t  color: white;\n\t  text-align: center;\n\t}\n\t\n\tp.insprong {\n\t  margin-left: 5%;\n\t}\n\t\n\th1 { \n\t  display: block;\n\t  font-size: 2em;\n\t  margin-top: 0.67em;\n\t  margin-bottom: 0.67em;\n\t  margin-left: 0;\n\t  margin-right: 0;\n\t  font-weight: bold; \n\t  color: blue; \n\t}\n</style>\n\n</head>\n\n\n<body>  \n  <!-- Tabel persoonsgegevens --> \n\t<table id=\"rapport\">\n\t  <tr>\n\t\t<th colspan=\"2\">Persoonsgegevens</th>\n\t  </tr>\n\t  <tr>\n\t\t<td width=\"35%\">Voornaam:</td>\n\t\t<td>{{rapport.voornaam}}</td>\n\t  </tr>\n\t  <tr>\n\t\t<td>Achternaam:</td>\n\t\t<td>{{rapport.achternaam}}</td>\n\t  </tr>\n\t</table><br>\n  <!-- Einde Tabel persoonsgegevens --> \n</body>\n</html>","output":"str","x":770,"y":2460,"wires":[["60feb78b.5a6cb8","1a704985.0c3406"]]},{"id":"1a704985.0c3406","type":"function","z":"24d31ef4.bdf632","name":"Variabele bestandsnaam Rapport","func":"function formatDate(date) {\n    var d = new Date(date),\n        month = '' + (d.getMonth() + 1),\n        day = '' + d.getDate(),\n        year = d.getFullYear();\n\n    if (month.length < 2) month = '0' + month;\n    if (day.length < 2) day = '0' + day;\n\n    return [year, month, day].join('-');\n}\nvar tempString =formatDate(Date().toString());\n\nvar tempString1 = \"C:/Users/arne/Desktop/\";\nvar tempString2 = tempString1.concat(tempString);\nvar finalPath = tempString2.concat(\".pdf\");\nmsg.filename=finalPath;\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":800,"y":2520,"wires":[["5a5ca642.4a6d78","5269be58.327fb"]]},{"id":"60feb78b.5a6cb8","type":"http response","z":"24d31ef4.bdf632","name":"","statusCode":"","headers":{},"x":1030,"y":2460,"wires":[]},{"id":"5a5ca642.4a6d78","type":"HTML-PDF","z":"24d31ef4.bdf632","name":"HTML-PDF","output":"file","x":1050,"y":2520,"wires":[["de4f6bc9.fc7f18"]]},{"id":"de4f6bc9.fc7f18","type":"change","z":"24d31ef4.bdf632","name":"Opstellen meldingsparameters","rules":[{"t":"set","p":"topic","pt":"msg","to":"Rapport","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"Rapport succesvol aangemaakt.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":790,"y":2580,"wires":[["c420d11.96c333"]]},{"id":"c420d11.96c333","type":"ui_toast","z":"24d31ef4.bdf632","position":"top right","displayTime":"5","highlight":"green","sendall":false,"outputs":0,"ok":"OK","cancel":"","raw":false,"topic":"","name":"Melding: Rapport aangemaakt.","x":1110,"y":2580,"wires":[]},{"id":"814a1ca7.0cdcd","type":"ui_button","z":"24d31ef4.bdf632","name":"Raport genereren","group":"c4745898.da08e8","order":6,"width":0,"height":0,"passthru":false,"label":"Rapport","tooltip":"Druk op de knop om een rapport van de sessie te genereren.","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"tijd","x":350,"y":2520,"wires":[["bfd31967.74f428"]]},{"id":"6abbc1cf.fc71f","type":"ui_text_input","z":"24d31ef4.bdf632","name":"Voornaam","label":"Voornaam","tooltip":"","group":"51404579.1d62dc","order":0,"width":0,"height":0,"passthru":true,"mode":"text","delay":300,"topic":"voornaam","x":330,"y":2300,"wires":[["67b06ed2.b6407"]]},{"id":"131037f2.b02eb8","type":"ui_text_input","z":"24d31ef4.bdf632","name":"","label":"Achternaam","tooltip":"","group":"51404579.1d62dc","order":1,"width":0,"height":0,"passthru":true,"mode":"text","delay":300,"topic":"achternaam","x":330,"y":2340,"wires":[["67b06ed2.b6407"]]},{"id":"5269be58.327fb","type":"debug","z":"24d31ef4.bdf632","name":"Filename","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1040,"y":2400,"wires":[]},{"id":"c4745898.da08e8","type":"ui_group","z":"","name":"Instellingen","tab":"787c087.3687bf8","order":6,"disp":true,"width":"6","collapse":false},{"id":"51404579.1d62dc","type":"ui_group","z":"","name":"Persoonsgegevens","tab":"42c0724c.82676c","order":1,"disp":true,"width":"6","collapse":false},{"id":"787c087.3687bf8","type":"ui_tab","z":"","name":"Automatische Mode","icon":"loop","order":2,"disabled":false,"hidden":false},{"id":"42c0724c.82676c","type":"ui_tab","z":"","name":"Persoonsgegevens","icon":"group","order":1,"disabled":false,"hidden":false}]

each msg that passes thru a flow is independent of other messages. If you have two messages with data that needs to be combined, you need to use a join node or write the data from one msg to a place the next msg can get it from.

Before you go any further, you should read thru the documentation Working with messages. Pay partoicular attention to the section Message sequences

Hey

Thank you for your answer. The reason why I didn't use 'join' is because I can't pass the 'http in' in this way (I know this should work anyway, but I have to figure that out).

I suspect that 'generate report' in my flow must also be placed before my 'join node', so that it takes the data from the message into account when generating the file? This is where things go wrong.

Thank you for your time and effort!

EDIT:
Maybe I can provide a 'gate' just before creating the PDF? This should open (once) when pressing "generate report". The only problem is still that there is no data on the HTML without refreshing the page. If I can combine the "http in" with the "join node" of course.

Maybe you want to use the http-request node instead of the http-in and http-response nodes. Have the button connect to the http-request and then process the resulting data.

Take a look at the the documentation: https://cookbook.nodered.org/#http-requests

1 Like

Dear Zenofmud

Thanks for your comment. I have adjusted my flow and got it working.
The image below contains my flow.

  • Dutch --> English
  • Samenvoegen data -> Join data
  • Variabele bestandsnaam Rapport -> Variable filename Report
  • Opstellen meldingsparameters -> Generate notification parameters
  • Melding: Rapport aangemaakt -> Notification: Report created

Extra information:

1 Like

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