@spexzee/react-pdfhook
Version:
React hook for generating PDF documents from components
110 lines (109 loc) • 5.23 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const jspdf_1 = __importDefault(require("jspdf"));
const PDFUtils_1 = require("./utils/PDFUtils");
const usePdfGenerator = (options) => {
const { fileName = 'document.pdf', format = 'a4', orientation = 'p', margin = 5, scale = 2, pageBreak = true, debug = false, fixedWidth, imageQuality, compressPdf = true } = options;
const pdfRef = (0, react_1.useRef)(null);
const [pdfLoading, setPdfLoading] = (0, react_1.useState)(false);
const generatePdf = (elements) => __awaiter(void 0, void 0, void 0, function* () {
try {
setPdfLoading(true);
if (!pdfRef.current)
return;
const pdf = new jspdf_1.default(orientation, 'mm', format, compressPdf);
const marginValue = typeof margin === 'number' ? margin : margin.top || 0;
let yPos = marginValue;
const elementsToProcess = [];
if (elements === null || elements === void 0 ? void 0 : elements.length) {
for (const item of elements) {
if (item.type === 'image') {
elementsToProcess.push(item.selector);
}
else if (item.mapping) {
const matches = Array.from(document.querySelectorAll(item.selector));
elementsToProcess.push(...matches);
}
else {
const element = document.querySelector(item.selector);
if (element)
elementsToProcess.push(element);
}
}
}
else {
elementsToProcess.push(...Array.from(pdfRef.current.children));
}
for (const element of elementsToProcess) {
if (typeof element === 'string') {
const imageDataUrl = yield (0, PDFUtils_1.convertImagetoDataURL)(element);
yPos = yield (0, PDFUtils_1.addCustomImagetoPDF)(pdf, imageDataUrl, yPos, marginValue);
}
else {
yPos = yield (0, PDFUtils_1.addElementToPdf)(pdf, element, yPos, marginValue, scale, fixedWidth, imageQuality);
}
if (pageBreak && yPos > pdf.internal.pageSize.getHeight() - marginValue) {
pdf.addPage();
yPos = marginValue;
}
}
const finalFileName = fileName.endsWith('.pdf') ? fileName : `${fileName}.pdf`;
pdf.save(finalFileName);
}
catch (error) {
if (debug)
console.error('Error generating PDF:', error);
throw error;
}
finally {
setPdfLoading(false);
}
});
const exportToPdf = () => __awaiter(void 0, void 0, void 0, function* () {
try {
setPdfLoading(true);
if (!pdfRef.current)
return;
const pdf = new jspdf_1.default(orientation, 'mm', format, compressPdf);
const marginValue = typeof margin === 'number' ? margin : margin.top || 5;
let yPos = marginValue;
const elementsToProcess = Array.from(pdfRef.current.children);
for (const element of elementsToProcess) {
const subChildren = Array.from(element.children);
if (subChildren.length === 0) {
yPos = yield (0, PDFUtils_1.addElementToPdf)(pdf, element, yPos, marginValue, scale, fixedWidth, imageQuality);
}
else {
for (const subChild of subChildren) {
yPos = yield (0, PDFUtils_1.addElementToPdf)(pdf, subChild, yPos, marginValue, scale, fixedWidth, imageQuality);
}
}
}
const finalFileName = fileName.endsWith('.pdf') ? fileName : `${fileName}.pdf`;
pdf.save(finalFileName);
}
catch (error) {
if (debug)
console.error('Error generating PDF', error);
throw error;
}
finally {
setPdfLoading(false);
}
});
return { generatePdf, pdfRef, pdfLoading, exportToPdf };
};
exports.default = usePdfGenerator;