UNPKG

@siamf/react-export

Version:

A React package that simplifies data exporting and clipboard management. It provides react component for printing documents, exporting data as PDF, Excel, and CSV, as well as copying text and structured data to the clipboard.

68 lines 3.14 kB
"use strict"; "use client"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const jspdf_1 = __importDefault(require("jspdf")); const jspdf_autotable_1 = __importDefault(require("jspdf-autotable")); const ExportAsPdf = (props) => { const { children, data, headers, title, theme = "grid", styles, headerStyles, footerStyles, foot, margin, fileName = "reactExport", orientation, onError, onSuccess } = props; if (orientation) { if (orientation !== "l" && orientation !== "portrait" && orientation !== "landscape" && orientation !== "p") throw new Error("Orientation can be only 'landscape', 'portrait', 'l' or 'p'."); } const onPdfHandler = () => { try { const doc = new jspdf_1.default({ orientation: orientation }); const body = data.map((item) => { const keys = Object.keys(item); const values = keys.map((key) => item[key]); return values; }); if (title) { const titleWidth = doc.getTextDimensions(title).w; const pageWidth = doc.internal.pageSize.getWidth(); const x = (pageWidth - titleWidth) / 2; doc.text(title, x, 20); } if (foot) { (0, jspdf_autotable_1.default)(doc, { theme: theme, head: [headers], headStyles: headerStyles, margin: Object.assign(Object.assign({}, margin), { top: title ? ((margin === null || margin === void 0 ? void 0 : margin.top) || 30) : margin === null || margin === void 0 ? void 0 : margin.top }), styles: styles, footStyles: footerStyles, body: body, foot: [foot] }); } else { (0, jspdf_autotable_1.default)(doc, { theme: theme, head: [headers], headStyles: headerStyles, margin: Object.assign(Object.assign({}, margin), { top: title ? ((margin === null || margin === void 0 ? void 0 : margin.top) || 30) : margin === null || margin === void 0 ? void 0 : margin.top }), styles: styles, footStyles: footerStyles, body: body }); } doc.save(`${fileName.toLowerCase().replace(/ /g, "-")}.pdf`); if (onSuccess) onSuccess(); } catch (error) { if (onError) onError(error); } }; return (0, jsx_runtime_1.jsx)(react_1.default.Fragment, { children: children === null || children === void 0 ? void 0 : children({ onClick: onPdfHandler }) }); }; exports.default = ExportAsPdf; //# sourceMappingURL=ExportAsPdf.js.map