1
0

html2pptx.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /**
  2. * html2pptx - Convert HTML slide to pptxgenjs slide with positioned elements
  3. *
  4. * USAGE:
  5. * const pptx = new pptxgen();
  6. * pptx.layout = 'LAYOUT_16x9'; // Must match HTML body dimensions
  7. *
  8. * const { slide, placeholders } = await html2pptx('slide.html', pptx);
  9. * slide.addChart(pptx.charts.LINE, data, placeholders[0]);
  10. *
  11. * await pptx.writeFile('output.pptx');
  12. *
  13. * FEATURES:
  14. * - Converts HTML to PowerPoint with accurate positioning
  15. * - Supports text, images, shapes, and bullet lists
  16. * - Extracts placeholder elements (class="placeholder") with positions
  17. * - Handles CSS gradients, borders, and margins
  18. *
  19. * VALIDATION:
  20. * - Uses body width/height from HTML for viewport sizing
  21. * - Throws error if HTML dimensions don't match presentation layout
  22. * - Throws error if content overflows body (with overflow details)
  23. *
  24. * RETURNS:
  25. * { slide, placeholders } where placeholders is an array of { id, x, y, w, h }
  26. */
  27. const { chromium } = require('playwright');
  28. const path = require('path');
  29. const sharp = require('sharp');
  30. const PT_PER_PX = 0.75;
  31. const PX_PER_IN = 96;
  32. const EMU_PER_IN = 914400;
  33. // Helper: Get body dimensions and check for overflow
  34. async function getBodyDimensions(page) {
  35. const bodyDimensions = await page.evaluate(() => {
  36. const body = document.body;
  37. const style = window.getComputedStyle(body);
  38. return {
  39. width: parseFloat(style.width),
  40. height: parseFloat(style.height),
  41. scrollWidth: body.scrollWidth,
  42. scrollHeight: body.scrollHeight
  43. };
  44. });
  45. const errors = [];
  46. const widthOverflowPx = Math.max(0, bodyDimensions.scrollWidth - bodyDimensions.width - 1);
  47. const heightOverflowPx = Math.max(0, bodyDimensions.scrollHeight - bodyDimensions.height - 1);
  48. const widthOverflowPt = widthOverflowPx * PT_PER_PX;
  49. const heightOverflowPt = heightOverflowPx * PT_PER_PX;
  50. if (widthOverflowPt > 0 || heightOverflowPt > 0) {
  51. const directions = [];
  52. if (widthOverflowPt > 0) directions.push(`${widthOverflowPt.toFixed(1)}pt horizontally`);
  53. if (heightOverflowPt > 0) directions.push(`${heightOverflowPt.toFixed(1)}pt vertically`);
  54. const reminder = heightOverflowPt > 0 ? ' (Remember: leave 0.5" margin at bottom of slide)' : '';
  55. errors.push(`HTML content overflows body by ${directions.join(' and ')}${reminder}`);
  56. }
  57. return { ...bodyDimensions, errors };
  58. }
  59. // Helper: Validate dimensions match presentation layout
  60. function validateDimensions(bodyDimensions, pres) {
  61. const errors = [];
  62. const widthInches = bodyDimensions.width / PX_PER_IN;
  63. const heightInches = bodyDimensions.height / PX_PER_IN;
  64. if (pres.presLayout) {
  65. const layoutWidth = pres.presLayout.width / EMU_PER_IN;
  66. const layoutHeight = pres.presLayout.height / EMU_PER_IN;
  67. if (Math.abs(layoutWidth - widthInches) > 0.1 || Math.abs(layoutHeight - heightInches) > 0.1) {
  68. errors.push(
  69. `HTML dimensions (${widthInches.toFixed(1)}" × ${heightInches.toFixed(1)}") ` +
  70. `don't match presentation layout (${layoutWidth.toFixed(1)}" × ${layoutHeight.toFixed(1)}")`
  71. );
  72. }
  73. }
  74. return errors;
  75. }
  76. function validateTextBoxPosition(slideData, bodyDimensions) {
  77. const errors = [];
  78. const slideHeightInches = bodyDimensions.height / PX_PER_IN;
  79. const minBottomMargin = 0.5; // 0.5 inches from bottom
  80. for (const el of slideData.elements) {
  81. // Check text elements (p, h1-h6, list, merged-text)
  82. if (['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'list', 'merged-text'].includes(el.type)) {
  83. const fontSize = el.style?.fontSize || 0;
  84. const bottomEdge = el.position.y + el.position.h;
  85. const distanceFromBottom = slideHeightInches - bottomEdge;
  86. if (fontSize > 12 && distanceFromBottom < minBottomMargin) {
  87. const getText = () => {
  88. if (typeof el.text === 'string') return el.text;
  89. if (Array.isArray(el.text)) return el.text.find(t => t.text)?.text || '';
  90. if (Array.isArray(el.items)) return el.items.find(item => item.text)?.text || '';
  91. return '';
  92. };
  93. const textPrefix = getText().substring(0, 50) + (getText().length > 50 ? '...' : '');
  94. errors.push(
  95. `Text box "${textPrefix}" ends too close to bottom edge ` +
  96. `(${distanceFromBottom.toFixed(2)}" from bottom, minimum ${minBottomMargin}" required)`
  97. );
  98. }
  99. }
  100. }
  101. return errors;
  102. }
  103. // Helper: Add background to slide
  104. async function addBackground(slideData, targetSlide, tmpDir) {
  105. if (slideData.background.type === 'image' && slideData.background.path) {
  106. let imagePath = slideData.background.path.startsWith('file://')
  107. ? slideData.background.path.replace('file://', '')
  108. : slideData.background.path;
  109. targetSlide.background = { path: imagePath };
  110. } else if (slideData.background.type === 'color' && slideData.background.value) {
  111. targetSlide.background = { color: slideData.background.value };
  112. }
  113. }
  114. // Helper: Add elements to slide
  115. function addElements(slideData, targetSlide, pres) {
  116. for (const el of slideData.elements) {
  117. if (el.type === 'image') {
  118. let imagePath = el.src.startsWith('file://') ? el.src.replace('file://', '') : el.src;
  119. targetSlide.addImage({
  120. path: imagePath,
  121. x: el.position.x,
  122. y: el.position.y,
  123. w: el.position.w,
  124. h: el.position.h
  125. });
  126. } else if (el.type === 'line') {
  127. targetSlide.addShape(pres.ShapeType.line, {
  128. x: el.x1,
  129. y: el.y1,
  130. w: el.x2 - el.x1,
  131. h: el.y2 - el.y1,
  132. line: { color: el.color, width: el.width }
  133. });
  134. } else if (el.type === 'shape') {
  135. const shapeOptions = {
  136. x: el.position.x,
  137. y: el.position.y,
  138. w: el.position.w,
  139. h: el.position.h,
  140. shape: el.shape.rectRadius > 0 ? pres.ShapeType.roundRect : pres.ShapeType.rect
  141. };
  142. if (el.shape.fill) {
  143. shapeOptions.fill = { color: el.shape.fill };
  144. if (el.shape.transparency != null) shapeOptions.fill.transparency = el.shape.transparency;
  145. }
  146. if (el.shape.line) shapeOptions.line = el.shape.line;
  147. if (el.shape.rectRadius > 0) shapeOptions.rectRadius = el.shape.rectRadius;
  148. if (el.shape.shadow) shapeOptions.shadow = el.shape.shadow;
  149. targetSlide.addText(el.text || '', shapeOptions);
  150. } else if (el.type === 'list') {
  151. const listOptions = {
  152. x: el.position.x,
  153. y: el.position.y,
  154. w: el.position.w,
  155. h: el.position.h,
  156. fontSize: el.style.fontSize,
  157. fontFace: el.style.fontFace,
  158. color: el.style.color,
  159. align: el.style.align,
  160. valign: 'top',
  161. lineSpacing: el.style.lineSpacing,
  162. paraSpaceBefore: el.style.paraSpaceBefore,
  163. paraSpaceAfter: el.style.paraSpaceAfter,
  164. margin: el.style.margin
  165. };
  166. if (el.style.margin) listOptions.margin = el.style.margin;
  167. targetSlide.addText(el.items, listOptions);
  168. } else if (el.type === 'merged-text') {
  169. // data-pptx-merge container — all paragraphs in one editable text frame.
  170. const mergedOptions = {
  171. x: el.position.x,
  172. y: el.position.y,
  173. w: el.position.w,
  174. h: el.position.h,
  175. fontSize: el.style.fontSize,
  176. fontFace: el.style.fontFace,
  177. color: el.style.color,
  178. align: el.style.align,
  179. valign: 'top',
  180. lineSpacing: el.style.lineSpacing,
  181. paraSpaceBefore: el.style.paraSpaceBefore,
  182. paraSpaceAfter: el.style.paraSpaceAfter,
  183. margin: el.style.margin,
  184. inset: 0
  185. };
  186. if (el.style.transparency != null) mergedOptions.transparency = el.style.transparency;
  187. targetSlide.addText(el.items, mergedOptions);
  188. } else {
  189. // Check if text is single-line (height suggests one line)
  190. const lineHeight = el.style.lineSpacing || el.style.fontSize * 1.2;
  191. const isSingleLine = el.position.h <= lineHeight * 1.5;
  192. let adjustedX = el.position.x;
  193. let adjustedW = el.position.w;
  194. // Make single-line text 2% wider to account for underestimate
  195. if (isSingleLine) {
  196. const widthIncrease = el.position.w * 0.02;
  197. const align = el.style.align;
  198. if (align === 'center') {
  199. // Center: expand both sides
  200. adjustedX = el.position.x - (widthIncrease / 2);
  201. adjustedW = el.position.w + widthIncrease;
  202. } else if (align === 'right') {
  203. // Right: expand to the left
  204. adjustedX = el.position.x - widthIncrease;
  205. adjustedW = el.position.w + widthIncrease;
  206. } else {
  207. // Left (default): expand to the right
  208. adjustedW = el.position.w + widthIncrease;
  209. }
  210. }
  211. const textOptions = {
  212. x: adjustedX,
  213. y: el.position.y,
  214. w: adjustedW,
  215. h: el.position.h,
  216. fontSize: el.style.fontSize,
  217. fontFace: el.style.fontFace,
  218. color: el.style.color,
  219. bold: el.style.bold,
  220. italic: el.style.italic,
  221. underline: el.style.underline,
  222. valign: 'top',
  223. lineSpacing: el.style.lineSpacing,
  224. paraSpaceBefore: el.style.paraSpaceBefore,
  225. paraSpaceAfter: el.style.paraSpaceAfter,
  226. inset: 0 // Remove default PowerPoint internal padding
  227. };
  228. if (el.style.align) textOptions.align = el.style.align;
  229. if (el.style.margin) textOptions.margin = el.style.margin;
  230. if (el.style.rotate !== undefined) textOptions.rotate = el.style.rotate;
  231. if (el.style.transparency !== null && el.style.transparency !== undefined) textOptions.transparency = el.style.transparency;
  232. targetSlide.addText(el.text, textOptions);
  233. }
  234. }
  235. }
  236. // Helper: Extract slide data from HTML page
  237. async function extractSlideData(page) {
  238. return await page.evaluate(() => {
  239. const PT_PER_PX = 0.75;
  240. const PX_PER_IN = 96;
  241. // Fonts that are single-weight and should not have bold applied
  242. // (applying bold causes PowerPoint to use faux bold which makes text wider)
  243. const SINGLE_WEIGHT_FONTS = ['impact'];
  244. // Helper: Check if a font should skip bold formatting
  245. const shouldSkipBold = (fontFamily) => {
  246. if (!fontFamily) return false;
  247. const normalizedFont = fontFamily.toLowerCase().replace(/['"]/g, '').split(',')[0].trim();
  248. return SINGLE_WEIGHT_FONTS.includes(normalizedFont);
  249. };
  250. // Unit conversion helpers
  251. const pxToInch = (px) => px / PX_PER_IN;
  252. const pxToPoints = (pxStr) => parseFloat(pxStr) * PT_PER_PX;
  253. const rgbToHex = (rgbStr) => {
  254. // Handle transparent backgrounds by defaulting to white
  255. if (rgbStr === 'rgba(0, 0, 0, 0)' || rgbStr === 'transparent') return 'FFFFFF';
  256. const match = rgbStr.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);
  257. if (!match) return 'FFFFFF';
  258. return match.slice(1).map(n => parseInt(n).toString(16).padStart(2, '0')).join('');
  259. };
  260. const extractAlpha = (rgbStr) => {
  261. const match = rgbStr.match(/rgba\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)/);
  262. if (!match || !match[4]) return null;
  263. const alpha = parseFloat(match[4]);
  264. return Math.round((1 - alpha) * 100);
  265. };
  266. const applyTextTransform = (text, textTransform) => {
  267. if (textTransform === 'uppercase') return text.toUpperCase();
  268. if (textTransform === 'lowercase') return text.toLowerCase();
  269. if (textTransform === 'capitalize') {
  270. return text.replace(/\b\w/g, c => c.toUpperCase());
  271. }
  272. return text;
  273. };
  274. // Extract rotation angle from CSS transform and writing-mode
  275. const getRotation = (transform, writingMode) => {
  276. let angle = 0;
  277. // Handle writing-mode first
  278. // PowerPoint: 90° = text rotated 90° clockwise (reads top to bottom, letters upright)
  279. // PowerPoint: 270° = text rotated 270° clockwise (reads bottom to top, letters upright)
  280. if (writingMode === 'vertical-rl') {
  281. // vertical-rl alone = text reads top to bottom = 90° in PowerPoint
  282. angle = 90;
  283. } else if (writingMode === 'vertical-lr') {
  284. // vertical-lr alone = text reads bottom to top = 270° in PowerPoint
  285. angle = 270;
  286. }
  287. // Then add any transform rotation
  288. if (transform && transform !== 'none') {
  289. // Try to match rotate() function
  290. const rotateMatch = transform.match(/rotate\((-?\d+(?:\.\d+)?)deg\)/);
  291. if (rotateMatch) {
  292. angle += parseFloat(rotateMatch[1]);
  293. } else {
  294. // Browser may compute as matrix - extract rotation from matrix
  295. const matrixMatch = transform.match(/matrix\(([^)]+)\)/);
  296. if (matrixMatch) {
  297. const values = matrixMatch[1].split(',').map(parseFloat);
  298. // matrix(a, b, c, d, e, f) where rotation = atan2(b, a)
  299. const matrixAngle = Math.atan2(values[1], values[0]) * (180 / Math.PI);
  300. angle += Math.round(matrixAngle);
  301. }
  302. }
  303. }
  304. // Normalize to 0-359 range
  305. angle = angle % 360;
  306. if (angle < 0) angle += 360;
  307. return angle === 0 ? null : angle;
  308. };
  309. // Get position/dimensions accounting for rotation
  310. const getPositionAndSize = (el, rect, rotation) => {
  311. if (rotation === null) {
  312. return { x: rect.left, y: rect.top, w: rect.width, h: rect.height };
  313. }
  314. // For 90° or 270° rotations, swap width and height
  315. // because PowerPoint applies rotation to the original (unrotated) box
  316. const isVertical = rotation === 90 || rotation === 270;
  317. if (isVertical) {
  318. // The browser shows us the rotated dimensions (tall box for vertical text)
  319. // But PowerPoint needs the pre-rotation dimensions (wide box that will be rotated)
  320. // So we swap: browser's height becomes PPT's width, browser's width becomes PPT's height
  321. const centerX = rect.left + rect.width / 2;
  322. const centerY = rect.top + rect.height / 2;
  323. return {
  324. x: centerX - rect.height / 2,
  325. y: centerY - rect.width / 2,
  326. w: rect.height,
  327. h: rect.width
  328. };
  329. }
  330. // For other rotations, use element's offset dimensions
  331. const centerX = rect.left + rect.width / 2;
  332. const centerY = rect.top + rect.height / 2;
  333. return {
  334. x: centerX - el.offsetWidth / 2,
  335. y: centerY - el.offsetHeight / 2,
  336. w: el.offsetWidth,
  337. h: el.offsetHeight
  338. };
  339. };
  340. // Parse CSS box-shadow into PptxGenJS shadow properties
  341. const parseBoxShadow = (boxShadow) => {
  342. if (!boxShadow || boxShadow === 'none') return null;
  343. // Browser computed style format: "rgba(0, 0, 0, 0.3) 2px 2px 8px 0px [inset]"
  344. // CSS format: "[inset] 2px 2px 8px 0px rgba(0, 0, 0, 0.3)"
  345. const insetMatch = boxShadow.match(/inset/);
  346. // IMPORTANT: PptxGenJS/PowerPoint doesn't properly support inset shadows
  347. // Only process outer shadows to avoid file corruption
  348. if (insetMatch) return null;
  349. // Extract color first (rgba or rgb at start)
  350. const colorMatch = boxShadow.match(/rgba?\([^)]+\)/);
  351. // Extract numeric values (handles both px and pt units)
  352. const parts = boxShadow.match(/([-\d.]+)(px|pt)/g);
  353. if (!parts || parts.length < 2) return null;
  354. const offsetX = parseFloat(parts[0]);
  355. const offsetY = parseFloat(parts[1]);
  356. const blur = parts.length > 2 ? parseFloat(parts[2]) : 0;
  357. // Calculate angle from offsets (in degrees, 0 = right, 90 = down)
  358. let angle = 0;
  359. if (offsetX !== 0 || offsetY !== 0) {
  360. angle = Math.atan2(offsetY, offsetX) * (180 / Math.PI);
  361. if (angle < 0) angle += 360;
  362. }
  363. // Calculate offset distance (hypotenuse)
  364. const offset = Math.sqrt(offsetX * offsetX + offsetY * offsetY) * PT_PER_PX;
  365. // Extract opacity from rgba
  366. let opacity = 0.5;
  367. if (colorMatch) {
  368. const opacityMatch = colorMatch[0].match(/[\d.]+\)$/);
  369. if (opacityMatch) {
  370. opacity = parseFloat(opacityMatch[0].replace(')', ''));
  371. }
  372. }
  373. return {
  374. type: 'outer',
  375. angle: Math.round(angle),
  376. blur: blur * 0.75, // Convert to points
  377. color: colorMatch ? rgbToHex(colorMatch[0]) : '000000',
  378. offset: offset,
  379. opacity
  380. };
  381. };
  382. // Parse inline formatting tags (<b>, <i>, <u>, <strong>, <em>, <span>) into text runs
  383. const parseInlineFormatting = (element, baseOptions = {}, runs = [], baseTextTransform = (x) => x) => {
  384. let prevNodeIsText = false;
  385. element.childNodes.forEach((node) => {
  386. let textTransform = baseTextTransform;
  387. const isText = node.nodeType === Node.TEXT_NODE || node.tagName === 'BR';
  388. if (isText) {
  389. const text = node.tagName === 'BR' ? '\n' : textTransform(node.textContent.replace(/\s+/g, ' '));
  390. const prevRun = runs[runs.length - 1];
  391. if (prevNodeIsText && prevRun) {
  392. prevRun.text += text;
  393. } else {
  394. runs.push({ text, options: { ...baseOptions } });
  395. }
  396. } else if (node.nodeType === Node.ELEMENT_NODE && node.textContent.trim()) {
  397. const options = { ...baseOptions };
  398. const computed = window.getComputedStyle(node);
  399. // Handle inline elements with computed styles
  400. if (node.tagName === 'SPAN' || node.tagName === 'B' || node.tagName === 'STRONG' || node.tagName === 'I' || node.tagName === 'EM' || node.tagName === 'U') {
  401. const isBold = computed.fontWeight === 'bold' || parseInt(computed.fontWeight) >= 600;
  402. if (isBold && !shouldSkipBold(computed.fontFamily)) options.bold = true;
  403. if (computed.fontStyle === 'italic') options.italic = true;
  404. if (computed.textDecoration && computed.textDecoration.includes('underline')) options.underline = true;
  405. if (computed.color && computed.color !== 'rgb(0, 0, 0)') {
  406. options.color = rgbToHex(computed.color);
  407. const transparency = extractAlpha(computed.color);
  408. if (transparency !== null) options.transparency = transparency;
  409. }
  410. if (computed.fontSize) options.fontSize = pxToPoints(computed.fontSize);
  411. // Apply text-transform on the span element itself
  412. if (computed.textTransform && computed.textTransform !== 'none') {
  413. const transformStr = computed.textTransform;
  414. textTransform = (text) => applyTextTransform(text, transformStr);
  415. }
  416. // Validate: Check for margins on inline elements
  417. if (computed.marginLeft && parseFloat(computed.marginLeft) > 0) {
  418. errors.push(`Inline element <${node.tagName.toLowerCase()}> has margin-left which is not supported in PowerPoint. Remove margin from inline elements.`);
  419. }
  420. if (computed.marginRight && parseFloat(computed.marginRight) > 0) {
  421. errors.push(`Inline element <${node.tagName.toLowerCase()}> has margin-right which is not supported in PowerPoint. Remove margin from inline elements.`);
  422. }
  423. if (computed.marginTop && parseFloat(computed.marginTop) > 0) {
  424. errors.push(`Inline element <${node.tagName.toLowerCase()}> has margin-top which is not supported in PowerPoint. Remove margin from inline elements.`);
  425. }
  426. if (computed.marginBottom && parseFloat(computed.marginBottom) > 0) {
  427. errors.push(`Inline element <${node.tagName.toLowerCase()}> has margin-bottom which is not supported in PowerPoint. Remove margin from inline elements.`);
  428. }
  429. // Recursively process the child node. This will flatten nested spans into multiple runs.
  430. parseInlineFormatting(node, options, runs, textTransform);
  431. }
  432. }
  433. prevNodeIsText = isText;
  434. });
  435. // Trim leading space from first run and trailing space from last run
  436. if (runs.length > 0) {
  437. runs[0].text = runs[0].text.replace(/^\s+/, '');
  438. runs[runs.length - 1].text = runs[runs.length - 1].text.replace(/\s+$/, '');
  439. }
  440. return runs.filter(r => r.text.length > 0);
  441. };
  442. // Extract background from body (image or color)
  443. const body = document.body;
  444. const bodyStyle = window.getComputedStyle(body);
  445. const bgImage = bodyStyle.backgroundImage;
  446. const bgColor = bodyStyle.backgroundColor;
  447. // Collect validation errors
  448. const errors = [];
  449. // Validate: Check for CSS gradients
  450. if (bgImage && (bgImage.includes('linear-gradient') || bgImage.includes('radial-gradient'))) {
  451. errors.push(
  452. 'CSS gradients are not supported. Use Sharp to rasterize gradients as PNG images first, ' +
  453. 'then reference with background-image: url(\'gradient.png\')'
  454. );
  455. }
  456. let background;
  457. if (bgImage && bgImage !== 'none') {
  458. // Extract URL from url("...") or url(...)
  459. const urlMatch = bgImage.match(/url\(["']?([^"')]+)["']?\)/);
  460. if (urlMatch) {
  461. background = {
  462. type: 'image',
  463. path: urlMatch[1]
  464. };
  465. } else {
  466. background = {
  467. type: 'color',
  468. value: rgbToHex(bgColor)
  469. };
  470. }
  471. } else {
  472. background = {
  473. type: 'color',
  474. value: rgbToHex(bgColor)
  475. };
  476. }
  477. // Process all elements
  478. const elements = [];
  479. const placeholders = [];
  480. const textTags = ['P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'UL', 'OL', 'LI'];
  481. const processed = new Set();
  482. document.querySelectorAll('*').forEach((el) => {
  483. if (processed.has(el)) return;
  484. // [data-pptx-merge="true"] — opt-in: merge all <p>/<h1>-<h6> descendants
  485. // into ONE PowerPoint text frame (single editable text box).
  486. // Each child paragraph becomes a run with breakLine:true at the end;
  487. // per-paragraph fontSize/color/bold/italic/underline are preserved as run options.
  488. // The container's bg/border (if any) still becomes its own shape, same as a normal div.
  489. if (el.tagName === 'DIV' && el.dataset && el.dataset.pptxMerge === 'true') {
  490. const containerRect = el.getBoundingClientRect();
  491. if (containerRect.width === 0 || containerRect.height === 0) {
  492. processed.add(el);
  493. return;
  494. }
  495. // Reject nested merge containers — undefined behavior.
  496. if (el.querySelector('[data-pptx-merge="true"]')) {
  497. errors.push(
  498. `data-pptx-merge container cannot contain another data-pptx-merge container. ` +
  499. 'Nested merge is not supported.'
  500. );
  501. processed.add(el);
  502. return;
  503. }
  504. const mergeComputed = window.getComputedStyle(el);
  505. // Container background image — same restriction as regular divs.
  506. if (mergeComputed.backgroundImage && mergeComputed.backgroundImage !== 'none') {
  507. errors.push(
  508. 'Background images on data-pptx-merge container are not supported. ' +
  509. 'Use solid colors or borders, or layer images via slide.addImage().'
  510. );
  511. return;
  512. }
  513. // Emit a shape for the container's bg/uniform-border (mirrors the regular div branch).
  514. const mHasBg = mergeComputed.backgroundColor && mergeComputed.backgroundColor !== 'rgba(0, 0, 0, 0)';
  515. const mBorders = [
  516. mergeComputed.borderTopWidth,
  517. mergeComputed.borderRightWidth,
  518. mergeComputed.borderBottomWidth,
  519. mergeComputed.borderLeftWidth
  520. ].map(b => parseFloat(b) || 0);
  521. const mHasBorder = mBorders.some(b => b > 0);
  522. const mHasUniformBorder = mHasBorder && mBorders.every(b => b === mBorders[0]);
  523. if (mHasBg || mHasUniformBorder) {
  524. elements.push({
  525. type: 'shape',
  526. text: '',
  527. position: {
  528. x: pxToInch(containerRect.left),
  529. y: pxToInch(containerRect.top),
  530. w: pxToInch(containerRect.width),
  531. h: pxToInch(containerRect.height)
  532. },
  533. shape: {
  534. fill: mHasBg ? rgbToHex(mergeComputed.backgroundColor) : null,
  535. transparency: mHasBg ? extractAlpha(mergeComputed.backgroundColor) : null,
  536. line: mHasUniformBorder ? {
  537. color: rgbToHex(mergeComputed.borderColor),
  538. width: pxToPoints(mergeComputed.borderWidth)
  539. } : null,
  540. rectRadius: (() => {
  541. const radius = mergeComputed.borderRadius;
  542. const radiusValue = parseFloat(radius);
  543. if (radiusValue === 0) return 0;
  544. if (radius.includes('%')) {
  545. if (radiusValue >= 50) return 1;
  546. const minDim = Math.min(containerRect.width, containerRect.height);
  547. return (radiusValue / 100) * pxToInch(minDim);
  548. }
  549. if (radius.includes('pt')) return radiusValue / 72;
  550. return radiusValue / PX_PER_IN;
  551. })(),
  552. shadow: parseBoxShadow(mergeComputed.boxShadow)
  553. }
  554. });
  555. }
  556. // Collect <p>/<h*> descendants in document order.
  557. const textDescendants = Array.from(el.querySelectorAll('p, h1, h2, h3, h4, h5, h6'));
  558. if (textDescendants.length === 0) {
  559. errors.push(
  560. `data-pptx-merge container has no <p>/<h*> children to merge. ` +
  561. 'Remove the data-pptx-merge attribute or add text elements.'
  562. );
  563. processed.add(el);
  564. return;
  565. }
  566. // Use the first text element's computed style as the textbox-level base
  567. // (align / lineSpacing / paraSpace are paragraph/textbox-level in pptxgenjs, not per-run).
  568. const firstComputed = window.getComputedStyle(textDescendants[0]);
  569. const baseStyle = {
  570. fontSize: pxToPoints(firstComputed.fontSize),
  571. fontFace: firstComputed.fontFamily.split(',')[0].replace(/['"]/g, '').trim(),
  572. color: rgbToHex(firstComputed.color),
  573. align: firstComputed.textAlign === 'start' ? 'left' : firstComputed.textAlign,
  574. lineSpacing: firstComputed.lineHeight && firstComputed.lineHeight !== 'normal'
  575. ? pxToPoints(firstComputed.lineHeight)
  576. : null,
  577. paraSpaceBefore: 0,
  578. paraSpaceAfter: 0,
  579. // Container padding becomes the textbox internal margin (PptxGenJS: [left, right, bottom, top]).
  580. margin: [
  581. pxToPoints(mergeComputed.paddingLeft),
  582. pxToPoints(mergeComputed.paddingRight),
  583. pxToPoints(mergeComputed.paddingBottom),
  584. pxToPoints(mergeComputed.paddingTop)
  585. ]
  586. };
  587. const baseTransparency = extractAlpha(firstComputed.color);
  588. if (baseTransparency !== null) baseStyle.transparency = baseTransparency;
  589. // Build the merged runs.
  590. const mergedRuns = [];
  591. textDescendants.forEach((textEl, idx) => {
  592. const isLast = idx === textDescendants.length - 1;
  593. const tComputed = window.getComputedStyle(textEl);
  594. const transformStr = tComputed.textTransform;
  595. // Per-paragraph style overrides — only include if they differ from base.
  596. const elemFontSize = pxToPoints(tComputed.fontSize);
  597. const elemFontFace = tComputed.fontFamily.split(',')[0].replace(/['"]/g, '').trim();
  598. const elemColor = rgbToHex(tComputed.color);
  599. const elemBold = tComputed.fontWeight === 'bold' || parseInt(tComputed.fontWeight) >= 600;
  600. const elemItalic = tComputed.fontStyle === 'italic';
  601. const elemUnderline = tComputed.textDecoration.includes('underline');
  602. const runBaseOptions = {};
  603. if (elemFontSize !== baseStyle.fontSize) runBaseOptions.fontSize = elemFontSize;
  604. if (elemFontFace !== baseStyle.fontFace) runBaseOptions.fontFace = elemFontFace;
  605. if (elemColor !== baseStyle.color) runBaseOptions.color = elemColor;
  606. if (elemBold && !shouldSkipBold(tComputed.fontFamily)) runBaseOptions.bold = true;
  607. if (elemItalic) runBaseOptions.italic = true;
  608. if (elemUnderline) runBaseOptions.underline = true;
  609. const hasInline = textEl.querySelector('b, i, u, strong, em, span, br');
  610. let runs;
  611. if (hasInline) {
  612. runs = parseInlineFormatting(
  613. textEl,
  614. runBaseOptions,
  615. [],
  616. (str) => applyTextTransform(str, transformStr)
  617. );
  618. } else {
  619. const txt = applyTextTransform(textEl.textContent.trim(), transformStr);
  620. if (!txt) return;
  621. runs = [{ text: txt, options: { ...runBaseOptions } }];
  622. }
  623. if (runs.length > 0 && !isLast) {
  624. runs[runs.length - 1].options.breakLine = true;
  625. }
  626. mergedRuns.push(...runs);
  627. processed.add(textEl);
  628. });
  629. if (mergedRuns.length === 0) {
  630. processed.add(el);
  631. return;
  632. }
  633. elements.push({
  634. type: 'merged-text',
  635. items: mergedRuns,
  636. position: {
  637. x: pxToInch(containerRect.left),
  638. y: pxToInch(containerRect.top),
  639. w: pxToInch(containerRect.width),
  640. h: pxToInch(containerRect.height)
  641. },
  642. style: baseStyle
  643. });
  644. processed.add(el);
  645. return;
  646. }
  647. // Validate text elements don't have backgrounds, borders, or shadows
  648. if (textTags.includes(el.tagName)) {
  649. const computed = window.getComputedStyle(el);
  650. const hasBg = computed.backgroundColor && computed.backgroundColor !== 'rgba(0, 0, 0, 0)';
  651. const hasBorder = (computed.borderWidth && parseFloat(computed.borderWidth) > 0) ||
  652. (computed.borderTopWidth && parseFloat(computed.borderTopWidth) > 0) ||
  653. (computed.borderRightWidth && parseFloat(computed.borderRightWidth) > 0) ||
  654. (computed.borderBottomWidth && parseFloat(computed.borderBottomWidth) > 0) ||
  655. (computed.borderLeftWidth && parseFloat(computed.borderLeftWidth) > 0);
  656. const hasShadow = computed.boxShadow && computed.boxShadow !== 'none';
  657. if (hasBg || hasBorder || hasShadow) {
  658. errors.push(
  659. `Text element <${el.tagName.toLowerCase()}> has ${hasBg ? 'background' : hasBorder ? 'border' : 'shadow'}. ` +
  660. 'Backgrounds, borders, and shadows are only supported on <div> elements, not text elements.'
  661. );
  662. return;
  663. }
  664. }
  665. // Extract placeholder elements (for charts, etc.)
  666. if (el.className && el.className.includes('placeholder')) {
  667. const rect = el.getBoundingClientRect();
  668. if (rect.width === 0 || rect.height === 0) {
  669. errors.push(
  670. `Placeholder "${el.id || 'unnamed'}" has ${rect.width === 0 ? 'width: 0' : 'height: 0'}. Check the layout CSS.`
  671. );
  672. } else {
  673. placeholders.push({
  674. id: el.id || `placeholder-${placeholders.length}`,
  675. x: pxToInch(rect.left),
  676. y: pxToInch(rect.top),
  677. w: pxToInch(rect.width),
  678. h: pxToInch(rect.height)
  679. });
  680. }
  681. processed.add(el);
  682. return;
  683. }
  684. // Extract images
  685. if (el.tagName === 'IMG') {
  686. const rect = el.getBoundingClientRect();
  687. if (rect.width > 0 && rect.height > 0) {
  688. elements.push({
  689. type: 'image',
  690. src: el.src,
  691. position: {
  692. x: pxToInch(rect.left),
  693. y: pxToInch(rect.top),
  694. w: pxToInch(rect.width),
  695. h: pxToInch(rect.height)
  696. }
  697. });
  698. processed.add(el);
  699. return;
  700. }
  701. }
  702. // Extract DIVs with backgrounds/borders as shapes
  703. const isContainer = el.tagName === 'DIV' && !textTags.includes(el.tagName);
  704. if (isContainer) {
  705. const computed = window.getComputedStyle(el);
  706. const hasBg = computed.backgroundColor && computed.backgroundColor !== 'rgba(0, 0, 0, 0)';
  707. // Validate: Check for unwrapped text content in DIV
  708. for (const node of el.childNodes) {
  709. if (node.nodeType === Node.TEXT_NODE) {
  710. const text = node.textContent.trim();
  711. if (text) {
  712. errors.push(
  713. `DIV element contains unwrapped text "${text.substring(0, 50)}${text.length > 50 ? '...' : ''}". ` +
  714. 'All text must be wrapped in <p>, <h1>-<h6>, <ul>, or <ol> tags to appear in PowerPoint.'
  715. );
  716. }
  717. }
  718. }
  719. // Check for background images on shapes
  720. const bgImage = computed.backgroundImage;
  721. if (bgImage && bgImage !== 'none') {
  722. errors.push(
  723. 'Background images on DIV elements are not supported. ' +
  724. 'Use solid colors or borders for shapes, or use slide.addImage() in PptxGenJS to layer images.'
  725. );
  726. return;
  727. }
  728. // Check for borders - both uniform and partial
  729. const borderTop = computed.borderTopWidth;
  730. const borderRight = computed.borderRightWidth;
  731. const borderBottom = computed.borderBottomWidth;
  732. const borderLeft = computed.borderLeftWidth;
  733. const borders = [borderTop, borderRight, borderBottom, borderLeft].map(b => parseFloat(b) || 0);
  734. const hasBorder = borders.some(b => b > 0);
  735. const hasUniformBorder = hasBorder && borders.every(b => b === borders[0]);
  736. const borderLines = [];
  737. if (hasBorder && !hasUniformBorder) {
  738. const rect = el.getBoundingClientRect();
  739. const x = pxToInch(rect.left);
  740. const y = pxToInch(rect.top);
  741. const w = pxToInch(rect.width);
  742. const h = pxToInch(rect.height);
  743. // Collect lines to add after shape (inset by half the line width to center on edge)
  744. if (parseFloat(borderTop) > 0) {
  745. const widthPt = pxToPoints(borderTop);
  746. const inset = (widthPt / 72) / 2; // Convert points to inches, then half
  747. borderLines.push({
  748. type: 'line',
  749. x1: x, y1: y + inset, x2: x + w, y2: y + inset,
  750. width: widthPt,
  751. color: rgbToHex(computed.borderTopColor)
  752. });
  753. }
  754. if (parseFloat(borderRight) > 0) {
  755. const widthPt = pxToPoints(borderRight);
  756. const inset = (widthPt / 72) / 2;
  757. borderLines.push({
  758. type: 'line',
  759. x1: x + w - inset, y1: y, x2: x + w - inset, y2: y + h,
  760. width: widthPt,
  761. color: rgbToHex(computed.borderRightColor)
  762. });
  763. }
  764. if (parseFloat(borderBottom) > 0) {
  765. const widthPt = pxToPoints(borderBottom);
  766. const inset = (widthPt / 72) / 2;
  767. borderLines.push({
  768. type: 'line',
  769. x1: x, y1: y + h - inset, x2: x + w, y2: y + h - inset,
  770. width: widthPt,
  771. color: rgbToHex(computed.borderBottomColor)
  772. });
  773. }
  774. if (parseFloat(borderLeft) > 0) {
  775. const widthPt = pxToPoints(borderLeft);
  776. const inset = (widthPt / 72) / 2;
  777. borderLines.push({
  778. type: 'line',
  779. x1: x + inset, y1: y, x2: x + inset, y2: y + h,
  780. width: widthPt,
  781. color: rgbToHex(computed.borderLeftColor)
  782. });
  783. }
  784. }
  785. if (hasBg || hasBorder) {
  786. const rect = el.getBoundingClientRect();
  787. if (rect.width > 0 && rect.height > 0) {
  788. const shadow = parseBoxShadow(computed.boxShadow);
  789. // Only add shape if there's background or uniform border
  790. if (hasBg || hasUniformBorder) {
  791. elements.push({
  792. type: 'shape',
  793. text: '', // Shape only - child text elements render on top
  794. position: {
  795. x: pxToInch(rect.left),
  796. y: pxToInch(rect.top),
  797. w: pxToInch(rect.width),
  798. h: pxToInch(rect.height)
  799. },
  800. shape: {
  801. fill: hasBg ? rgbToHex(computed.backgroundColor) : null,
  802. transparency: hasBg ? extractAlpha(computed.backgroundColor) : null,
  803. line: hasUniformBorder ? {
  804. color: rgbToHex(computed.borderColor),
  805. width: pxToPoints(computed.borderWidth)
  806. } : null,
  807. // Convert border-radius to rectRadius (in inches)
  808. // % values: 50%+ = circle (1), <50% = percentage of min dimension
  809. // pt values: divide by 72 (72pt = 1 inch)
  810. // px values: divide by 96 (96px = 1 inch)
  811. rectRadius: (() => {
  812. const radius = computed.borderRadius;
  813. const radiusValue = parseFloat(radius);
  814. if (radiusValue === 0) return 0;
  815. if (radius.includes('%')) {
  816. if (radiusValue >= 50) return 1;
  817. // Calculate percentage of smaller dimension
  818. const minDim = Math.min(rect.width, rect.height);
  819. return (radiusValue / 100) * pxToInch(minDim);
  820. }
  821. if (radius.includes('pt')) return radiusValue / 72;
  822. return radiusValue / PX_PER_IN;
  823. })(),
  824. shadow: shadow
  825. }
  826. });
  827. }
  828. // Add partial border lines
  829. elements.push(...borderLines);
  830. processed.add(el);
  831. return;
  832. }
  833. }
  834. }
  835. // Extract bullet lists as single text block
  836. if (el.tagName === 'UL' || el.tagName === 'OL') {
  837. const rect = el.getBoundingClientRect();
  838. if (rect.width === 0 || rect.height === 0) return;
  839. const liElements = Array.from(el.querySelectorAll('li'));
  840. const items = [];
  841. const ulComputed = window.getComputedStyle(el);
  842. const ulPaddingLeftPt = pxToPoints(ulComputed.paddingLeft);
  843. // Split: margin-left for bullet position, indent for text position
  844. // margin-left + indent = ul padding-left
  845. const marginLeft = ulPaddingLeftPt * 0.5;
  846. const textIndent = ulPaddingLeftPt * 0.5;
  847. liElements.forEach((li, idx) => {
  848. const isLast = idx === liElements.length - 1;
  849. const runs = parseInlineFormatting(li, { breakLine: false });
  850. // Clean manual bullets from first run
  851. if (runs.length > 0) {
  852. runs[0].text = runs[0].text.replace(/^[•\-\*▪▸]\s*/, '');
  853. runs[0].options.bullet = { indent: textIndent };
  854. }
  855. // Set breakLine on last run
  856. if (runs.length > 0 && !isLast) {
  857. runs[runs.length - 1].options.breakLine = true;
  858. }
  859. items.push(...runs);
  860. });
  861. const computed = window.getComputedStyle(liElements[0] || el);
  862. elements.push({
  863. type: 'list',
  864. items: items,
  865. position: {
  866. x: pxToInch(rect.left),
  867. y: pxToInch(rect.top),
  868. w: pxToInch(rect.width),
  869. h: pxToInch(rect.height)
  870. },
  871. style: {
  872. fontSize: pxToPoints(computed.fontSize),
  873. fontFace: computed.fontFamily.split(',')[0].replace(/['"]/g, '').trim(),
  874. color: rgbToHex(computed.color),
  875. transparency: extractAlpha(computed.color),
  876. align: computed.textAlign === 'start' ? 'left' : computed.textAlign,
  877. lineSpacing: computed.lineHeight && computed.lineHeight !== 'normal' ? pxToPoints(computed.lineHeight) : null,
  878. paraSpaceBefore: 0,
  879. paraSpaceAfter: pxToPoints(computed.marginBottom),
  880. // PptxGenJS margin array is [left, right, bottom, top]
  881. margin: [marginLeft, 0, 0, 0]
  882. }
  883. });
  884. liElements.forEach(li => processed.add(li));
  885. processed.add(el);
  886. return;
  887. }
  888. // Extract text elements (P, H1, H2, etc.)
  889. if (!textTags.includes(el.tagName)) return;
  890. const rect = el.getBoundingClientRect();
  891. const text = el.textContent.trim();
  892. if (rect.width === 0 || rect.height === 0 || !text) return;
  893. // Validate: Check for manual bullet symbols in text elements (not in lists)
  894. if (el.tagName !== 'LI' && /^[•\-\*▪▸○●◆◇■□]\s/.test(text.trimStart())) {
  895. errors.push(
  896. `Text element <${el.tagName.toLowerCase()}> starts with bullet symbol "${text.substring(0, 20)}...". ` +
  897. 'Use <ul> or <ol> lists instead of manual bullet symbols.'
  898. );
  899. return;
  900. }
  901. const computed = window.getComputedStyle(el);
  902. const rotation = getRotation(computed.transform, computed.writingMode);
  903. const { x, y, w, h } = getPositionAndSize(el, rect, rotation);
  904. const baseStyle = {
  905. fontSize: pxToPoints(computed.fontSize),
  906. fontFace: computed.fontFamily.split(',')[0].replace(/['"]/g, '').trim(),
  907. color: rgbToHex(computed.color),
  908. align: computed.textAlign === 'start' ? 'left' : computed.textAlign,
  909. lineSpacing: pxToPoints(computed.lineHeight),
  910. paraSpaceBefore: pxToPoints(computed.marginTop),
  911. paraSpaceAfter: pxToPoints(computed.marginBottom),
  912. // PptxGenJS margin array is [left, right, bottom, top] (not [top, right, bottom, left] as documented)
  913. margin: [
  914. pxToPoints(computed.paddingLeft),
  915. pxToPoints(computed.paddingRight),
  916. pxToPoints(computed.paddingBottom),
  917. pxToPoints(computed.paddingTop)
  918. ]
  919. };
  920. const transparency = extractAlpha(computed.color);
  921. if (transparency !== null) baseStyle.transparency = transparency;
  922. if (rotation !== null) baseStyle.rotate = rotation;
  923. const hasFormatting = el.querySelector('b, i, u, strong, em, span, br');
  924. if (hasFormatting) {
  925. // Text with inline formatting
  926. const transformStr = computed.textTransform;
  927. const runs = parseInlineFormatting(el, {}, [], (str) => applyTextTransform(str, transformStr));
  928. // Adjust lineSpacing based on largest fontSize in runs
  929. const adjustedStyle = { ...baseStyle };
  930. if (adjustedStyle.lineSpacing) {
  931. const maxFontSize = Math.max(
  932. adjustedStyle.fontSize,
  933. ...runs.map(r => r.options?.fontSize || 0)
  934. );
  935. if (maxFontSize > adjustedStyle.fontSize) {
  936. const lineHeightMultiplier = adjustedStyle.lineSpacing / adjustedStyle.fontSize;
  937. adjustedStyle.lineSpacing = maxFontSize * lineHeightMultiplier;
  938. }
  939. }
  940. elements.push({
  941. type: el.tagName.toLowerCase(),
  942. text: runs,
  943. position: { x: pxToInch(x), y: pxToInch(y), w: pxToInch(w), h: pxToInch(h) },
  944. style: adjustedStyle
  945. });
  946. } else {
  947. // Plain text - inherit CSS formatting
  948. const textTransform = computed.textTransform;
  949. const transformedText = applyTextTransform(text, textTransform);
  950. const isBold = computed.fontWeight === 'bold' || parseInt(computed.fontWeight) >= 600;
  951. elements.push({
  952. type: el.tagName.toLowerCase(),
  953. text: transformedText,
  954. position: { x: pxToInch(x), y: pxToInch(y), w: pxToInch(w), h: pxToInch(h) },
  955. style: {
  956. ...baseStyle,
  957. bold: isBold && !shouldSkipBold(computed.fontFamily),
  958. italic: computed.fontStyle === 'italic',
  959. underline: computed.textDecoration.includes('underline')
  960. }
  961. });
  962. }
  963. processed.add(el);
  964. });
  965. return { background, elements, placeholders, errors };
  966. });
  967. }
  968. async function html2pptx(htmlFile, pres, options = {}) {
  969. const {
  970. tmpDir = process.env.TMPDIR || '/tmp',
  971. slide = null
  972. } = options;
  973. try {
  974. // Use Chrome on macOS, default Chromium on Unix
  975. const launchOptions = { env: { TMPDIR: tmpDir } };
  976. if (process.platform === 'darwin') {
  977. launchOptions.channel = 'chrome';
  978. }
  979. const browser = await chromium.launch(launchOptions);
  980. let bodyDimensions;
  981. let slideData;
  982. const filePath = path.isAbsolute(htmlFile) ? htmlFile : path.join(process.cwd(), htmlFile);
  983. const validationErrors = [];
  984. try {
  985. const page = await browser.newPage();
  986. page.on('console', (msg) => {
  987. // Log the message text to your test runner's console
  988. console.log(`Browser console: ${msg.text()}`);
  989. });
  990. await page.goto(`file://${filePath}`);
  991. bodyDimensions = await getBodyDimensions(page);
  992. await page.setViewportSize({
  993. width: Math.round(bodyDimensions.width),
  994. height: Math.round(bodyDimensions.height)
  995. });
  996. slideData = await extractSlideData(page);
  997. } finally {
  998. await browser.close();
  999. }
  1000. // Collect all validation errors
  1001. if (bodyDimensions.errors && bodyDimensions.errors.length > 0) {
  1002. validationErrors.push(...bodyDimensions.errors);
  1003. }
  1004. const dimensionErrors = validateDimensions(bodyDimensions, pres);
  1005. if (dimensionErrors.length > 0) {
  1006. validationErrors.push(...dimensionErrors);
  1007. }
  1008. const textBoxPositionErrors = validateTextBoxPosition(slideData, bodyDimensions);
  1009. if (textBoxPositionErrors.length > 0) {
  1010. validationErrors.push(...textBoxPositionErrors);
  1011. }
  1012. if (slideData.errors && slideData.errors.length > 0) {
  1013. validationErrors.push(...slideData.errors);
  1014. }
  1015. // Throw all errors at once if any exist
  1016. if (validationErrors.length > 0) {
  1017. const errorMessage = validationErrors.length === 1
  1018. ? validationErrors[0]
  1019. : `Multiple validation errors found:\n${validationErrors.map((e, i) => ` ${i + 1}. ${e}`).join('\n')}`;
  1020. throw new Error(errorMessage);
  1021. }
  1022. const targetSlide = slide || pres.addSlide();
  1023. await addBackground(slideData, targetSlide, tmpDir);
  1024. addElements(slideData, targetSlide, pres);
  1025. return { slide: targetSlide, placeholders: slideData.placeholders };
  1026. } catch (error) {
  1027. if (!error.message.startsWith(htmlFile)) {
  1028. throw new Error(`${htmlFile}: ${error.message}`);
  1029. }
  1030. throw error;
  1031. }
  1032. }
  1033. module.exports = html2pptx;