Skip to content

Commit 3805f72

Browse files
committed
fixed rendering invalid color name #2872
1 parent 76f2c98 commit 3805f72

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
});
1313
```
1414
- Fixed extra blank page when using headerRows, dontBreakRows and cell pageBreak together
15+
- Fixed rendering of an invalid color name - previously it used the last valid color; now it defaults to black
1516

1617
## 0.3.7 - 2026-03-17
1718

src/PDFDocument.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ class PDFDocument extends PDFKit {
169169
return attachment;
170170
}
171171

172+
resolveColor(color, defaultColor) {
173+
color = color || defaultColor;
174+
175+
if (typeof this._normalizeColor === 'function') {
176+
if (this._normalizeColor(color) === null) { // color is not valid
177+
return defaultColor;
178+
}
179+
}
180+
181+
return color;
182+
}
183+
172184
setOpenActionAsPrint() {
173185
let printActionRef = this.ref({
174186
Type: 'Action',

src/Renderer.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class Renderer {
188188

189189
let opacity = isNumber(inline.opacity) ? inline.opacity : 1;
190190
this.pdfDocument.opacity(opacity);
191-
this.pdfDocument.fill(inline.color || 'black');
191+
this.pdfDocument.fill(this.pdfDocument.resolveColor(inline.color, 'black'));
192192

193193
this.pdfDocument._font = inline.font;
194194
this.pdfDocument.fontSize(inline.fontSize);
@@ -287,14 +287,14 @@ class Renderer {
287287
let strokeOpacity = isNumber(vector.strokeOpacity) ? vector.strokeOpacity : 1;
288288

289289
if (vector.color && vector.lineColor) {
290-
this.pdfDocument.fillColor(vector.color, fillOpacity);
291-
this.pdfDocument.strokeColor(vector.lineColor, strokeOpacity);
290+
this.pdfDocument.fillColor(this.pdfDocument.resolveColor(vector.color, 'black'), fillOpacity);
291+
this.pdfDocument.strokeColor(this.pdfDocument.resolveColor(vector.lineColor, 'black'), strokeOpacity);
292292
this.pdfDocument.fillAndStroke();
293293
} else if (vector.color) {
294-
this.pdfDocument.fillColor(vector.color, fillOpacity);
294+
this.pdfDocument.fillColor(this.pdfDocument.resolveColor(vector.color, 'black'), fillOpacity);
295295
this.pdfDocument.fill();
296296
} else {
297-
this.pdfDocument.strokeColor(vector.lineColor || 'black', strokeOpacity);
297+
this.pdfDocument.strokeColor(this.pdfDocument.resolveColor(vector.lineColor, 'black'), strokeOpacity);
298298
this.pdfDocument.stroke();
299299
}
300300
}
@@ -436,7 +436,7 @@ class Renderer {
436436
renderWatermark(page) {
437437
let watermark = page.watermark;
438438

439-
this.pdfDocument.fill(watermark.color);
439+
this.pdfDocument.fill(this.pdfDocument.resolveColor(watermark.color, 'black'));
440440
this.pdfDocument.opacity(watermark.opacity);
441441

442442
this.pdfDocument.save();

tests/unit/PDFDocument.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,29 @@ describe('PDFDocument', function () {
5757
// TODO
5858

5959
});
60+
61+
describe('resolveColor', function () {
62+
63+
it('should resolve valid color name', function () {
64+
assert.equal(pdfDocument.resolveColor('red'), 'red');
65+
});
66+
67+
it('should resolve default color', function () {
68+
assert.equal(pdfDocument.resolveColor(undefined, 'red'), 'red');
69+
});
70+
71+
it('should resolve hex color', function () {
72+
assert.equal(pdfDocument.resolveColor('#f900f8'), '#f900f8');
73+
});
74+
75+
it('should resolve invalid color name', function () {
76+
assert.equal(pdfDocument.resolveColor('invalid'), null);
77+
});
78+
79+
it('should resolve invalid color name with default', function () {
80+
assert.equal(pdfDocument.resolveColor('invalid', 'red'), 'red');
81+
});
82+
83+
});
84+
6085
});

0 commit comments

Comments
 (0)