var pdfMake = require("pdfmake/build/pdfmake");
var pdfFonts = require("pdfmake/build/vfs_fonts");
pdfMake.vfs = pdfFonts.pdfMake.vfs;
var fs = require("fs");
var jsdom = require("jsdom");
var { JSDOM } = jsdom;
var { window } = new JSDOM("");
var htmlToPdfMake = require("./index.js");
//var util = require("util");
var html = htmlToPdfMake(`
Simple text
Title Level 1
Title Level 2
Title Level 3
Title Level 4
Title Level 5
Title Level 6
This is a sentence with a bold and purple word, one in italic, and one with underline. And finally a link.
An orange bold span with margins.
Below is a unordered list:
First item
Second item
With a sub unordered list:
Sub First bolded item
Sub Second underlined item
With a sub sub unordered list:
Sub Sub First item
Sub Sub Second item
With a sub order list:
Sub Item 1
Sub Item 2
With a sub sub ordered list
Sub Sub Item 1
Sub Sub Item 2
This sentence is surrended by BR
A first level ordered list with type "I":
Item 1
Item 2
Item 3
Text in bold.
This is a red span
<HR> with the default style:Below, another <HR> but with different style: left=120, width=300, color='red', margin=[0,20,0,20], thickness=2
Region
Result Q1
Result Q2
Americas
+3%
+6%
Europe
+3.9%
+5%
Asia
+1.5%
+0.9%
Header Column 1
Header Column 2
Value Column 1
Value Column 2
Col A
Col B
Col C
Col D
Cell A1
Cell B1 & B2
Cell C1
Cell D1 & D2
Cell A2
Cell C2
Cell A3
Cell B3 & C3
Cell D3
Cell A4 & A5 & B4 & B5 & C4 & C5
Cell D4
Cell D5
Table with widths=[100,"*","auto"] and heights=40 using "data-pdfmake" attribute
Cell1
Cell2
Cell3
Cell with red background
Cell
Cell with red borders
Table autosized based on style "height" and "width" using "tableAutoSize:true" option:
height:100px / width:250px
height:100px / width:'auto'
Here "<td width="100">" will use 250px for the width because we have to use the largest col's width
height:200px / width:'auto'
Change the table's layout (header with red border, body with blue border):
Header A
Header B
A1
B1
A2
B2
A3
B3
An image:
Bold italic centered text
text "bold" text "bold & italic" text "bold & italic & red" text "bold & italic" text "bold"
Below we preserve the spaces:
this is just an example.
And support for FONT tag.
`, {window:window, tableAutoSize:true});
/*var html = htmlToPdfMake(``, {window:window, tableAutoSize:true});
console.log(JSON.stringify(html))*/
var docDefinition = {
content: [
html
],
pageBreakBefore: function(currentNode) {
// we add a page break before elements with the classname "pdf-pagebreak-before"
return currentNode.style && currentNode.style.indexOf('pdf-pagebreak-before') > -1;
},
styles:{
red:{
color:'red'
},
blue:{
color:'blue'
},
bold:{
bold:true
},
'html-h6':{
color:'purple'
},
'html-strong':{
color:'purple'
},
'a':{
bold:true
},
'b':{
italics: true
},
'c':{
color:'red',
italics: false
},
'with-spaces':{
preserveLeadingSpaces: true
}
}
};
var pdfDocGenerator = pdfMake.createPdf(docDefinition, {
// see https://pdfmake.github.io/docs/0.1/document-definition-object/tables/
exampleLayout: {
hLineColor: function (rowIndex, node, colIndex) {
if (rowIndex === node.table.body.length) return 'blue';
return rowIndex <= 1 ? 'red' : '#dddddd';
},
vLineColor: function (colIndex, node, rowIndex) {
if (rowIndex === 0) return 'red';
return rowIndex > 0 && (colIndex === 0 || colIndex === node.table.body[0].length) ? 'blue' : 'black';
}
}
});
pdfDocGenerator.getBuffer(function(buffer) {
fs.writeFileSync('example.pdf', buffer);
console.log('--> example.pdf')
});