@spexzee/react-pdfhook
Version:
React hook for generating PDF documents from components
77 lines (76 loc) • 4.08 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 });
exports.addElementToPdf = exports.addCustomImagetoPDF = exports.convertImagetoDataURL = void 0;
const html2canvas_1 = __importDefault(require("html2canvas"));
const convertImagetoDataURL = (src) => __awaiter(void 0, void 0, void 0, function* () {
const response = yield fetch(src);
const blob = yield response.blob();
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(blob);
});
});
exports.convertImagetoDataURL = convertImagetoDataURL;
const addCustomImagetoPDF = (pdf, src, yPos, margin, options) => __awaiter(void 0, void 0, void 0, function* () {
return new Promise((resolve) => {
const image = new Image();
image.src = src;
image.onload = () => {
var _a;
const imgWidth = image.naturalWidth;
const imgHeight = image.naturalHeight;
const pdfWidth = (options === null || options === void 0 ? void 0 : options.width) || pdf.internal.pageSize.getWidth() - margin * 2.8;
const pdfHeight = (options === null || options === void 0 ? void 0 : options.height) || (imgHeight * pdfWidth) / imgWidth;
const x = (options === null || options === void 0 ? void 0 : options.x) || margin;
const y = (options === null || options === void 0 ? void 0 : options.y) || yPos;
const format = (options === null || options === void 0 ? void 0 : options.format) || 'PNG';
const maintainAspectRatio = (_a = options === null || options === void 0 ? void 0 : options.maintainAspectRatio) !== null && _a !== void 0 ? _a : true;
if (maintainAspectRatio) {
pdf.addImage(src, format, x, y, pdfWidth, pdfHeight);
}
else {
pdf.addImage(src, format, x, y, pdfWidth, pdfHeight, undefined, 'FAST');
}
resolve(yPos + pdfHeight + 10);
};
});
});
exports.addCustomImagetoPDF = addCustomImagetoPDF;
const addElementToPdf = (pdf, element, yPos, margin, scale, fixedWidth, imageQuality) => __awaiter(void 0, void 0, void 0, function* () {
const pdfWidth = pdf.internal.pageSize.getWidth() - margin * 2;
const pdfHeight = pdf.internal.pageSize.getHeight() - margin * 2;
const canvas = yield (0, html2canvas_1.default)(element, {
scale,
useCORS: true,
logging: false,
windowWidth: fixedWidth || 1500,
width: fixedWidth || 1500,
onclone: (doc) => {
doc.querySelectorAll('.pdf-only').forEach((el) => (el.style.display = 'block'));
doc.querySelectorAll('.screen-only').forEach((el) => (el.style.display = 'none'));
}
});
const imageData = canvas.toDataURL('image/png', imageQuality);
const elementHeight = (canvas.height * pdfWidth) / canvas.width;
if (yPos + elementHeight > pdfHeight + margin) {
pdf.addPage();
yPos = margin;
}
pdf.addImage(imageData, 'PNG', margin, yPos, pdfWidth, elementHeight);
return yPos + elementHeight + 4;
});
exports.addElementToPdf = addElementToPdf;