Skip to content

Commit

Permalink
v2.5.12
Browse files Browse the repository at this point in the history
  • Loading branch information
Aymkdn committed Jul 16, 2024
1 parent 0ca0000 commit 87b7bf8
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/browser-2.5.11.js → docs/browser-2.5.12.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ <h1>HTML to PDFMake convertor</h1>
<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>
</div>
</div>
<script src="browser-2.5.11.js"></script>
<script src="browser-2.5.12.js"></script>
<script src="https://cdn.jsdelivr.net/npm/pdfmake@latest/build/pdfmake.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/pdfmake@latest/build/vfs_fonts.js"></script>
<script>
Expand Down
Binary file modified example.pdf
Binary file not shown.
33 changes: 18 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ function htmlToPdfMake(htmlText, options) {
// check if we have 'color' or 'size' -- mainly for '<font>'
var color = element.getAttribute("color");
if (color) {
ret.push({key:"color", value:this.parseColor(color)});
ret.push({key:"color", value:this.parseColor(color).color});
}
var size = element.getAttribute("size");
if (size !== null) {
Expand All @@ -762,6 +762,7 @@ function htmlToPdfMake(htmlText, options) {
if (styleDef.length===2) {
var key = styleDef[0].trim().toLowerCase();
var value = styleDef[1].trim();
var res;
if (_this.ignoreStyles.indexOf(key) === -1) {
switch (key) {
case "margin": {
Expand Down Expand Up @@ -822,12 +823,16 @@ function htmlToPdfMake(htmlText, options) {
break;
}
case "color": {
ret.push({key:"color", value:_this.parseColor(value)})
res = _this.parseColor(value);
ret.push({key:"color", value:res.color});
if (res.opacity < 1) ret.push({key:"opacity", value:res.opacity});
break;
}
case "background-color": {
// if TH/TD and key is 'background', then we use 'fillColor' instead
ret.push({key:(nodeName === 'TD' || nodeName === 'TH' ? "fillColor" : "background"), value:_this.parseColor(value)})
res = _this.parseColor(value);
ret.push({key:(nodeName === 'TD' || nodeName === 'TH' ? "fillColor" : "background"), value:res.color});
if (res.opacity < 1) ret.push({key:(nodeName === 'TD' || nodeName === 'TH' ? "fillOpacity" : "opacity"), value:res.opacity});
break;
}
case "text-indent": {
Expand Down Expand Up @@ -896,9 +901,9 @@ function htmlToPdfMake(htmlText, options) {
if (properties.length > 2) {
var color = properties.slice(2).join(' ');
if (index > -1) {
borderColor[index] = _this.parseColor(color);
borderColor[index] = _this.parseColor(color).color;
} else {
for (i=0; i<4; i++) borderColor[i] = _this.parseColor(color);
for (i=0; i<4; i++) borderColor[i] = _this.parseColor(color).color;
}
}
});
Expand Down Expand Up @@ -933,9 +938,10 @@ function htmlToPdfMake(htmlText, options) {
* Also tries to convert RGB colors into hex values
*
* @param color color as string representation
* @returns color as hex values for pdfmake
* @returns {color (as hex values for pdfmake), opacity}
*/
this.parseColor = function(color) {
var opacity = 1;
// e.g. `#fff` or `#ff0048`
var haxRegex = new RegExp('^#([0-9a-f]{3}|[0-9a-f]{6})$', 'i');

Expand All @@ -949,9 +955,8 @@ function htmlToPdfMake(htmlText, options) {
var nameRegex = new RegExp('^[a-z]+$', 'i');

var decimalColors, decimalValue, hexString, ret=[];

if (haxRegex.test(color)) {
return color;
return {color:color, opacity:opacity};
}

if (hslRegex.test(color)) {
Expand All @@ -975,9 +980,7 @@ function htmlToPdfMake(htmlText, options) {
decimalColors.forEach(function(decimalValue, i) {
// for the alpha number
if (i === 3) {
hexString = Math.round(decimalValue.replace(",","") * 255).toString(16);
// when the alpha is 1 == FF, we should not use it
if (hexString === "ff") hexString="";
opacity = decimalValue.slice(1)*1;
} else {
// if it ends with '%', we calculcate based on 100%=255
if (decimalValue.endsWith('%')) {
Expand All @@ -988,15 +991,15 @@ function htmlToPdfMake(htmlText, options) {
}
hexString = '0' + decimalValue.toString(16);
hexString = hexString.slice(-2);
ret.push(hexString);
}
ret.push(hexString);
})
return '#' + ret.join('');
return {color:'#' + ret.join(''), opacity:opacity};
}
if (nameRegex.test(color)) return color;
if (nameRegex.test(color)) return {color:color, opacity:opacity};

console.error('Could not parse color "' + color + '"');
return color;
return {color:color, opacity:opacity};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "html-to-pdfmake",
"version": "2.5.11",
"version": "2.5.12",
"description": "Convert HTML code to PDFMake",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ test("unit tests", function(t) {
if (debug) console.log(JSON.stringify(ret));
t.check(Array.isArray(ret) && ret.length === 1, "return is OK");
ret = ret[0];
t.check(ret.text === "red" && ret.color === "#e63737cc", "color:rgba(230,55,55,0.8)");
t.check(ret.text === "red" && ret.color === "#e63737" && ret.opacity === 0.8, "color:rgba(230,55,55,0.8)" );
t.finish();
});

Expand Down

0 comments on commit 87b7bf8

Please sign in to comment.