jspdf-autotable
Version:
Generate pdf tables with javascript (jsPDF plugin)
1,307 lines (1,259 loc) • 91.4 kB
JavaScript
/*!
*
* jsPDF AutoTable plugin v5.0.7
*
* Copyright (c) 2026 Simon Bengtsson, https://github.com/simonbengtsson/jsPDF-AutoTable
* Licensed under the MIT License.
* http://opensource.org/licenses/mit-license
*
*/
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else {
var a = factory();
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(Object(typeof globalThis !== 'undefined' ? globalThis : typeof this !== 'undefined' ? this : typeof window !== 'undefined' ? window : typeof self !== 'undefined' ? self : global), function() {
return /******/ (function() { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ 28:
/***/ (function(__unused_webpack_module, exports) {
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.HtmlRowInput = void 0;
exports.defaultStyles = defaultStyles;
exports.getTheme = getTheme;
var HtmlRowInput = /** @class */ (function (_super) {
__extends(HtmlRowInput, _super);
function HtmlRowInput(element) {
var _this = _super.call(this) || this;
_this._element = element;
return _this;
}
return HtmlRowInput;
}(Array));
exports.HtmlRowInput = HtmlRowInput;
// Base style for all themes
function defaultStyles(scaleFactor) {
return {
font: 'helvetica', // helvetica, times, courier
fontStyle: 'normal', // normal, bold, italic, bolditalic
overflow: 'linebreak', // linebreak, ellipsize, visible or hidden
fillColor: false, // Either false for transparent, rbg array e.g. [255, 255, 255] or gray level e.g 200
textColor: 20,
halign: 'left', // left, center, right, justify
valign: 'top', // top, middle, bottom
fontSize: 10,
cellPadding: 5 / scaleFactor, // number or {top,left,right,left,vertical,horizontal}
lineColor: 200,
lineWidth: 0,
cellWidth: 'auto', // 'auto'|'wrap'|number
minCellHeight: 0,
minCellWidth: 0,
};
}
function getTheme(name) {
var themes = {
striped: {
table: { fillColor: 255, textColor: 80, fontStyle: 'normal' },
head: { textColor: 255, fillColor: [41, 128, 185], fontStyle: 'bold' },
body: {},
foot: { textColor: 255, fillColor: [41, 128, 185], fontStyle: 'bold' },
alternateRow: { fillColor: 245 },
},
grid: {
table: {
fillColor: 255,
textColor: 80,
fontStyle: 'normal',
lineWidth: 0.1,
},
head: {
textColor: 255,
fillColor: [26, 188, 156],
fontStyle: 'bold',
lineWidth: 0,
},
body: {},
foot: {
textColor: 255,
fillColor: [26, 188, 156],
fontStyle: 'bold',
lineWidth: 0,
},
alternateRow: {},
},
plain: { head: { fontStyle: 'bold' }, foot: { fontStyle: 'bold' } },
};
return themes[name];
}
/***/ }),
/***/ 150:
/***/ (function(__unused_webpack_module, exports) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports["default"] = default_1;
/**
* Improved text function with halign and valign support
* Inspiration from: http://stackoverflow.com/questions/28327510/align-text-right-using-jspdf/28433113#28433113
*/
function default_1(text, x, y, styles, doc) {
styles = styles || {};
var PHYSICAL_LINE_HEIGHT = 1.15;
var k = doc.internal.scaleFactor;
var fontSize = doc.internal.getFontSize() / k;
var lineHeightFactor = doc.getLineHeightFactor
? doc.getLineHeightFactor()
: PHYSICAL_LINE_HEIGHT;
var lineHeight = fontSize * lineHeightFactor;
var splitRegex = /\r\n|\r|\n/g;
var splitText = '';
var lineCount = 1;
if (styles.valign === 'middle' ||
styles.valign === 'bottom' ||
styles.halign === 'center' ||
styles.halign === 'right') {
splitText = typeof text === 'string' ? text.split(splitRegex) : text;
lineCount = splitText.length || 1;
}
// Align the top
y += fontSize * (2 - PHYSICAL_LINE_HEIGHT);
if (styles.valign === 'middle')
y -= (lineCount / 2) * lineHeight;
else if (styles.valign === 'bottom')
y -= lineCount * lineHeight;
if (styles.halign === 'center' || styles.halign === 'right') {
var alignSize = fontSize;
if (styles.halign === 'center')
alignSize *= 0.5;
if (splitText && lineCount >= 1) {
for (var iLine = 0; iLine < splitText.length; iLine++) {
doc.text(splitText[iLine], x - doc.getStringUnitWidth(splitText[iLine]) * alignSize, y);
y += lineHeight;
}
return doc;
}
x -= doc.getStringUnitWidth(text) * alignSize;
}
if (styles.halign === 'justify') {
doc.text(text, x, y, { maxWidth: styles.maxWidth || 100, align: 'justify' });
}
else {
doc.text(text, x, y);
}
return doc;
}
/***/ }),
/***/ 152:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.calculateWidths = calculateWidths;
exports.resizeColumns = resizeColumns;
exports.ellipsize = ellipsize;
var common_1 = __webpack_require__(799);
/**
* Calculate the column widths
*/
function calculateWidths(doc, table) {
calculate(doc, table);
var resizableColumns = [];
var initialTableWidth = 0;
table.columns.forEach(function (column) {
var customWidth = column.getMaxCustomCellWidth(table);
if (customWidth) {
// final column width
column.width = customWidth;
}
else {
// initial column width (will be resized)
column.width = column.wrappedWidth;
resizableColumns.push(column);
}
initialTableWidth += column.width;
});
// width difference that needs to be distributed
var resizeWidth = table.getWidth(doc.pageSize().width) - initialTableWidth;
// first resize attempt: with respect to minReadableWidth and minWidth
if (resizeWidth) {
resizeWidth = resizeColumns(resizableColumns, resizeWidth, function (column) {
return Math.max(column.minReadableWidth, column.minWidth);
});
}
// second resize attempt: ignore minReadableWidth but respect minWidth
if (resizeWidth) {
resizeWidth = resizeColumns(resizableColumns, resizeWidth, function (column) { return column.minWidth; });
}
resizeWidth = Math.abs(resizeWidth);
if (!table.settings.horizontalPageBreak &&
resizeWidth > 0.1 / doc.scaleFactor()) {
// Table can't get smaller due to custom-width or minWidth restrictions
// We can't really do much here. Up to user to for example
// reduce font size, increase page size or remove custom cell widths
// to allow more columns to be reduced in size
resizeWidth = resizeWidth < 1 ? resizeWidth : Math.round(resizeWidth);
console.log("Of the table content, ".concat(resizeWidth, " units width could not fit page"));
}
applyColSpans(table);
fitContent(table, doc);
applyRowSpans(table);
}
function calculate(doc, table) {
var sf = doc.scaleFactor();
var horizontalPageBreak = table.settings.horizontalPageBreak;
var availablePageWidth = (0, common_1.getPageAvailableWidth)(doc, table);
table.allRows().forEach(function (row) {
for (var _i = 0, _a = table.columns; _i < _a.length; _i++) {
var column = _a[_i];
var cell = row.cells[column.index];
if (!cell)
continue;
var hooks = table.hooks.didParseCell;
table.callCellHooks(doc, hooks, cell, row, column, null);
var padding = cell.padding('horizontal');
cell.contentWidth = (0, common_1.getStringWidth)(cell.text, cell.styles, doc) + padding;
// Using [^\S\u00A0] instead of \s ensures that we split the text on all
// whitespace except non-breaking spaces (\u00A0). We need to preserve
// them in the split process to ensure correct word separation and width
// calculation.
var longestWordWidth = (0, common_1.getStringWidth)(cell.text.join(' ').split(/[^\S\u00A0]+/), cell.styles, doc);
cell.minReadableWidth = longestWordWidth + cell.padding('horizontal');
if (typeof cell.styles.cellWidth === 'number') {
cell.minWidth = cell.styles.cellWidth;
cell.wrappedWidth = cell.styles.cellWidth;
}
else if (cell.styles.cellWidth === 'wrap' ||
horizontalPageBreak === true) {
// cell width should not be more than available page width
if (cell.contentWidth > availablePageWidth) {
cell.minWidth = availablePageWidth;
cell.wrappedWidth = availablePageWidth;
}
else {
cell.minWidth = cell.contentWidth;
cell.wrappedWidth = cell.contentWidth;
}
}
else {
// auto
var defaultMinWidth = 10 / sf;
cell.minWidth = cell.styles.minCellWidth || defaultMinWidth;
cell.wrappedWidth = cell.contentWidth;
if (cell.minWidth > cell.wrappedWidth) {
cell.wrappedWidth = cell.minWidth;
}
}
}
});
table.allRows().forEach(function (row) {
for (var _i = 0, _a = table.columns; _i < _a.length; _i++) {
var column = _a[_i];
var cell = row.cells[column.index];
// For now we ignore the minWidth and wrappedWidth of colspan cells when calculating colspan widths.
// Could probably be improved upon however.
if (cell && cell.colSpan === 1) {
column.wrappedWidth = Math.max(column.wrappedWidth, cell.wrappedWidth);
column.minWidth = Math.max(column.minWidth, cell.minWidth);
column.minReadableWidth = Math.max(column.minReadableWidth, cell.minReadableWidth);
}
else {
// Respect cellWidth set in columnStyles even if there is no cells for this column
// or if the column only have colspan cells. Since the width of colspan cells
// does not affect the width of columns, setting columnStyles cellWidth enables the
// user to at least do it manually.
// Note that this is not perfect for now since for example row and table styles are
// not accounted for
var columnStyles = table.styles.columnStyles[column.dataKey] ||
table.styles.columnStyles[column.index] ||
{};
var cellWidth = columnStyles.cellWidth || columnStyles.minCellWidth;
if (cellWidth && typeof cellWidth === 'number') {
column.minWidth = cellWidth;
column.wrappedWidth = cellWidth;
}
}
if (cell) {
// Make sure all columns get at least min width even though width calculations are not based on them
if (cell.colSpan > 1 && !column.minWidth) {
column.minWidth = cell.minWidth;
}
if (cell.colSpan > 1 && !column.wrappedWidth) {
column.wrappedWidth = cell.minWidth;
}
}
}
});
}
/**
* Distribute resizeWidth on passed resizable columns
*/
function resizeColumns(columns, resizeWidth, getMinWidth) {
var initialResizeWidth = resizeWidth;
var sumWrappedWidth = columns.reduce(function (acc, column) { return acc + column.wrappedWidth; }, 0);
for (var i = 0; i < columns.length; i++) {
var column = columns[i];
var ratio = column.wrappedWidth / sumWrappedWidth;
var suggestedChange = initialResizeWidth * ratio;
var suggestedWidth = column.width + suggestedChange;
var minWidth = getMinWidth(column);
var newWidth = suggestedWidth < minWidth ? minWidth : suggestedWidth;
resizeWidth -= newWidth - column.width;
column.width = newWidth;
}
resizeWidth = Math.round(resizeWidth * 1e10) / 1e10;
// Run the resizer again if there's remaining width needs
// to be distributed and there're columns that can be resized
if (resizeWidth) {
var resizableColumns = columns.filter(function (column) {
return resizeWidth < 0
? column.width > getMinWidth(column) // check if column can shrink
: true; // check if column can grow
});
if (resizableColumns.length) {
resizeWidth = resizeColumns(resizableColumns, resizeWidth, getMinWidth);
}
}
return resizeWidth;
}
function applyRowSpans(table) {
var rowSpanCells = {};
var colRowSpansLeft = 1;
var all = table.allRows();
for (var rowIndex = 0; rowIndex < all.length; rowIndex++) {
var row = all[rowIndex];
for (var _i = 0, _a = table.columns; _i < _a.length; _i++) {
var column = _a[_i];
var data = rowSpanCells[column.index];
if (colRowSpansLeft > 1) {
colRowSpansLeft--;
delete row.cells[column.index];
}
else if (data) {
data.cell.height += row.height;
colRowSpansLeft = data.cell.colSpan;
delete row.cells[column.index];
data.left--;
if (data.left <= 1) {
delete rowSpanCells[column.index];
}
}
else {
var cell = row.cells[column.index];
if (!cell) {
continue;
}
cell.height = row.height;
if (cell.rowSpan > 1) {
var remaining = all.length - rowIndex;
var left = cell.rowSpan > remaining ? remaining : cell.rowSpan;
rowSpanCells[column.index] = { cell: cell, left: left, row: row };
}
}
}
}
}
function applyColSpans(table) {
var all = table.allRows();
for (var rowIndex = 0; rowIndex < all.length; rowIndex++) {
var row = all[rowIndex];
var colSpanCell = null;
var combinedColSpanWidth = 0;
var colSpansLeft = 0;
for (var columnIndex = 0; columnIndex < table.columns.length; columnIndex++) {
var column = table.columns[columnIndex];
// Width and colspan
colSpansLeft -= 1;
if (colSpansLeft > 1 && table.columns[columnIndex + 1]) {
combinedColSpanWidth += column.width;
delete row.cells[column.index];
}
else if (colSpanCell) {
var cell = colSpanCell;
delete row.cells[column.index];
colSpanCell = null;
cell.width = column.width + combinedColSpanWidth;
}
else {
var cell = row.cells[column.index];
if (!cell)
continue;
colSpansLeft = cell.colSpan;
combinedColSpanWidth = 0;
if (cell.colSpan > 1) {
colSpanCell = cell;
combinedColSpanWidth += column.width;
continue;
}
cell.width = column.width + combinedColSpanWidth;
}
}
}
}
function fitContent(table, doc) {
var rowSpanHeight = { count: 0, height: 0 };
for (var _i = 0, _a = table.allRows(); _i < _a.length; _i++) {
var row = _a[_i];
for (var _b = 0, _c = table.columns; _b < _c.length; _b++) {
var column = _c[_b];
var cell = row.cells[column.index];
if (!cell)
continue;
doc.applyStyles(cell.styles, true);
var textSpace = cell.width - cell.padding('horizontal');
if (cell.styles.overflow === 'linebreak') {
// Add one pt to textSpace to fix rounding error
cell.text = doc.splitTextToSize(cell.text, textSpace + 1 / doc.scaleFactor(), { fontSize: cell.styles.fontSize });
}
else if (cell.styles.overflow === 'ellipsize') {
cell.text = ellipsize(cell.text, textSpace, cell.styles, doc, '...');
}
else if (cell.styles.overflow === 'hidden') {
cell.text = ellipsize(cell.text, textSpace, cell.styles, doc, '');
}
else if (typeof cell.styles.overflow === 'function') {
var result = cell.styles.overflow(cell.text, textSpace);
if (typeof result === 'string') {
cell.text = [result];
}
else {
cell.text = result;
}
}
cell.contentHeight = cell.getContentHeight(doc.scaleFactor(), doc.getLineHeightFactor());
var realContentHeight = cell.contentHeight / cell.rowSpan;
if (cell.rowSpan > 1 &&
rowSpanHeight.count * rowSpanHeight.height <
realContentHeight * cell.rowSpan) {
rowSpanHeight = { height: realContentHeight, count: cell.rowSpan };
}
else if (rowSpanHeight && rowSpanHeight.count > 0) {
if (rowSpanHeight.height > realContentHeight) {
realContentHeight = rowSpanHeight.height;
}
}
if (realContentHeight > row.height) {
row.height = realContentHeight;
}
}
rowSpanHeight.count--;
}
}
function ellipsize(text, width, styles, doc, overflow) {
return text.map(function (str) { return ellipsizeStr(str, width, styles, doc, overflow); });
}
function ellipsizeStr(text, width, styles, doc, overflow) {
var precision = 10000 * doc.scaleFactor();
width = Math.ceil(width * precision) / precision;
if (width >= (0, common_1.getStringWidth)(text, styles, doc)) {
return text;
}
while (width < (0, common_1.getStringWidth)(text + overflow, styles, doc)) {
if (text.length <= 1) {
break;
}
text = text.substring(0, text.length - 1);
}
return text.trim() + overflow;
}
/***/ }),
/***/ 176:
/***/ (function(__unused_webpack_module, exports) {
/* eslint-disable @typescript-eslint/no-unused-vars */
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.assign = assign;
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
function assign(target, s, s1, s2, s3) {
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
// eslint-disable-next-line prefer-rest-params
var nextSource = arguments[index];
if (nextSource != null) {
// Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
}
/***/ }),
/***/ 344:
/***/ (function(__unused_webpack_module, exports) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.validateInput = validateInput;
function validateInput(global, document, current) {
for (var _i = 0, _a = [global, document, current]; _i < _a.length; _i++) {
var options = _a[_i];
if (options && typeof options !== 'object') {
console.error('The options parameter should be of type object, is: ' + typeof options);
}
if (options.startY && typeof options.startY !== 'number') {
console.error('Invalid value for startY option', options.startY);
delete options.startY;
}
}
}
/***/ }),
/***/ 371:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.parseInput = parseInput;
var common_1 = __webpack_require__(799);
var documentHandler_1 = __webpack_require__(643);
var htmlParser_1 = __webpack_require__(660);
var inputValidator_1 = __webpack_require__(344);
var polyfills_1 = __webpack_require__(176);
function parseInput(d, current) {
var doc = new documentHandler_1.DocHandler(d);
var document = doc.getDocumentOptions();
var global = doc.getGlobalOptions();
(0, inputValidator_1.validateInput)(global, document, current);
var options = (0, polyfills_1.assign)({}, global, document, current);
var win;
if (typeof window !== 'undefined') {
win = window;
}
var styles = parseStyles(global, document, current);
var hooks = parseHooks(global, document, current);
var settings = parseSettings(doc, options);
var content = parseContent(doc, options, win);
return { id: current.tableId, content: content, hooks: hooks, styles: styles, settings: settings };
}
function parseStyles(gInput, dInput, cInput) {
var styleOptions = {
styles: {},
headStyles: {},
bodyStyles: {},
footStyles: {},
alternateRowStyles: {},
columnStyles: {},
};
var _loop_1 = function (prop) {
if (prop === 'columnStyles') {
var global_1 = gInput[prop];
var document_1 = dInput[prop];
var current = cInput[prop];
styleOptions.columnStyles = (0, polyfills_1.assign)({}, global_1, document_1, current);
}
else {
var allOptions = [gInput, dInput, cInput];
var styles = allOptions.map(function (opts) { return opts[prop] || {}; });
styleOptions[prop] = (0, polyfills_1.assign)({}, styles[0], styles[1], styles[2]);
}
};
for (var _i = 0, _a = Object.keys(styleOptions); _i < _a.length; _i++) {
var prop = _a[_i];
_loop_1(prop);
}
return styleOptions;
}
function parseHooks(global, document, current) {
var allOptions = [global, document, current];
var result = {
didParseCell: [],
willDrawCell: [],
didDrawCell: [],
willDrawPage: [],
didDrawPage: [],
};
for (var _i = 0, allOptions_1 = allOptions; _i < allOptions_1.length; _i++) {
var options = allOptions_1[_i];
if (options.didParseCell)
result.didParseCell.push(options.didParseCell);
if (options.willDrawCell)
result.willDrawCell.push(options.willDrawCell);
if (options.didDrawCell)
result.didDrawCell.push(options.didDrawCell);
if (options.willDrawPage)
result.willDrawPage.push(options.willDrawPage);
if (options.didDrawPage)
result.didDrawPage.push(options.didDrawPage);
}
return result;
}
function parseSettings(doc, options) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
var margin = (0, common_1.parseSpacing)(options.margin, 40 / doc.scaleFactor());
var startY = (_a = getStartY(doc, options.startY)) !== null && _a !== void 0 ? _a : margin.top;
var showFoot;
if (options.showFoot === true) {
showFoot = 'everyPage';
}
else if (options.showFoot === false) {
showFoot = 'never';
}
else {
showFoot = (_b = options.showFoot) !== null && _b !== void 0 ? _b : 'everyPage';
}
var showHead;
if (options.showHead === true) {
showHead = 'everyPage';
}
else if (options.showHead === false) {
showHead = 'never';
}
else {
showHead = (_c = options.showHead) !== null && _c !== void 0 ? _c : 'everyPage';
}
var useCss = (_d = options.useCss) !== null && _d !== void 0 ? _d : false;
var theme = options.theme || (useCss ? 'plain' : 'striped');
var horizontalPageBreak = !!options.horizontalPageBreak;
var horizontalPageBreakRepeat = (_e = options.horizontalPageBreakRepeat) !== null && _e !== void 0 ? _e : null;
return {
includeHiddenHtml: (_f = options.includeHiddenHtml) !== null && _f !== void 0 ? _f : false,
useCss: useCss,
theme: theme,
startY: startY,
margin: margin,
pageBreak: (_g = options.pageBreak) !== null && _g !== void 0 ? _g : 'auto',
rowPageBreak: (_h = options.rowPageBreak) !== null && _h !== void 0 ? _h : 'auto',
tableWidth: (_j = options.tableWidth) !== null && _j !== void 0 ? _j : 'auto',
showHead: showHead,
showFoot: showFoot,
tableLineWidth: (_k = options.tableLineWidth) !== null && _k !== void 0 ? _k : 0,
tableLineColor: (_l = options.tableLineColor) !== null && _l !== void 0 ? _l : 200,
horizontalPageBreak: horizontalPageBreak,
horizontalPageBreakRepeat: horizontalPageBreakRepeat,
horizontalPageBreakBehaviour: (_m = options.horizontalPageBreakBehaviour) !== null && _m !== void 0 ? _m : 'afterAllRows',
};
}
function getStartY(doc, userStartY) {
var previous = doc.getLastAutoTable();
var sf = doc.scaleFactor();
var currentPage = doc.pageNumber();
var isSamePageAsPreviousTable = false;
if (previous && previous.startPageNumber) {
var endingPage = previous.startPageNumber + previous.pageNumber - 1;
isSamePageAsPreviousTable = endingPage === currentPage;
}
if (typeof userStartY === 'number') {
return userStartY;
}
else if (userStartY == null || userStartY === false) {
if (isSamePageAsPreviousTable && (previous === null || previous === void 0 ? void 0 : previous.finalY) != null) {
// Some users had issues with overlapping tables when they used multiple
// tables without setting startY so setting it here to a sensible default.
return previous.finalY + 20 / sf;
}
}
return null;
}
function parseContent(doc, options, window) {
var head = options.head || [];
var body = options.body || [];
var foot = options.foot || [];
if (options.html) {
var hidden = options.includeHiddenHtml;
if (window) {
var htmlContent = (0, htmlParser_1.parseHtml)(doc, options.html, window, hidden, options.useCss) || {};
head = htmlContent.head || head;
body = htmlContent.body || head;
foot = htmlContent.foot || head;
}
else {
console.error('Cannot parse html in non browser environment');
}
}
var columns = options.columns || parseColumns(head, body, foot);
return { columns: columns, head: head, body: body, foot: foot };
}
function parseColumns(head, body, foot) {
var firstRow = head[0] || body[0] || foot[0] || [];
var result = [];
Object.keys(firstRow)
.filter(function (key) { return key !== '_element'; })
.forEach(function (key) {
var colSpan = 1;
var input;
if (Array.isArray(firstRow)) {
input = firstRow[parseInt(key)];
}
else {
input = firstRow[key];
}
if (typeof input === 'object' && !Array.isArray(input)) {
colSpan = (input === null || input === void 0 ? void 0 : input.colSpan) || 1;
}
for (var i = 0; i < colSpan; i++) {
var id = void 0;
if (Array.isArray(firstRow)) {
id = result.length;
}
else {
id = key + (i > 0 ? "_".concat(i) : '');
}
var rowResult = { dataKey: id };
result.push(rowResult);
}
});
return result;
}
/***/ }),
/***/ 376:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.createTable = createTable;
var config_1 = __webpack_require__(28);
var documentHandler_1 = __webpack_require__(643);
var models_1 = __webpack_require__(524);
var polyfills_1 = __webpack_require__(176);
var widthCalculator_1 = __webpack_require__(152);
function createTable(jsPDFDoc, input) {
var doc = new documentHandler_1.DocHandler(jsPDFDoc);
var content = parseContent(input, doc.scaleFactor());
var table = new models_1.Table(input, content);
(0, widthCalculator_1.calculateWidths)(doc, table);
doc.applyStyles(doc.userStyles);
return table;
}
function parseContent(input, sf) {
var content = input.content;
var columns = createColumns(content.columns);
// If no head or foot is set, try generating it with content from columns
if (content.head.length === 0) {
var sectionRow = generateSectionRow(columns, 'head');
if (sectionRow)
content.head.push(sectionRow);
}
if (content.foot.length === 0) {
var sectionRow = generateSectionRow(columns, 'foot');
if (sectionRow)
content.foot.push(sectionRow);
}
var theme = input.settings.theme;
var styles = input.styles;
return {
columns: columns,
head: parseSection('head', content.head, columns, styles, theme, sf),
body: parseSection('body', content.body, columns, styles, theme, sf),
foot: parseSection('foot', content.foot, columns, styles, theme, sf),
};
}
function parseSection(sectionName, sectionRows, columns, styleProps, theme, scaleFactor) {
var rowSpansLeftForColumn = {};
var result = sectionRows.map(function (rawRow, rowIndex) {
var skippedRowForRowSpans = 0;
var cells = {};
var colSpansAdded = 0;
var columnSpansLeft = 0;
for (var _i = 0, columns_1 = columns; _i < columns_1.length; _i++) {
var column = columns_1[_i];
if (rowSpansLeftForColumn[column.index] == null ||
rowSpansLeftForColumn[column.index].left === 0) {
if (columnSpansLeft === 0) {
var rawCell = void 0;
if (Array.isArray(rawRow)) {
rawCell =
rawRow[column.index - colSpansAdded - skippedRowForRowSpans];
}
else {
rawCell = rawRow[column.dataKey];
}
var cellInputStyles = {};
if (typeof rawCell === 'object' && !Array.isArray(rawCell)) {
cellInputStyles = (rawCell === null || rawCell === void 0 ? void 0 : rawCell.styles) || {};
}
var styles = cellStyles(sectionName, column, rowIndex, theme, styleProps, scaleFactor, cellInputStyles);
var cell = new models_1.Cell(rawCell, styles, sectionName);
// dataKey is not used internally no more but keep for
// backwards compat in hooks
cells[column.dataKey] = cell;
cells[column.index] = cell;
columnSpansLeft = cell.colSpan - 1;
rowSpansLeftForColumn[column.index] = {
left: cell.rowSpan - 1,
times: columnSpansLeft,
};
}
else {
columnSpansLeft--;
colSpansAdded++;
}
}
else {
rowSpansLeftForColumn[column.index].left--;
columnSpansLeft = rowSpansLeftForColumn[column.index].times;
skippedRowForRowSpans++;
}
}
return new models_1.Row(rawRow, rowIndex, sectionName, cells);
});
return result;
}
function generateSectionRow(columns, section) {
var sectionRow = {};
columns.forEach(function (col) {
if (col.raw != null) {
var title = getSectionTitle(section, col.raw);
if (title != null)
sectionRow[col.dataKey] = title;
}
});
return Object.keys(sectionRow).length > 0 ? sectionRow : null;
}
function getSectionTitle(section, column) {
if (section === 'head') {
if (typeof column === 'object') {
return column.header || null;
}
else if (typeof column === 'string' || typeof column === 'number') {
return column;
}
}
else if (section === 'foot' && typeof column === 'object') {
return column.footer;
}
return null;
}
function createColumns(columns) {
return columns.map(function (input, index) {
var _a;
var key;
if (typeof input === 'object') {
key = (_a = input.dataKey) !== null && _a !== void 0 ? _a : index;
}
else {
key = index;
}
return new models_1.Column(key, input, index);
});
}
function cellStyles(sectionName, column, rowIndex, themeName, styles, scaleFactor, cellInputStyles) {
var theme = (0, config_1.getTheme)(themeName);
var sectionStyles;
if (sectionName === 'head') {
sectionStyles = styles.headStyles;
}
else if (sectionName === 'body') {
sectionStyles = styles.bodyStyles;
}
else if (sectionName === 'foot') {
sectionStyles = styles.footStyles;
}
var otherStyles = (0, polyfills_1.assign)({}, theme.table, theme[sectionName], styles.styles, sectionStyles);
var columnStyles = styles.columnStyles[column.dataKey] ||
styles.columnStyles[column.index] ||
{};
var colStyles = sectionName === 'body' ? columnStyles : {};
var rowStyles = sectionName === 'body' && rowIndex % 2 === 0
? (0, polyfills_1.assign)({}, theme.alternateRow, styles.alternateRowStyles)
: {};
var defaultStyle = (0, config_1.defaultStyles)(scaleFactor);
var themeStyles = (0, polyfills_1.assign)({}, defaultStyle, otherStyles, rowStyles, colStyles);
return (0, polyfills_1.assign)(themeStyles, cellInputStyles);
}
/***/ }),
/***/ 460:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.parseCss = parseCss;
// Limitations
// - No support for border spacing
// - No support for transparency
var common_1 = __webpack_require__(799);
function parseCss(supportedFonts, element, scaleFactor, style, window) {
var result = {};
var pxScaleFactor = 96 / 72;
var backgroundColor = parseColor(element, function (elem) {
return window.getComputedStyle(elem)['backgroundColor'];
});
if (backgroundColor != null)
result.fillColor = backgroundColor;
var textColor = parseColor(element, function (elem) {
return window.getComputedStyle(elem)['color'];
});
if (textColor != null)
result.textColor = textColor;
var padding = parsePadding(style, scaleFactor);
if (padding)
result.cellPadding = padding;
var borderColorSide = 'borderTopColor';
var finalScaleFactor = pxScaleFactor * scaleFactor;
var btw = style.borderTopWidth;
if (style.borderBottomWidth === btw &&
style.borderRightWidth === btw &&
style.borderLeftWidth === btw) {
var borderWidth = (parseFloat(btw) || 0) / finalScaleFactor;
if (borderWidth)
result.lineWidth = borderWidth;
}
else {
result.lineWidth = {
top: (parseFloat(style.borderTopWidth) || 0) / finalScaleFactor,
right: (parseFloat(style.borderRightWidth) || 0) / finalScaleFactor,
bottom: (parseFloat(style.borderBottomWidth) || 0) / finalScaleFactor,
left: (parseFloat(style.borderLeftWidth) || 0) / finalScaleFactor,
};
// Choose border color of first available side
// could be improved by supporting object as lineColor
if (!result.lineWidth.top) {
if (result.lineWidth.right) {
borderColorSide = 'borderRightColor';
}
else if (result.lineWidth.bottom) {
borderColorSide = 'borderBottomColor';
}
else if (result.lineWidth.left) {
borderColorSide = 'borderLeftColor';
}
}
}
var borderColor = parseColor(element, function (elem) {
return window.getComputedStyle(elem)[borderColorSide];
});
if (borderColor != null)
result.lineColor = borderColor;
var accepted = ['left', 'right', 'center', 'justify'];
if (accepted.indexOf(style.textAlign) !== -1) {
result.halign = style.textAlign;
}
accepted = ['middle', 'bottom', 'top'];
if (accepted.indexOf(style.verticalAlign) !== -1) {
result.valign = style.verticalAlign;
}
var res = parseInt(style.fontSize || '');
if (!isNaN(res))
result.fontSize = res / pxScaleFactor;
var fontStyle = parseFontStyle(style);
if (fontStyle)
result.fontStyle = fontStyle;
var font = (style.fontFamily || '').toLowerCase();
if (supportedFonts.indexOf(font) !== -1) {
result.font = font;
}
return result;
}
function parseFontStyle(style) {
var res = '';
if (style.fontWeight === 'bold' ||
style.fontWeight === 'bolder' ||
parseInt(style.fontWeight) >= 700) {
res = 'bold';
}
if (style.fontStyle === 'italic' || style.fontStyle === 'oblique') {
res += 'italic';
}
return res;
}
function parseColor(element, styleGetter) {
var cssColor = realColor(element, styleGetter);
if (!cssColor)
return null;
var rgba = cssColor.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d*\.?\d*))?\)$/);
if (!rgba || !Array.isArray(rgba)) {
return null;
}
var color = [
parseInt(rgba[1]),
parseInt(rgba[2]),
parseInt(rgba[3]),
];
var alpha = parseInt(rgba[4]);
if (alpha === 0 || isNaN(color[0]) || isNaN(color[1]) || isNaN(color[2])) {
return null;
}
return color;
}
function realColor(elem, styleGetter) {
var bg = styleGetter(elem);
if (bg === 'rgba(0, 0, 0, 0)' ||
bg === 'transparent' ||
bg === 'initial' ||
bg === 'inherit') {
if (elem.parentElement == null) {
return null;
}
return realColor(elem.parentElement, styleGetter);
}
else {
return bg;
}
}
function parsePadding(style, scaleFactor) {
var val = [
style.paddingTop,
style.paddingRight,
style.paddingBottom,
style.paddingLeft,
];
var pxScaleFactor = 96 / (72 / scaleFactor);
var linePadding = (parseInt(style.lineHeight) - parseInt(style.fontSize)) / scaleFactor / 2;
var inputPadding = val.map(function (n) {
return parseInt(n || '0') / pxScaleFactor;
});
var padding = (0, common_1.parseSpacing)(inputPadding, 0);
if (linePadding > padding.top) {
padding.top = linePadding;
}
if (linePadding > padding.bottom) {
padding.bottom = linePadding;
}
return padding;
}
/***/ }),
/***/ 524:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.Column = exports.Cell = exports.Row = exports.Table = void 0;
var common_1 = __webpack_require__(799);
var config_1 = __webpack_require__(28);
var HookData_1 = __webpack_require__(601);
var Table = /** @class */ (function () {
function Table(input, content) {
this.pageNumber = 1;
this.id = input.id;
this.settings = input.settings;
this.styles = input.styles;
this.hooks = input.hooks;
this.columns = content.columns;
this.head = content.head;
this.body = content.body;
this.foot = content.foot;
}
Table.prototype.getHeadHeight = function (columns) {
return this.head.reduce(function (acc, row) { return acc + row.getMaxCellHeight(columns); }, 0);
};
Table.prototype.getFootHeight = function (columns) {
return this.foot.reduce(function (acc, row) { return acc + row.getMaxCellHeight(columns); }, 0);
};
Table.prototype.allRows = function () {
return this.head.concat(this.body).concat(this.foot);
};
Table.prototype.callCellHooks = function (doc, handlers, cell, row, column, cursor) {
for (var _i = 0, handlers_1 = handlers; _i < handlers_1.length; _i++) {
var handler = handlers_1[_i];
var data = new HookData_1.CellHookData(doc, this, cell, row, column, cursor);
var result = handler(data) === false;
// Make sure text is always string[] since user can assign string
cell.text = Array.isArray(cell.text) ? cell.text : [cell.text];
if (result) {
return false;
}
}
return true;
};
Table.prototype.callEndPageHooks = function (doc, cursor) {
doc.applyStyles(doc.userStyles);
for (var _i = 0, _a = this.hooks.didDrawPage; _i < _a.length; _i++) {
var handler = _a[_i];
handler(new HookData_1.HookData(doc, this, cursor));
}
};
Table.prototype.callWillDrawPageHooks = function (doc, cursor) {
for (var _i = 0, _a = this.hooks.willDrawPage; _i < _a.length; _i++) {
var handler = _a[_i];
handler(new HookData_1.HookData(doc, this, cursor));
}
};
Table.prototype.getWidth = function (pageWidth) {
if (typeof this.settings.tableWidth === 'number') {
return this.settings.tableWidth;
}
else if (this.settings.tableWidth === 'wrap') {
var wrappedWidth = this.columns.reduce(function (total, col) { return total + col.wrappedWidth; }, 0);
return wrappedWidth;
}
else {
var margin = this.settings.margin;
return pageWidth - margin.left - margin.right;
}
};
return Table;
}());
exports.Table = Table;
var Row = /** @class */ (function () {
function Row(raw, index, section, cells, spansMultiplePages) {
if (spansMultiplePages === void 0) { spansMultiplePages = false; }
this.height = 0;
this.raw = raw;
if (raw instanceof config_1.HtmlRowInput) {
this.raw = raw._element;
this.element = raw._element;
}
this.index = index;
this.section = section;
this.cells = cells;
this.spansMultiplePages = spansMultiplePages;
}
Row.prototype.getMaxCellHeight = function (columns) {
var _this = this;
return columns.reduce(function (acc, column) { var _a; return Math.max(acc, ((_a = _this.cells[column.index]) === null || _a === void 0 ? void 0 : _a.height) || 0); }, 0);
};
Row.prototype.hasRowSpan = function (columns) {
var _this = this;
return (columns.filter(function (column) {
var cell = _this.cells[column.index];
if (!cell)
return false;
return cell.rowSpan > 1;
}).length > 0);
};
Row.prototype.canEntireRowFit = function (height, columns) {
return this.getMaxCellHeight(columns) <= height;
};
Row.prototype.getMinimumRowHeight = function (columns, doc) {
var _this = this;
return columns.reduce(function (acc, column) {
var cell = _this.cells[column.index];
if (!cell)
return 0;
var lineHeight = doc.getLineHeight(cell.styles.fontSize);
var vPadding = cell.padding('vertical');
var oneRowHeight = vPadding + lineHeight;
return oneRowHeight > acc ? oneRowHeight : acc;
}, 0);
};
return Row;
}());
exports.Row = Row;
var Cell = /** @class */ (function () {
function Cell(raw, styles, section) {
var _a;
this.contentHeight = 0;
this.contentWidth = 0;
this.wrappedWidth = 0;
this.minReadableWidth = 0;
this.minWidth = 0;
this.width = 0;
this.height = 0;
this.x = 0;
this.y = 0;
this.styles = styles;
this.section = section;
this.raw = raw;
var content = raw;
if (raw != null && typeof raw === 'object' && !Array.isArray(raw)) {
this.rowSpan = raw.rowSpan || 1;
this.colSpan = raw.colSpan || 1;
content = (_a = raw.content) !== null && _a !== void 0 ? _a : raw;
if (raw._element) {
this.raw = raw._element;
}
}
else {
this.rowSpan = 1;
this.colSpan = 1;
}
// Stringify 0 and false, but not undefined or null
var text = content != null ? '' + content : '';
var splitRegex = /\r\n|\r|\n/g;
this.text = text.split(splitRegex);
}
Cell.prototype.getTextPos = function () {
var y;
if (this.styles.valign === 'top') {
y = this.y + this.padding('top');
}
else if (this.styles.valign === 'bottom') {
y = this.y + this.height - this.padding('bottom');
}
else {
var netHeight = this.height - this.padding('vertical');
y = this.y + netHeight / 2 + this.padding('top');
}
var x;
if (this.styles.halign === 'right') {
x = this.x + this.width - this.padding('right');
}
else if (this.styles.halign === 'center') {
var netWidth = this.width - this.padding('horizontal');
x = this.x + netWidth / 2 + this.padding('left');
}
else {
x = this.x + this.padding('left');
}
return { x: x, y: y };
};
// TODO (v4): replace parameters with only (lineHeight)
Cell.prototype.getContentHeight = function (scaleFactor, lineHeightFactor) {
if (lineHeightFactor === void 0) { lineHeightFactor = 1.15; }
var lineCount = Array.isArray(this.text) ? this.text.length : 1;
var lineHeight = (this.styles.fontSize / scaleFactor) * lineHeightFactor;
var height = lineCount * lineHeight + this.padding('vertical');
return Math.max(height, this.styles.minCellHeight);
};
Cell.prototype.padding = function (name) {
var padding = (0, common_1.parseSpacing)(this.styles.cellPadding, 0);
if (name === 'vertical') {
return padding.top + padding.bottom;
}
else if (name === 'horizontal') {
return padding.left + padding.right;
}
else {
return padding[name];
}
};
return Cell;
}());
exports.Cell = Cell;
var Column = /** @class */ (function () {
function Column(dataKey, raw, index) {
this.wrappedWidth = 0;
this.minReadableWidth = 0;
this.minWidth = 0;
this.width = 0;
this.dataKey = dataKey;
this.raw = raw;
this.index = index;
}
Column.prototype.getMaxCustomCellWidth = function (table) {
var max = 0;
for (var _i = 0, _a = table.allRows(); _i < _a.length; _i++) {
var row = _a[_i];
var cell = row.cells[this.index];
if (cell && typeof cell.styles.cellWidth === 'number') {
max = Math.max(max, cell.styles.cellWidth);
}
}
return max;
};
return Column;
}());
exports.Column = Column;
/***/ }),
/***/ 601:
/***/ (function(__unused_webpack_module, exports) {
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.CellHookData = exports.HookData = void 0;
var HookData = /** @class */ (function () {
function HookData(doc, table, cursor) {
this.table = table;
this.pageN