Skip to content

Commit 26dc771

Browse files
committed
2.5.31
- Added more support for font-weight - Fixed issue when issue "transparent" for the background color
1 parent d07d039 commit 26dc771

File tree

9 files changed

+37
-13
lines changed

9 files changed

+37
-13
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Try it live with the [online demo](https://aymkdn.github.io/html-to-pdfmake/inde
5252
</html>
5353
```
5454

55-
### Node.js Usage
55+
### Node based Project Usage
5656

5757
```bash
5858
npm install html-to-pdfmake jsdom
@@ -62,14 +62,15 @@ npm install html-to-pdfmake jsdom
6262
const pdfMake = require('pdfmake/build/pdfmake');
6363
const pdfFonts = require('pdfmake/build/vfs_fonts');
6464
const htmlToPdfmake = require('html-to-pdfmake');
65+
// if you need to run it in a terminal console using "node", then you need the below two lines:
6566
const jsdom = require('jsdom');
6667
const { JSDOM } = jsdom;
6768

6869
// the below line may vary depending on your version of PDFMake
6970
// please, check https://github.com/bpampuch/pdfmake to know how to initialize this library
7071
pdfMake.vfs = pdfFonts;
7172

72-
// initiate the "window" object in Node
73+
// if you need to run it in a terminal console using "node", then you need to initiate the "window" object with the below line:
7374
const { window } = new JSDOM('');
7475

7576
// Convert HTML to PDFMake format
@@ -85,6 +86,7 @@ const docDefinition = { content: converted };
8586

8687
// Generate PDF
8788
pdfMake.createPdf(docDefinition).getBuffer((buffer) => {
89+
// when running the command in a terminal console using "node", then we can save the file using the 'fs' native package
8890
require('fs').writeFileSync('output.pdf', buffer);
8991
});
9092
```

browser.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/browser-2.5.30.js renamed to docs/browser-2.5.31.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ <h1>HTML to PDFMake convertor</h1>
116116
<div id="pdf_ie" style="display:none;padding:3em">The PDF file is sent to you for download. Use a modern browser (like Chrome or Firefox) to display the PDF in this page.</div>
117117
</div>
118118
</div>
119-
<script src="browser-2.5.30.js"></script>
119+
<script src="browser-2.5.31.js"></script>
120120
<script src="https://cdn.jsdelivr.net/npm/pdfmake@latest/build/pdfmake.min.js"></script>
121121
<script src="https://cdn.jsdelivr.net/npm/pdfmake@latest/build/vfs_fonts.js"></script>
122122
<script>

example.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var pdfMake = require("pdfmake/build/pdfmake");
22
var pdfFonts = require("pdfmake/build/vfs_fonts");
3-
pdfMake.vfs = pdfFonts.pdfMake.vfs;
3+
pdfMake.vfs = pdfFonts;
44
var fs = require("fs");
55
var jsdom = require("jsdom");
66
var { JSDOM } = jsdom;
@@ -222,8 +222,8 @@ var html = htmlToPdfMake(`
222222
<div>And support for <font color="blue" size="3">FONT</font> tag.</div>
223223
`, {window:window, tableAutoSize:true});
224224

225-
/*var html = htmlToPdfMake(``, {window:window});
226-
console.log(JSON.stringify(html))*/
225+
// var html = htmlToPdfMake(``, {window:window});
226+
// console.log(JSON.stringify(html))
227227

228228
var docDefinition = {
229229
content: [

example.pdf

609 Bytes
Binary file not shown.

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,8 @@ function htmlToPdfMake(htmlText, options) {
834834
break;
835835
}
836836
case "font-weight": {
837-
if (value === "bold") ret.push({key:"bold", value:true});
837+
if (value === "bold" || value >= 700) ret.push({key:"bold", value:true});
838+
else ret.push({ key: "bold", value: false });
838839
break;
839840
}
840841
case "text-decoration": {
@@ -868,8 +869,11 @@ function htmlToPdfMake(htmlText, options) {
868869
case "background-color": {
869870
// if TH/TD and key is 'background', then we use 'fillColor' instead
870871
res = _this.parseColor(value);
871-
ret.push({key:(nodeName === 'TD' || nodeName === 'TH' ? "fillColor" : "background"), value:res.color});
872-
if (res.opacity < 1) ret.push({key:(nodeName === 'TD' || nodeName === 'TH' ? "fillOpacity" : "opacity"), value:res.opacity});
872+
// if the color is "transparent", we ignore it
873+
if (res.color !== "transparent") {
874+
ret.push({key:(nodeName === 'TD' || nodeName === 'TH' ? "fillColor" : "background"), value:res.color});
875+
if (res.opacity < 1) ret.push({key:(nodeName === 'TD' || nodeName === 'TH' ? "fillOpacity" : "opacity"), value:res.opacity});
876+
}
873877
break;
874878
}
875879
case "text-indent": {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-to-pdfmake",
3-
"version": "2.5.30",
3+
"version": "2.5.31",
44
"description": "Convert HTML code to PDFMake",
55
"main": "index.js",
66
"scripts": {
@@ -22,7 +22,7 @@
2222
"devDependencies": {
2323
"eslint": "^5.16.0",
2424
"jsdom": "^16.6.0",
25-
"pdfmake": "^0.2.7",
25+
"pdfmake": "^0.2.20",
2626
"simple-test-framework": "^0.2.1",
2727
"svg-to-pdfkit": "^0.1.8"
2828
}

test/unit.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ test("unit tests", function(t) {
3636
t.finish();
3737
})
3838

39+
t.test("font-weight", function (t) {
40+
var ret = htmlToPdfMake(`<div><span style="font-weight: bold">Bold</span><span style="font-weight:700">700<span style="font-weight: normal">normal</span></span></div>`, { window: window });
41+
if (debug) console.log(JSON.stringify(ret));
42+
t.check(Array.isArray(ret) && ret.length === 1, "return is OK");
43+
ret = ret[0].text;
44+
t.check(
45+
ret[0].text === "Bold" &&
46+
ret[0].bold === true &&
47+
Array.isArray(ret[1].text) &&
48+
ret[1].text[0].text === "700" &&
49+
ret[1].text[0].bold === true &&
50+
ret[1].text[1].text === "normal" &&
51+
ret[1].text[1].bold === false &&
52+
"font-weight");
53+
54+
t.finish();
55+
})
56+
3957
t.test("u",function(t) {
4058
var ret = htmlToPdfMake("<u>underline word</u>", {window:window});
4159
if (debug) console.log(JSON.stringify(ret));

0 commit comments

Comments
 (0)