How to make PDF from HTML

Not really. It is a client side node (needs a window and DOM) but that can be solved with other packages (jsdom).

Here is a demo...
Code_nV71JDWy9C

The output...

The function...

const { JSDOM } = jsdom;
const { window } = new JSDOM("");

const html = htmlToPdfmake(msg.payload, { window: window });

const docDefinition = {
    // a string or { width: number, height: number }
    pageSize: msg.pageSize || 'A4',

    // by default we use portrait, you can change it to landscape if you wish
    pageOrientation: msg.pageOrientation || 'portrait',

    // [left, top, right, bottom] or [horizontal, vertical] or just a number for equal margins
    pageMargins: [40, 60, 40, 60],

    content: [
        html
    ]
};
msg.payload = docDefinition
return msg

The Demo flow...

[{"id":"075ae101fed922d1","type":"pdfmake","z":"4c5ad8c7caa80822","name":"","outputType":"Buffer","inputProperty":"payload","options":"{}","outputProperty":"payload","x":1300,"y":980,"wires":[["4e692a74d7a1230c"]]},{"id":"11a2106c4a5c8d06","type":"function","z":"4c5ad8c7caa80822","name":"HTML to docDef","func":"const { JSDOM } = jsdom;\nconst { window } = new JSDOM(\"\");\n\nconst html = htmlToPdfmake(msg.payload, { window: window });\n\nconst docDefinition = {\n    // a string or { width: number, height: number }\n    pageSize: msg.pageSize || 'A4',\n\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, 60, 40, 60],\n\n    content: [\n        html\n    ]\n};\n\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":1300,"y":920,"wires":[["075ae101fed922d1"]]},{"id":"d75fa487a7253c78","type":"inject","z":"4c5ad8c7caa80822","name":"HTML, A6, Landscape","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"filename","v":"c:/temp/b1.pdf","vt":"str"},{"p":"pageOrientation","v":"landscape","vt":"str"},{"p":"pageSize","v":"A6","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"<div>       <h1>Different HTML</h1>      <table>         <tr>             <td>col 1</td> <td>col 2</td>         </tr>         <tr>             <td>A</td> <td>1</td>         </tr>         <tr>             <td>B</td> <td>2</td>         </tr>         <tr>             <td>C</td> <td>3</td>         </tr>     </table>  </div>","payloadType":"str","x":1080,"y":940,"wires":[["11a2106c4a5c8d06"]]},{"id":"4e692a74d7a1230c","type":"file","z":"4c5ad8c7caa80822","name":"","filename":"filename","filenameType":"msg","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":1320,"y":1040,"wires":[["9ba7ebda55b1d722"]]},{"id":"18e8442ae7da9bbc","type":"inject","z":"4c5ad8c7caa80822","name":"HTML, A5, Portait","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"filename","v":"c:/temp/a1.pdf","vt":"str"},{"p":"pageSize","v":"A5","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"<div> <h1>My title</h1> <p>     This is a sentence with a <strong>bold word</strong>, <em>one in italic</em>,     and <u>one with underline</u>. And finally <a href=\"https://www.google.com\">a link to google</a>. </p> </div>","payloadType":"str","x":1100,"y":900,"wires":[["11a2106c4a5c8d06"]]},{"id":"9ba7ebda55b1d722","type":"debug","z":"4c5ad8c7caa80822","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"filename","targetType":"msg","statusVal":"filename","statusType":"auto","x":1310,"y":1100,"wires":[]}]
2 Likes