Skip to content

Commit 54140ab

Browse files
Fix: Ensure consistent line rendering for cell borders
The issue reported that footer line widths were not appearing the same as header line widths, even when configured with the same numerical lineWidth value (e.g., 2) and color. This was likely due to the jsPDF draw color state not being explicitly re-applied immediately before drawing each border segment within the `drawCellBorders` function. While `lineWidth` was set, the `lineColor` relied on an earlier `applyStyles` call, which could lead to inconsistencies if the state changed. This commit modifies the `drawLine` helper function within `drawCellBorders` in `src/tableDrawer.ts` to: 1. Accept `cell.styles.lineColor` as a parameter. 2. Explicitly call `jsPDFDoc.setDrawColor()` with this color immediately before `jsPDFDoc.setLineWidth()` and the `jsPDFDoc.line()` command. This ensures that both line width and line color are correctly set in the jsPDF graphics state for each border segment (top, bottom, left, right) for all cells, improving rendering consistency across table sections (head, body, foot).
1 parent f3aac80 commit 54140ab

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/tableDrawer.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import autoTableText from './autoTableText'
22
import { addTableBorder, getFillStyle } from './common'
3-
import { LineWidths } from './config'
3+
import { LineWidths, Color } from './config'
44
import { DocHandler, jsPDFDocument } from './documentHandler'
55
import { Cell, Column, Pos, Row, Table } from './models'
66
import { assign } from './polyfills'
@@ -500,7 +500,7 @@ function drawCellBorders(
500500
if (lineWidth.left) {
501501
x1 -= 0.5 * lineWidth.left
502502
}
503-
drawLine(lineWidth.top, x1, y1, x2, y2)
503+
drawLine(lineWidth.top, x1, y1, x2, y2, cell.styles.lineColor)
504504
}
505505

506506
if (lineWidth.bottom) {
@@ -514,7 +514,7 @@ function drawCellBorders(
514514
if (lineWidth.left) {
515515
x1 -= 0.5 * lineWidth.left
516516
}
517-
drawLine(lineWidth.bottom, x1, y1, x2, y2)
517+
drawLine(lineWidth.bottom, x1, y1, x2, y2, cell.styles.lineColor)
518518
}
519519

520520
if (lineWidth.left) {
@@ -528,7 +528,7 @@ function drawCellBorders(
528528
if (lineWidth.bottom) {
529529
y2 += 0.5 * lineWidth.bottom
530530
}
531-
drawLine(lineWidth.left, x1, y1, x2, y2)
531+
drawLine(lineWidth.left, x1, y1, x2, y2, cell.styles.lineColor)
532532
}
533533

534534
if (lineWidth.right) {
@@ -542,7 +542,7 @@ function drawCellBorders(
542542
if (lineWidth.bottom) {
543543
y2 += 0.5 * lineWidth.bottom
544544
}
545-
drawLine(lineWidth.right, x1, y1, x2, y2)
545+
drawLine(lineWidth.right, x1, y1, x2, y2, cell.styles.lineColor)
546546
}
547547

548548
function drawLine(
@@ -551,9 +551,18 @@ function drawCellBorders(
551551
y1: number,
552552
x2: number,
553553
y2: number,
554+
lineColor: Color,
554555
) {
555-
doc.getDocument().setLineWidth(width)
556-
doc.getDocument().line(x1, y1, x2, y2, 'S')
556+
const jsPDFDoc = doc.getDocument()
557+
if (typeof lineColor === 'string') {
558+
jsPDFDoc.setDrawColor(lineColor)
559+
} else if (Array.isArray(lineColor)) {
560+
jsPDFDoc.setDrawColor(lineColor[0], lineColor[1], lineColor[2])
561+
} else if (typeof lineColor === 'number') {
562+
jsPDFDoc.setDrawColor(lineColor)
563+
}
564+
jsPDFDoc.setLineWidth(width)
565+
jsPDFDoc.line(x1, y1, x2, y2, 'S')
557566
}
558567
}
559568

0 commit comments

Comments
 (0)