Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #966 - hex fillColor together with lineWidth causes invalid arguments for rect fn #978

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion examples/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,11 @@ examples.borders = function () {
margin: { top: 40 },
theme: 'plain',
headStyles: {
fillColor: [241, 196, 15],
fillColor: '#f1c40f',
fontSize: 15,
lineWidth: {
top: 1
}
},
footStyles: {
fillColor: [241, 196, 15],
Expand Down Expand Up @@ -601,6 +604,13 @@ examples.borders = function () {
bottom: 1,
}
}
if (data.row.section === "body" && data.row.index === 1 && data.cell === data.row.cells[1]) {
data.cell.styles.fillColor = '#f1c40f' // cell background color
data.cell.styles.lineColor = 'red' // cell border color
data.cell.styles.lineWidth = {
bottom: 1, // only bottom border will be painted
}
}
},
// Use this to add content to each page that has the autoTable on it. This can be page headers,
// page footers and page numbers for example.
Expand Down
15 changes: 14 additions & 1 deletion src/documentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,20 @@ export class DocHandler {
return this.jsPDFDocument.splitTextToSize(text, size, opts)
}

rect(x: number, y: number, width: number, height: number, fillStyle: string | null) {
/**
* Adds a rectangle to the PDF
* @param x Coordinate (in units declared at inception of PDF document) against left edge of the page
* @param y Coordinate (in units declared at inception of PDF document) against upper edge of the page
* @param width Width (in units declared at inception of PDF document)
* @param height Height (in units declared at inception of PDF document)
* @param fillStyle A string specifying the painting style or null. Valid styles include: 'S' [default] - stroke, 'F' - fill, and 'DF' (or 'FD') - fill then stroke. In "compat" API mode, a null value postpones setting the style so that a shape may be composed using multiple method calls. The last drawing method call used to define the shape should not have a null style argument. **In "advanced" API mode this parameter is deprecated.**
*/
rect(x: number, y: number, width: number, height: number, fillStyle: 'S' | 'F' | 'DF' | 'FD' | null) {
if (!['S', 'F', 'DF', 'FD', null].some((v) => v === fillStyle)) {
throw new TypeError(
`Invalid value '${fillStyle}' passed to rect. Allowed values are: 'S', 'F', 'DF', 'FD', null`
)
}
return this.jsPDFDocument.rect(x, y, width, height, fillStyle)
}

Expand Down
7 changes: 3 additions & 4 deletions src/tableDrawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,17 +400,16 @@ function drawCellBorders(doc: DocHandler, cell: Cell, cursor: Pos) {
* @param doc
* @param cell
* @param cursor
* @param fillColor - `false` for transparent, `string` for color, other types will use "F" from jsPDF.rect
* @param fillColor - passed to getFillStyle; `false` will map to transparent, `truthy` values to 'F' from jsPDF.rect
*/
function drawCellBackground(
doc: DocHandler,
cell: Cell,
cursor: Pos,
fillColor: Color
) {
const cellFillColor =
fillColor === false ? null : typeof fillColor !== 'string' ? 'F' : fillColor
doc.rect(cell.x, cursor.y, cell.width, cell.height, cellFillColor)
const fillStyle = getFillStyle(0, fillColor)
doc.rect(cell.x, cursor.y, cell.width, cell.height, fillStyle)
}

/**
Expand Down