# How to make PDF from HTML

**URL:** <https://discourse.nodered.org/t/how-to-make-pdf-from-html/67688>\
**Category:** General\
**Created:** [14 September 2022 08:51 UTC](https://discourse.nodered.org/t/how-to-make-pdf-from-html/67688 "2022-09-14T08:51:21Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 September 2022 09:46 UTC](https://discourse.nodered.org/t/how-to-make-pdf-from-html/67688/4 "2022-09-14T09:46:24Z")

</div>

> [@smitterer](#):
>
> I don't know if it'd be hard to combine a node-red module with a non-node-red node.

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/0/e0a4511d6374e364cea24fd49613d3ad40a7a110.gif)

The output...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/5/a58e48a0d104171eddb82ecf4d71807b7088841d.png)

The function...

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

```auto
[{"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":[]}]

```

---

_[View the full topic](https://discourse.nodered.org/t/how-to-make-pdf-from-html/67688)._
