# html-to-pdfmake [pdfmake](https://pdfmake.github.io/docs/) permits to easily create a PDF with JavaScript; however there is no support of HTML code, so I decided to create a module to handle this feature. ## Online Demo You can find the online demo at https://aymkdn.github.io/html-to-pdfmake/index.html ## How to use This module will convert some basic and valid HTML code to its equivalent in *pdfmake*. ### Node ```bash npm install html-to-pdfmake ``` ```javascript var htmlToPdfmake = require("html-to-pdfmake"); // or: // import htmlToPdfmake from "html-to-pdfmake" ``` Example: ```javascript var pdfMake = require("pdfmake/build/pdfmake"); var pdfFonts = require("pdfmake/build/vfs_fonts"); pdfMake.vfs = pdfFonts.pdfMake.vfs; var htmlToPdfmake = require("html-to-pdfmake"); var html = htmlToPdfmake(`
This is a sentence with a bold word, one in italic, and one with underline. And finally a link.
height:100px / width:250px | height:100px / width:'auto' |
Here it will use 250px for the width because we have to use the largest col's width | height:200px / width:'auto' |
Lorem Ipsum is simply d-ummy text of th-e printing and typese-tting industry. Lorem Ipsum has b-een the industry's standard dummy text ever since the 1500s
`, { replaceText:function(text, nodes) { // 'nodes' contains all the parent nodes for the text return text.replace(/-/g, "\\u2011"); // it will replace any occurrence of '-' with '\\u2011' in "Lorem Ipsum is simply d-ummy text […] dummy text ever since the 1500s" } }); ``` #### `customTag` If your HTML code doesn't use regular HTML tags, then you can use `customTag` to define your own result. Example with a QR code generator: ```js var html = htmlToPdfMake(`texto in code
`, {,
customTag:function(params) {
var ret = params.ret;
var element = params.element;
var parents = params.parents;
switch(ret.nodeName) {
case "CODE": {
ret = this.applyStyle({ret:ret, parents:parents.concat([element])});
ret.qr = ret.text[0].text;
switch(element.getAttribute("typecode")){
case 'QR':
delete ret.text;
ret.nodeName='QR';
if(!ret.style || !Array.isArray(ret.style)){
ret.style = [];
}
ret.style.push('html-qr');
break;
}
break;
}
}
return ret;
}
});
```
### HTML tags supported
The below HTML tags are supported:
- `A` (with external and internal links)
- `DIV` / `P` / `SPAN`
- `B` / `STRONG`
- `I` / `EM`
- `S`
- `UL` / `OL` / `LI`
- `TABLE` / `THEAD` / `TBODY` / `TFOOTER` / `TR` / `TH` / `TD`
- `H1` to `H6`
- `FONT`
- `IMG`
- `SVG`
- `SUP` / `SUB`
### CSS properties supported
CSS can create very complex design, however this framework can only handle the most simple HTML / CSS. The support of CSS style is limited and might not work in all cases with all values:
- `background-color`
- `border`
- `color`
- `font-family`
- `font-style` (with `italic`)
- `font-weight` (with `bold`)
- `height`
- `margin`
- `text-align`
- `text-decoration`
- `text-indent`
- `white-space` (with `break-spaces` and `pre*`)
- `width`
### Default styles
I've defined some default styles for the supported element.
For example, using a <STRONG> will display the word in **bold**. Or, a link will appear in blue with an underline, and so on...
Here is the list of defaults styles:
```javascript
{
b: {bold:true},
strong: {bold:true},
u: {decoration:'underline'},
s: {decoration: 'lineThrough'},
em: {italics:true},
i: {italics:true},
h1: {fontSize:24, bold:true, marginBottom:5},
h2: {fontSize:22, bold:true, marginBottom:5},
h3: {fontSize:20, bold:true, marginBottom:5},
h4: {fontSize:18, bold:true, marginBottom:5},
h5: {fontSize:16, bold:true, marginBottom:5},
h6: {fontSize:14, bold:true, marginBottom:5},
a: {color:'blue', decoration:'underline'},
strike: {decoration: 'lineThrough'},
p: {margin:[0, 5, 0, 10]},
ul: {marginBottom:5},
li: {marginLeft:5},
table: {marginBottom:5},
th: {bold:true, fillColor:'#EEEEEE'}
}
```
For the old HTML4 tag ``, the `size` attributes can have a value from 1 to 7, which will be converted to 10pt, 14pt, 16pt, 18pt, 20pt, 24pt, or 28pt.
**Please, note that the above default styles are stronger than the ones defined in the style classes.** Read below how to overwrite them.
### Customize style
Each converted element will have an associated style-class called `html-tagname`.
For example, if you want all <STRONG> tags to be highlighted with a yellow backgroud you can use `html-strong` in the `styles` definition:
```javascript
var html = htmlToPdfmake(`
This sentence has a highlighted word, but not only.
`); var docDefinition = { content: [ html ], styles:{ 'html-strong':{ background:'yellow' // it will add a yellow background to all elements } } }; var pdfDocGenerator = pdfMake.createPdf(docDefinition); ``` ### CSS class and style The `class` and `styles` for the elements will also be added. ```javascript var html = htmlToPdfmake(`This sentence has a bold and red word.
`); /* It returns: { text: [ { text: 'This sentence has ' }, { text: 'a bold and red word', style: ['red', 'html-span'], // 'red' added because of `class="red"` bold: true // added because of `style="font-weight:bold"` }, { text: '.' } ], margin: [0, 5, 0, 10], style: ['html-p'] } */ var docDefinition = { content: [ html ], styles:{ red:{ // we define the class called "red" color:'red' } } }; var pdfDocGenerator = pdfMake.createPdf(docDefinition); ``` **Please, note that the default styles are stronger than the ones defined in the style classes.** For example, if you define a class `html-a` to change all links in *purple*, then it won't work because the default styles will overwrite it: ```javascript var docDefinition = { content: [ html ], styles:{ 'html-a':{ color:'purple' // it won't work: all links will remain 'blue' } } }; ``` To make it work, you have to either delete the default styles, or change it with a new value. Starting `v1.1.0`, an option parameter is available as a second parameter. Example: you want `This is my paragraph on page 1.
This is my paragraph on page 2.
Table with widths=[100,"*","auto"] and heights=40 | ||
Cell1 | Cell2 | Cell3 |