Skip to content

Commit

Permalink
v2.4.25
Browse files Browse the repository at this point in the history
Add support for rgba
  • Loading branch information
Aymkdn committed Aug 24, 2023
1 parent 1dd4b96 commit 46f36cf
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 19 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.4.24.js → docs/browser-2.4.25.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.4.24.js"></script>
<script src="browser-2.4.25.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.
37 changes: 22 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -868,16 +868,16 @@ function htmlToPdfMake(htmlText, options) {
// e.g. `#fff` or `#ff0048`
var haxRegex = new RegExp('^#([0-9a-f]{3}|[0-9a-f]{6})$');

// e.g. rgb(0,255,34) or rgb(22, 0, 0) or rgb(100%, 100%, 100%)
var rgbRegex = new RegExp('^rgb\\((\\d+(\\.\\d+)?%?),\\s*(\\d+(\\.\\d+)?%?),\\s*(\\d+(\\.\\d+)?%?)\\)$');
// e.g. rgb(0,255,34) or rgb(22, 0, 0) or rgb(100%, 100%, 100%) or rgba(0,125,250,0.8)
var rgbRegex = /^rgba?\(\s*(\d+(\.\d+)?%?),\s*(\d+(\.\d+)?%?),\s*(\d+(\.\d+)?%?)(,\s*\d+(\.\d+)?)?\)$/;

// e.g. hsl(300, 10%, 20%)
var hslRegex = new RegExp('^hsl\\((\\d+(\\.\\d+)?%?),\\s*(\\d+(\\.\\d+)?%?),\\s*(\\d+(\\.\\d+)?%?)\\)$');

// e.g. "white" or "red"
var nameRegex = new RegExp('^[a-z]+$');

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

if (haxRegex.test(color)) {
return color;
Expand All @@ -897,20 +897,27 @@ function htmlToPdfMake(htmlText, options) {
ret = [];
}
if (rgbRegex.test(color)) {
decimalColors = rgbRegex.exec(color).slice(1);
for (i = 0; i < 6; i+=2) {
decimalValue = decimalColors[i];
// if it ends with '%', we calculcate based on 100%=255
if (decimalValue.endsWith('%')) {
decimalValue = Math.round(decimalValue.slice(0,-1) * 255 / 100);
} else decimalValue = decimalValue*1;
if (decimalValue > 255) {
decimalValue = 255;
decimalColors = rgbRegex.exec(color).slice(1).filter(function(v,i) {
return i%2===0 && typeof v !== "undefined";
});

decimalColors.forEach(function(decimalValue, i) {
// for the alpha number
if (i === 3) {
hexString = Math.round(decimalValue.replace(",","") * 255).toString(16);
} else {
// if it ends with '%', we calculcate based on 100%=255
if (decimalValue.endsWith('%')) {
decimalValue = Math.round(decimalValue.slice(0,-1) * 255 / 100);
} else decimalValue = decimalValue*1;
if (decimalValue > 255) {
decimalValue = 255;
}
hexString = '0' + decimalValue.toString(16);
hexString = hexString.slice(-2);
}
hexString = '0' + decimalValue.toString(16);
hexString = hexString.slice(-2);
ret.push(hexString);
}
})
return '#' + ret.join('');
}
if (nameRegex.test(color)) return color;
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.4.24",
"version": "2.4.25",
"description": "Convert HTML code to PDFMake",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 10 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,16 @@ test("unit tests", function(t) {
t.finish();
});

t.test("parse RGBA color", function (t) {
var html = `<span style="color:rgba(230,55,55,0.8)">red</span>`;
var ret = htmlToPdfMake(html, {window: window});
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.finish();
});

t.test("parse RGB color with %", function (t) {
var html = `<span style="color:rgb(90.2%, 21.568%, 21.568%)">red</span>`;
var ret = htmlToPdfMake(html, {window: window});
Expand Down

0 comments on commit 46f36cf

Please sign in to comment.