html2pptx.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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)
  82. if (['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'list'].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 {
  169. // Check if text is single-line (height suggests one line)
  170. const lineHeight = el.style.lineSpacing || el.style.fontSize * 1.2;
  171. const isSingleLine = el.position.h <= lineHeight * 1.5;
  172. let adjustedX = el.position.x;
  173. let adjustedW = el.position.w;
  174. // Make single-line text 2% wider to account for underestimate
  175. if (isSingleLine) {
  176. const widthIncrease = el.position.w * 0.02;
  177. const align = el.style.align;
  178. if (align === 'center') {
  179. // Center: expand both sides
  180. adjustedX = el.position.x - (widthIncrease / 2);
  181. adjustedW = el.position.w + widthIncrease;
  182. } else if (align === 'right') {
  183. // Right: expand to the left
  184. adjustedX = el.position.x - widthIncrease;
  185. adjustedW = el.position.w + widthIncrease;
  186. } else {
  187. // Left (default): expand to the right
  188. adjustedW = el.position.w + widthIncrease;
  189. }
  190. }
  191. const textOptions = {
  192. x: adjustedX,
  193. y: el.position.y,
  194. w: adjustedW,
  195. h: el.position.h,
  196. fontSize: el.style.fontSize,
  197. fontFace: el.style.fontFace,
  198. color: el.style.color,
  199. bold: el.style.bold,
  200. italic: el.style.italic,
  201. underline: el.style.underline,
  202. valign: 'top',
  203. lineSpacing: el.style.lineSpacing,
  204. paraSpaceBefore: el.style.paraSpaceBefore,
  205. paraSpaceAfter: el.style.paraSpaceAfter,
  206. inset: 0 // Remove default PowerPoint internal padding
  207. };
  208. if (el.style.align) textOptions.align = el.style.align;
  209. if (el.style.margin) textOptions.margin = el.style.margin;
  210. if (el.style.rotate !== undefined) textOptions.rotate = el.style.rotate;
  211. if (el.style.transparency !== null && el.style.transparency !== undefined) textOptions.transparency = el.style.transparency;
  212. targetSlide.addText(el.text, textOptions);
  213. }
  214. }
  215. }
  216. // Helper: Extract slide data from HTML page
  217. async function extractSlideData(page) {
  218. return await page.evaluate(() => {
  219. const PT_PER_PX = 0.75;
  220. const PX_PER_IN = 96;
  221. // Fonts that are single-weight and should not have bold applied
  222. // (applying bold causes PowerPoint to use faux bold which makes text wider)
  223. const SINGLE_WEIGHT_FONTS = ['impact'];
  224. // Helper: Check if a font should skip bold formatting
  225. const shouldSkipBold = (fontFamily) => {
  226. if (!fontFamily) return false;
  227. const normalizedFont = fontFamily.toLowerCase().replace(/['"]/g, '').split(',')[0].trim();
  228. return SINGLE_WEIGHT_FONTS.includes(normalizedFont);
  229. };
  230. // Unit conversion helpers
  231. const pxToInch = (px) => px / PX_PER_IN;
  232. const pxToPoints = (pxStr) => parseFloat(pxStr) * PT_PER_PX;
  233. const rgbToHex = (rgbStr) => {
  234. // Handle transparent backgrounds by defaulting to white
  235. if (rgbStr === 'rgba(0, 0, 0, 0)' || rgbStr === 'transparent') return 'FFFFFF';
  236. const match = rgbStr.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);
  237. if (!match) return 'FFFFFF';
  238. return match.slice(1).map(n => parseInt(n).toString(16).padStart(2, '0')).join('');
  239. };
  240. const extractAlpha = (rgbStr) => {
  241. const match = rgbStr.match(/rgba\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)/);
  242. if (!match || !match[4]) return null;
  243. const alpha = parseFloat(match[4]);
  244. return Math.round((1 - alpha) * 100);
  245. };
  246. const applyTextTransform = (text, textTransform) => {
  247. if (textTransform === 'uppercase') return text.toUpperCase();
  248. if (textTransform === 'lowercase') return text.toLowerCase();
  249. if (textTransform === 'capitalize') {
  250. return text.replace(/\b\w/g, c => c.toUpperCase());
  251. }
  252. return text;
  253. };
  254. // Extract rotation angle from CSS transform and writing-mode
  255. const getRotation = (transform, writingMode) => {
  256. let angle = 0;
  257. // Handle writing-mode first
  258. // PowerPoint: 90° = text rotated 90° clockwise (reads top to bottom, letters upright)
  259. // PowerPoint: 270° = text rotated 270° clockwise (reads bottom to top, letters upright)
  260. if (writingMode === 'vertical-rl') {
  261. // vertical-rl alone = text reads top to bottom = 90° in PowerPoint
  262. angle = 90;
  263. } else if (writingMode === 'vertical-lr') {
  264. // vertical-lr alone = text reads bottom to top = 270° in PowerPoint
  265. angle = 270;
  266. }
  267. // Then add any transform rotation
  268. if (transform && transform !== 'none') {
  269. // Try to match rotate() function
  270. const rotateMatch = transform.match(/rotate\((-?\d+(?:\.\d+)?)deg\)/);
  271. if (rotateMatch) {
  272. angle += parseFloat(rotateMatch[1]);
  273. } else {
  274. // Browser may compute as matrix - extract rotation from matrix
  275. const matrixMatch = transform.match(/matrix\(([^)]+)\)/);
  276. if (matrixMatch) {
  277. const values = matrixMatch[1].split(',').map(parseFloat);
  278. // matrix(a, b, c, d, e, f) where rotation = atan2(b, a)
  279. const matrixAngle = Math.atan2(values[1], values[0]) * (180 / Math.PI);
  280. angle += Math.round(matrixAngle);
  281. }
  282. }
  283. }
  284. // Normalize to 0-359 range
  285. angle = angle % 360;
  286. if (angle < 0) angle += 360;
  287. return angle === 0 ? null : angle;
  288. };
  289. // Get position/dimensions accounting for rotation
  290. const getPositionAndSize = (el, rect, rotation) => {
  291. if (rotation === null) {
  292. return { x: rect.left, y: rect.top, w: rect.width, h: rect.height };
  293. }
  294. // For 90° or 270° rotations, swap width and height
  295. // because PowerPoint applies rotation to the original (unrotated) box
  296. const isVertical = rotation === 90 || rotation === 270;
  297. if (isVertical) {
  298. // The browser shows us the rotated dimensions (tall box for vertical text)
  299. // But PowerPoint needs the pre-rotation dimensions (wide box that will be rotated)
  300. // So we swap: browser's height becomes PPT's width, browser's width becomes PPT's height
  301. const centerX = rect.left + rect.width / 2;
  302. const centerY = rect.top + rect.height / 2;
  303. return {
  304. x: centerX - rect.height / 2,
  305. y: centerY - rect.width / 2,
  306. w: rect.height,
  307. h: rect.width
  308. };
  309. }
  310. // For other rotations, use element's offset dimensions
  311. const centerX = rect.left + rect.width / 2;
  312. const centerY = rect.top + rect.height / 2;
  313. return {
  314. x: centerX - el.offsetWidth / 2,
  315. y: centerY - el.offsetHeight / 2,
  316. w: el.offsetWidth,
  317. h: el.offsetHeight
  318. };
  319. };
  320. // Parse CSS box-shadow into PptxGenJS shadow properties
  321. const parseBoxShadow = (boxShadow) => {
  322. if (!boxShadow || boxShadow === 'none') return null;
  323. // Browser computed style format: "rgba(0, 0, 0, 0.3) 2px 2px 8px 0px [inset]"
  324. // CSS format: "[inset] 2px 2px 8px 0px rgba(0, 0, 0, 0.3)"
  325. const insetMatch = boxShadow.match(/inset/);
  326. // IMPORTANT: PptxGenJS/PowerPoint doesn't properly support inset shadows
  327. // Only process outer shadows to avoid file corruption
  328. if (insetMatch) return null;
  329. // Extract color first (rgba or rgb at start)
  330. const colorMatch = boxShadow.match(/rgba?\([^)]+\)/);
  331. // Extract numeric values (handles both px and pt units)
  332. const parts = boxShadow.match(/([-\d.]+)(px|pt)/g);
  333. if (!parts || parts.length < 2) return null;
  334. const offsetX = parseFloat(parts[0]);
  335. const offsetY = parseFloat(parts[1]);
  336. const blur = parts.length > 2 ? parseFloat(parts[2]) : 0;
  337. // Calculate angle from offsets (in degrees, 0 = right, 90 = down)
  338. let angle = 0;
  339. if (offsetX !== 0 || offsetY !== 0) {
  340. angle = Math.atan2(offsetY, offsetX) * (180 / Math.PI);
  341. if (angle < 0) angle += 360;
  342. }
  343. // Calculate offset distance (hypotenuse)
  344. const offset = Math.sqrt(offsetX * offsetX + offsetY * offsetY) * PT_PER_PX;
  345. // Extract opacity from rgba
  346. let opacity = 0.5;
  347. if (colorMatch) {
  348. const opacityMatch = colorMatch[0].match(/[\d.]+\)$/);
  349. if (opacityMatch) {
  350. opacity = parseFloat(opacityMatch[0].replace(')', ''));
  351. }
  352. }
  353. return {
  354. type: 'outer',
  355. angle: Math.round(angle),
  356. blur: blur * 0.75, // Convert to points
  357. color: colorMatch ? rgbToHex(colorMatch[0]) : '000000',
  358. offset: offset,
  359. opacity
  360. };
  361. };
  362. // Parse inline formatting tags (<b>, <i>, <u>, <strong>, <em>, <span>) into text runs
  363. const parseInlineFormatting = (element, baseOptions = {}, runs = [], baseTextTransform = (x) => x) => {
  364. let prevNodeIsText = false;
  365. element.childNodes.forEach((node) => {
  366. let textTransform = baseTextTransform;
  367. const isText = node.nodeType === Node.TEXT_NODE || node.tagName === 'BR';
  368. if (isText) {
  369. const text = node.tagName === 'BR' ? '\n' : textTransform(node.textContent.replace(/\s+/g, ' '));
  370. const prevRun = runs[runs.length - 1];
  371. if (prevNodeIsText && prevRun) {
  372. prevRun.text += text;
  373. } else {
  374. runs.push({ text, options: { ...baseOptions } });
  375. }
  376. } else if (node.nodeType === Node.ELEMENT_NODE && node.textContent.trim()) {
  377. const options = { ...baseOptions };
  378. const computed = window.getComputedStyle(node);
  379. // Handle inline elements with computed styles
  380. if (node.tagName === 'SPAN' || node.tagName === 'B' || node.tagName === 'STRONG' || node.tagName === 'I' || node.tagName === 'EM' || node.tagName === 'U') {
  381. const isBold = computed.fontWeight === 'bold' || parseInt(computed.fontWeight) >= 600;
  382. if (isBold && !shouldSkipBold(computed.fontFamily)) options.bold = true;
  383. if (computed.fontStyle === 'italic') options.italic = true;
  384. if (computed.textDecoration && computed.textDecoration.includes('underline')) options.underline = true;
  385. if (computed.color && computed.color !== 'rgb(0, 0, 0)') {
  386. options.color = rgbToHex(computed.color);
  387. const transparency = extractAlpha(computed.color);
  388. if (transparency !== null) options.transparency = transparency;
  389. }
  390. if (computed.fontSize) options.fontSize = pxToPoints(computed.fontSize);
  391. // Apply text-transform on the span element itself
  392. if (computed.textTransform && computed.textTransform !== 'none') {
  393. const transformStr = computed.textTransform;
  394. textTransform = (text) => applyTextTransform(text, transformStr);
  395. }
  396. // Validate: Check for margins on inline elements
  397. if (computed.marginLeft && parseFloat(computed.marginLeft) > 0) {
  398. errors.push(`Inline element <${node.tagName.toLowerCase()}> has margin-left which is not supported in PowerPoint. Remove margin from inline elements.`);
  399. }
  400. if (computed.marginRight && parseFloat(computed.marginRight) > 0) {
  401. errors.push(`Inline element <${node.tagName.toLowerCase()}> has margin-right which is not supported in PowerPoint. Remove margin from inline elements.`);
  402. }
  403. if (computed.marginTop && parseFloat(computed.marginTop) > 0) {
  404. errors.push(`Inline element <${node.tagName.toLowerCase()}> has margin-top which is not supported in PowerPoint. Remove margin from inline elements.`);
  405. }
  406. if (computed.marginBottom && parseFloat(computed.marginBottom) > 0) {
  407. errors.push(`Inline element <${node.tagName.toLowerCase()}> has margin-bottom which is not supported in PowerPoint. Remove margin from inline elements.`);
  408. }
  409. // Recursively process the child node. This will flatten nested spans into multiple runs.
  410. parseInlineFormatting(node, options, runs, textTransform);
  411. }
  412. }
  413. prevNodeIsText = isText;
  414. });
  415. // Trim leading space from first run and trailing space from last run
  416. if (runs.length > 0) {
  417. runs[0].text = runs[0].text.replace(/^\s+/, '');
  418. runs[runs.length - 1].text = runs[runs.length - 1].text.replace(/\s+$/, '');
  419. }
  420. return runs.filter(r => r.text.length > 0);
  421. };
  422. // Extract background from body (image or color)
  423. const body = document.body;
  424. const bodyStyle = window.getComputedStyle(body);
  425. const bgImage = bodyStyle.backgroundImage;
  426. const bgColor = bodyStyle.backgroundColor;
  427. // Collect validation errors
  428. const errors = [];
  429. // Validate: Check for CSS gradients
  430. if (bgImage && (bgImage.includes('linear-gradient') || bgImage.includes('radial-gradient'))) {
  431. errors.push(
  432. 'CSS gradients are not supported. Use Sharp to rasterize gradients as PNG images first, ' +
  433. 'then reference with background-image: url(\'gradient.png\')'
  434. );
  435. }
  436. let background;
  437. if (bgImage && bgImage !== 'none') {
  438. // Extract URL from url("...") or url(...)
  439. const urlMatch = bgImage.match(/url\(["']?([^"')]+)["']?\)/);
  440. if (urlMatch) {
  441. background = {
  442. type: 'image',
  443. path: urlMatch[1]
  444. };
  445. } else {
  446. background = {
  447. type: 'color',
  448. value: rgbToHex(bgColor)
  449. };
  450. }
  451. } else {
  452. background = {
  453. type: 'color',
  454. value: rgbToHex(bgColor)
  455. };
  456. }
  457. // Process all elements
  458. const elements = [];
  459. const placeholders = [];
  460. const textTags = ['P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'UL', 'OL', 'LI'];
  461. const processed = new Set();
  462. document.querySelectorAll('*').forEach((el) => {
  463. if (processed.has(el)) return;
  464. // Validate text elements don't have backgrounds, borders, or shadows
  465. if (textTags.includes(el.tagName)) {
  466. const computed = window.getComputedStyle(el);
  467. const hasBg = computed.backgroundColor && computed.backgroundColor !== 'rgba(0, 0, 0, 0)';
  468. const hasBorder = (computed.borderWidth && parseFloat(computed.borderWidth) > 0) ||
  469. (computed.borderTopWidth && parseFloat(computed.borderTopWidth) > 0) ||
  470. (computed.borderRightWidth && parseFloat(computed.borderRightWidth) > 0) ||
  471. (computed.borderBottomWidth && parseFloat(computed.borderBottomWidth) > 0) ||
  472. (computed.borderLeftWidth && parseFloat(computed.borderLeftWidth) > 0);
  473. const hasShadow = computed.boxShadow && computed.boxShadow !== 'none';
  474. if (hasBg || hasBorder || hasShadow) {
  475. errors.push(
  476. `Text element <${el.tagName.toLowerCase()}> has ${hasBg ? 'background' : hasBorder ? 'border' : 'shadow'}. ` +
  477. 'Backgrounds, borders, and shadows are only supported on <div> elements, not text elements.'
  478. );
  479. return;
  480. }
  481. }
  482. // Extract placeholder elements (for charts, etc.)
  483. if (el.className && el.className.includes('placeholder')) {
  484. const rect = el.getBoundingClientRect();
  485. if (rect.width === 0 || rect.height === 0) {
  486. errors.push(
  487. `Placeholder "${el.id || 'unnamed'}" has ${rect.width === 0 ? 'width: 0' : 'height: 0'}. Check the layout CSS.`
  488. );
  489. } else {
  490. placeholders.push({
  491. id: el.id || `placeholder-${placeholders.length}`,
  492. x: pxToInch(rect.left),
  493. y: pxToInch(rect.top),
  494. w: pxToInch(rect.width),
  495. h: pxToInch(rect.height)
  496. });
  497. }
  498. processed.add(el);
  499. return;
  500. }
  501. // Extract images
  502. if (el.tagName === 'IMG') {
  503. const rect = el.getBoundingClientRect();
  504. if (rect.width > 0 && rect.height > 0) {
  505. elements.push({
  506. type: 'image',
  507. src: el.src,
  508. position: {
  509. x: pxToInch(rect.left),
  510. y: pxToInch(rect.top),
  511. w: pxToInch(rect.width),
  512. h: pxToInch(rect.height)
  513. }
  514. });
  515. processed.add(el);
  516. return;
  517. }
  518. }
  519. // Extract DIVs with backgrounds/borders as shapes
  520. const isContainer = el.tagName === 'DIV' && !textTags.includes(el.tagName);
  521. if (isContainer) {
  522. const computed = window.getComputedStyle(el);
  523. const hasBg = computed.backgroundColor && computed.backgroundColor !== 'rgba(0, 0, 0, 0)';
  524. // Validate: Check for unwrapped text content in DIV
  525. for (const node of el.childNodes) {
  526. if (node.nodeType === Node.TEXT_NODE) {
  527. const text = node.textContent.trim();
  528. if (text) {
  529. errors.push(
  530. `DIV element contains unwrapped text "${text.substring(0, 50)}${text.length > 50 ? '...' : ''}". ` +
  531. 'All text must be wrapped in <p>, <h1>-<h6>, <ul>, or <ol> tags to appear in PowerPoint.'
  532. );
  533. }
  534. }
  535. }
  536. // Check for background images on shapes
  537. const bgImage = computed.backgroundImage;
  538. if (bgImage && bgImage !== 'none') {
  539. errors.push(
  540. 'Background images on DIV elements are not supported. ' +
  541. 'Use solid colors or borders for shapes, or use slide.addImage() in PptxGenJS to layer images.'
  542. );
  543. return;
  544. }
  545. // Check for borders - both uniform and partial
  546. const borderTop = computed.borderTopWidth;
  547. const borderRight = computed.borderRightWidth;
  548. const borderBottom = computed.borderBottomWidth;
  549. const borderLeft = computed.borderLeftWidth;
  550. const borders = [borderTop, borderRight, borderBottom, borderLeft].map(b => parseFloat(b) || 0);
  551. const hasBorder = borders.some(b => b > 0);
  552. const hasUniformBorder = hasBorder && borders.every(b => b === borders[0]);
  553. const borderLines = [];
  554. if (hasBorder && !hasUniformBorder) {
  555. const rect = el.getBoundingClientRect();
  556. const x = pxToInch(rect.left);
  557. const y = pxToInch(rect.top);
  558. const w = pxToInch(rect.width);
  559. const h = pxToInch(rect.height);
  560. // Collect lines to add after shape (inset by half the line width to center on edge)
  561. if (parseFloat(borderTop) > 0) {
  562. const widthPt = pxToPoints(borderTop);
  563. const inset = (widthPt / 72) / 2; // Convert points to inches, then half
  564. borderLines.push({
  565. type: 'line',
  566. x1: x, y1: y + inset, x2: x + w, y2: y + inset,
  567. width: widthPt,
  568. color: rgbToHex(computed.borderTopColor)
  569. });
  570. }
  571. if (parseFloat(borderRight) > 0) {
  572. const widthPt = pxToPoints(borderRight);
  573. const inset = (widthPt / 72) / 2;
  574. borderLines.push({
  575. type: 'line',
  576. x1: x + w - inset, y1: y, x2: x + w - inset, y2: y + h,
  577. width: widthPt,
  578. color: rgbToHex(computed.borderRightColor)
  579. });
  580. }
  581. if (parseFloat(borderBottom) > 0) {
  582. const widthPt = pxToPoints(borderBottom);
  583. const inset = (widthPt / 72) / 2;
  584. borderLines.push({
  585. type: 'line',
  586. x1: x, y1: y + h - inset, x2: x + w, y2: y + h - inset,
  587. width: widthPt,
  588. color: rgbToHex(computed.borderBottomColor)
  589. });
  590. }
  591. if (parseFloat(borderLeft) > 0) {
  592. const widthPt = pxToPoints(borderLeft);
  593. const inset = (widthPt / 72) / 2;
  594. borderLines.push({
  595. type: 'line',
  596. x1: x + inset, y1: y, x2: x + inset, y2: y + h,
  597. width: widthPt,
  598. color: rgbToHex(computed.borderLeftColor)
  599. });
  600. }
  601. }
  602. if (hasBg || hasBorder) {
  603. const rect = el.getBoundingClientRect();
  604. if (rect.width > 0 && rect.height > 0) {
  605. const shadow = parseBoxShadow(computed.boxShadow);
  606. // Only add shape if there's background or uniform border
  607. if (hasBg || hasUniformBorder) {
  608. elements.push({
  609. type: 'shape',
  610. text: '', // Shape only - child text elements render on top
  611. position: {
  612. x: pxToInch(rect.left),
  613. y: pxToInch(rect.top),
  614. w: pxToInch(rect.width),
  615. h: pxToInch(rect.height)
  616. },
  617. shape: {
  618. fill: hasBg ? rgbToHex(computed.backgroundColor) : null,
  619. transparency: hasBg ? extractAlpha(computed.backgroundColor) : null,
  620. line: hasUniformBorder ? {
  621. color: rgbToHex(computed.borderColor),
  622. width: pxToPoints(computed.borderWidth)
  623. } : null,
  624. // Convert border-radius to rectRadius (in inches)
  625. // % values: 50%+ = circle (1), <50% = percentage of min dimension
  626. // pt values: divide by 72 (72pt = 1 inch)
  627. // px values: divide by 96 (96px = 1 inch)
  628. rectRadius: (() => {
  629. const radius = computed.borderRadius;
  630. const radiusValue = parseFloat(radius);
  631. if (radiusValue === 0) return 0;
  632. if (radius.includes('%')) {
  633. if (radiusValue >= 50) return 1;
  634. // Calculate percentage of smaller dimension
  635. const minDim = Math.min(rect.width, rect.height);
  636. return (radiusValue / 100) * pxToInch(minDim);
  637. }
  638. if (radius.includes('pt')) return radiusValue / 72;
  639. return radiusValue / PX_PER_IN;
  640. })(),
  641. shadow: shadow
  642. }
  643. });
  644. }
  645. // Add partial border lines
  646. elements.push(...borderLines);
  647. processed.add(el);
  648. return;
  649. }
  650. }
  651. }
  652. // Extract bullet lists as single text block
  653. if (el.tagName === 'UL' || el.tagName === 'OL') {
  654. const rect = el.getBoundingClientRect();
  655. if (rect.width === 0 || rect.height === 0) return;
  656. const liElements = Array.from(el.querySelectorAll('li'));
  657. const items = [];
  658. const ulComputed = window.getComputedStyle(el);
  659. const ulPaddingLeftPt = pxToPoints(ulComputed.paddingLeft);
  660. // Split: margin-left for bullet position, indent for text position
  661. // margin-left + indent = ul padding-left
  662. const marginLeft = ulPaddingLeftPt * 0.5;
  663. const textIndent = ulPaddingLeftPt * 0.5;
  664. liElements.forEach((li, idx) => {
  665. const isLast = idx === liElements.length - 1;
  666. const runs = parseInlineFormatting(li, { breakLine: false });
  667. // Clean manual bullets from first run
  668. if (runs.length > 0) {
  669. runs[0].text = runs[0].text.replace(/^[•\-\*▪▸]\s*/, '');
  670. runs[0].options.bullet = { indent: textIndent };
  671. }
  672. // Set breakLine on last run
  673. if (runs.length > 0 && !isLast) {
  674. runs[runs.length - 1].options.breakLine = true;
  675. }
  676. items.push(...runs);
  677. });
  678. const computed = window.getComputedStyle(liElements[0] || el);
  679. elements.push({
  680. type: 'list',
  681. items: items,
  682. position: {
  683. x: pxToInch(rect.left),
  684. y: pxToInch(rect.top),
  685. w: pxToInch(rect.width),
  686. h: pxToInch(rect.height)
  687. },
  688. style: {
  689. fontSize: pxToPoints(computed.fontSize),
  690. fontFace: computed.fontFamily.split(',')[0].replace(/['"]/g, '').trim(),
  691. color: rgbToHex(computed.color),
  692. transparency: extractAlpha(computed.color),
  693. align: computed.textAlign === 'start' ? 'left' : computed.textAlign,
  694. lineSpacing: computed.lineHeight && computed.lineHeight !== 'normal' ? pxToPoints(computed.lineHeight) : null,
  695. paraSpaceBefore: 0,
  696. paraSpaceAfter: pxToPoints(computed.marginBottom),
  697. // PptxGenJS margin array is [left, right, bottom, top]
  698. margin: [marginLeft, 0, 0, 0]
  699. }
  700. });
  701. liElements.forEach(li => processed.add(li));
  702. processed.add(el);
  703. return;
  704. }
  705. // Extract text elements (P, H1, H2, etc.)
  706. if (!textTags.includes(el.tagName)) return;
  707. const rect = el.getBoundingClientRect();
  708. const text = el.textContent.trim();
  709. if (rect.width === 0 || rect.height === 0 || !text) return;
  710. // Validate: Check for manual bullet symbols in text elements (not in lists)
  711. if (el.tagName !== 'LI' && /^[•\-\*▪▸○●◆◇■□]\s/.test(text.trimStart())) {
  712. errors.push(
  713. `Text element <${el.tagName.toLowerCase()}> starts with bullet symbol "${text.substring(0, 20)}...". ` +
  714. 'Use <ul> or <ol> lists instead of manual bullet symbols.'
  715. );
  716. return;
  717. }
  718. const computed = window.getComputedStyle(el);
  719. const rotation = getRotation(computed.transform, computed.writingMode);
  720. const { x, y, w, h } = getPositionAndSize(el, rect, rotation);
  721. const baseStyle = {
  722. fontSize: pxToPoints(computed.fontSize),
  723. fontFace: computed.fontFamily.split(',')[0].replace(/['"]/g, '').trim(),
  724. color: rgbToHex(computed.color),
  725. align: computed.textAlign === 'start' ? 'left' : computed.textAlign,
  726. lineSpacing: pxToPoints(computed.lineHeight),
  727. paraSpaceBefore: pxToPoints(computed.marginTop),
  728. paraSpaceAfter: pxToPoints(computed.marginBottom),
  729. // PptxGenJS margin array is [left, right, bottom, top] (not [top, right, bottom, left] as documented)
  730. margin: [
  731. pxToPoints(computed.paddingLeft),
  732. pxToPoints(computed.paddingRight),
  733. pxToPoints(computed.paddingBottom),
  734. pxToPoints(computed.paddingTop)
  735. ]
  736. };
  737. const transparency = extractAlpha(computed.color);
  738. if (transparency !== null) baseStyle.transparency = transparency;
  739. if (rotation !== null) baseStyle.rotate = rotation;
  740. const hasFormatting = el.querySelector('b, i, u, strong, em, span, br');
  741. if (hasFormatting) {
  742. // Text with inline formatting
  743. const transformStr = computed.textTransform;
  744. const runs = parseInlineFormatting(el, {}, [], (str) => applyTextTransform(str, transformStr));
  745. // Adjust lineSpacing based on largest fontSize in runs
  746. const adjustedStyle = { ...baseStyle };
  747. if (adjustedStyle.lineSpacing) {
  748. const maxFontSize = Math.max(
  749. adjustedStyle.fontSize,
  750. ...runs.map(r => r.options?.fontSize || 0)
  751. );
  752. if (maxFontSize > adjustedStyle.fontSize) {
  753. const lineHeightMultiplier = adjustedStyle.lineSpacing / adjustedStyle.fontSize;
  754. adjustedStyle.lineSpacing = maxFontSize * lineHeightMultiplier;
  755. }
  756. }
  757. elements.push({
  758. type: el.tagName.toLowerCase(),
  759. text: runs,
  760. position: { x: pxToInch(x), y: pxToInch(y), w: pxToInch(w), h: pxToInch(h) },
  761. style: adjustedStyle
  762. });
  763. } else {
  764. // Plain text - inherit CSS formatting
  765. const textTransform = computed.textTransform;
  766. const transformedText = applyTextTransform(text, textTransform);
  767. const isBold = computed.fontWeight === 'bold' || parseInt(computed.fontWeight) >= 600;
  768. elements.push({
  769. type: el.tagName.toLowerCase(),
  770. text: transformedText,
  771. position: { x: pxToInch(x), y: pxToInch(y), w: pxToInch(w), h: pxToInch(h) },
  772. style: {
  773. ...baseStyle,
  774. bold: isBold && !shouldSkipBold(computed.fontFamily),
  775. italic: computed.fontStyle === 'italic',
  776. underline: computed.textDecoration.includes('underline')
  777. }
  778. });
  779. }
  780. processed.add(el);
  781. });
  782. return { background, elements, placeholders, errors };
  783. });
  784. }
  785. async function html2pptx(htmlFile, pres, options = {}) {
  786. const {
  787. tmpDir = process.env.TMPDIR || '/tmp',
  788. slide = null
  789. } = options;
  790. try {
  791. // Use Chrome on macOS, default Chromium on Unix
  792. const launchOptions = { env: { TMPDIR: tmpDir } };
  793. if (process.platform === 'darwin') {
  794. launchOptions.channel = 'chrome';
  795. }
  796. const browser = await chromium.launch(launchOptions);
  797. let bodyDimensions;
  798. let slideData;
  799. const filePath = path.isAbsolute(htmlFile) ? htmlFile : path.join(process.cwd(), htmlFile);
  800. const validationErrors = [];
  801. try {
  802. const page = await browser.newPage();
  803. page.on('console', (msg) => {
  804. // Log the message text to your test runner's console
  805. console.log(`Browser console: ${msg.text()}`);
  806. });
  807. await page.goto(`file://${filePath}`);
  808. bodyDimensions = await getBodyDimensions(page);
  809. await page.setViewportSize({
  810. width: Math.round(bodyDimensions.width),
  811. height: Math.round(bodyDimensions.height)
  812. });
  813. slideData = await extractSlideData(page);
  814. } finally {
  815. await browser.close();
  816. }
  817. // Collect all validation errors
  818. if (bodyDimensions.errors && bodyDimensions.errors.length > 0) {
  819. validationErrors.push(...bodyDimensions.errors);
  820. }
  821. const dimensionErrors = validateDimensions(bodyDimensions, pres);
  822. if (dimensionErrors.length > 0) {
  823. validationErrors.push(...dimensionErrors);
  824. }
  825. const textBoxPositionErrors = validateTextBoxPosition(slideData, bodyDimensions);
  826. if (textBoxPositionErrors.length > 0) {
  827. validationErrors.push(...textBoxPositionErrors);
  828. }
  829. if (slideData.errors && slideData.errors.length > 0) {
  830. validationErrors.push(...slideData.errors);
  831. }
  832. // Throw all errors at once if any exist
  833. if (validationErrors.length > 0) {
  834. const errorMessage = validationErrors.length === 1
  835. ? validationErrors[0]
  836. : `Multiple validation errors found:\n${validationErrors.map((e, i) => ` ${i + 1}. ${e}`).join('\n')}`;
  837. throw new Error(errorMessage);
  838. }
  839. const targetSlide = slide || pres.addSlide();
  840. await addBackground(slideData, targetSlide, tmpDir);
  841. addElements(slideData, targetSlide, pres);
  842. return { slide: targetSlide, placeholders: slideData.placeholders };
  843. } catch (error) {
  844. if (!error.message.startsWith(htmlFile)) {
  845. throw new Error(`${htmlFile}: ${error.message}`);
  846. }
  847. throw error;
  848. }
  849. }
  850. module.exports = html2pptx;