UNPKG

@e-group/utils

Version:

eGroup team utils that share across projects.

155 lines (122 loc) 4.73 kB
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import html2canvas from 'html2canvas'; const getImageHeight = (pageWidth, contentWidth, contentHeight) => pageWidth / contentWidth * contentHeight; const addImage = /*#__PURE__*/function () { var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(pdf, pageWidth, pageHeight, el, options = {}) { var _options$xPadding, xPadding, _options$yPadding, yPadding, canvas, imgWidth, imgHeight, image, heightLeft, position; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _options$xPadding = options.xPadding, xPadding = _options$xPadding === void 0 ? 0 : _options$xPadding, _options$yPadding = options.yPadding, yPadding = _options$yPadding === void 0 ? 0 : _options$yPadding; _context.next = 3; return html2canvas(el); case 3: canvas = _context.sent; imgWidth = pageWidth - 2 * xPadding; imgHeight = getImageHeight(imgWidth, canvas.width, canvas.height); image = canvas.toDataURL('image/jpeg', 1.0); // add first image pdf.addImage(image, 'JPEG', xPadding, yPadding, imgWidth, imgHeight); // calculate if this image is more height than pageHeight heightLeft = imgHeight; position = yPadding; heightLeft -= pageHeight; while (heightLeft >= 0) { position += heightLeft - imgHeight; // top padding for other pages pdf.addPage(); pdf.addImage(image, 'JPEG', xPadding, position, imgWidth, imgHeight); heightLeft -= pageHeight; } // remove node el.remove(); case 13: case "end": return _context.stop(); } }, _callee); })); return function addImage(_x, _x2, _x3, _x4) { return _ref.apply(this, arguments); }; }(); const wrapGroup = (containerId, group) => { const wrapper = document.createElement('div'); group.forEach(el => { wrapper.appendChild(el.cloneNode(true)); }); const container = document.getElementById(containerId); if (container) { container.append(wrapper); } return wrapper; }; const pdfAddPages = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(containerId, pdf, items, options = {}) { var pageWidth, pageHeight, pageGroups, group, leftHeight, i, el, imgHeight; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: pageWidth = pdf.internal.pageSize.getWidth(); pageHeight = pdf.internal.pageSize.getHeight(); // Create page group by items and total items height should fit in page height. pageGroups = []; group = []; leftHeight = pageHeight; i = 0; case 6: if (!(i < items.length)) { _context2.next = 16; break; } el = items[i]; imgHeight = getImageHeight(pageWidth, el.offsetWidth, el.offsetHeight); if (!(imgHeight > pageHeight)) { _context2.next = 12; break; } pageGroups.push(wrapGroup(containerId, [el])); return _context2.abrupt("break", 16); case 12: if (leftHeight > imgHeight) { group.push(el); leftHeight -= imgHeight; } else { i -= 1; pageGroups.push(wrapGroup(containerId, group)); group = []; leftHeight = pageHeight; } case 13: i += 1; _context2.next = 6; break; case 16: // add last group if (group.length > 0) { pageGroups.push(wrapGroup(containerId, group)); } i = 0; case 18: if (!(i < pageGroups.length)) { _context2.next = 26; break; } group = pageGroups[i]; // eslint-disable-next-line no-await-in-loop _context2.next = 22; return addImage(pdf, pageWidth, pageHeight, group, options); case 22: if (i + 1 < pageGroups.length) { pdf.addPage(); } case 23: i += 1; _context2.next = 18; break; case 26: case "end": return _context2.stop(); } }, _callee2); })); return function pdfAddPages(_x5, _x6, _x7) { return _ref2.apply(this, arguments); }; }(); export default pdfAddPages;