### Install Ream Source: https://github.com/alex-krassavin/reamkit/blob/main/README.md Install the reamkit package using npm. ```sh npm install reamkit ``` -------------------------------- ### Initialize Stand State and UI Logic Source: https://github.com/alex-krassavin/reamkit/blob/main/stand/index.html Defines the initial application state, including layer configurations, sorting preferences, and helper functions for formatting percentages, timestamps, and HTML escaping. ```javascript const LAYERS = [ { key: 'source', label: 'Source PDF', cls: 'source' }, { key: 'gold', label: 'Gold · LibreOffice reading', cls: 'gold' }, { key: 'ours', label: 'Ours · Ream .docx', cls: 'ours' }, { key: 'diff', label: 'Diff · red gold only · blue ours only', cls: 'diff' }, ]; const remembered = (() => { try { return localStorage.getItem('stand-sort'); } catch { return null; } })(); const view = { name: decodeURIComponent(location.hash.slice(1)) || null, mode: 'grid', layer: 'ours', shown: '', sort: remembered === 'name' ? 'name' : 'worst', }; let last = null; const pct = (x) => (x * 100).toFixed(1) + '%'; const ago = (t) => { const s = Math.round((Date.now() - t) / 1000); return s < 60 ? s + 's ago' : s < 3600 ? Math.round(s / 60) + 'm ago' : Math.round(s / 3600) + 'h ago'; }; const esc = (s) => String(s).replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c]); const worst = (r) => r.scores.reduce((m, s) => Math.max(m, s.missing, s.extra), 0); ``` -------------------------------- ### Configure image handling in Markdown output Source: https://github.com/alex-krassavin/reamkit/blob/main/docs/src/content/docs/guides/examples.md Demonstrates how to handle images by either linking them as files or dropping them entirely during conversion. ```ts const md = await doc.convert('md', { images: 'link' }); // ![](./media/image1.png) const bare = await doc.convert('md', { images: 'drop' }); // no pictures at all ``` -------------------------------- ### Browser file input to PDF preview Source: https://github.com/alex-krassavin/reamkit/blob/main/docs/src/content/docs/guides/examples.md Converts a file selected via an HTML input element to a PDF and opens it in a new browser tab. ```ts import { Ream } from 'reamkit'; input.addEventListener('change', async () => { const bytes = new Uint8Array(await input.files![0].arrayBuffer()); const pdf = await Ream.parse(bytes).convert('pdf'); window.open(URL.createObjectURL(new Blob([pdf], { type: 'application/pdf' }))); }); ``` -------------------------------- ### Convert document to PDF with report Source: https://github.com/alex-krassavin/reamkit/blob/main/docs/src/content/docs/guides/examples.md Demonstrates the basic conversion process using Ream.parse and convertWithReport, which returns both the PDF bytes and a report of any conversion losses. ```ts import { Ream } from 'reamkit'; const { bytes, losses } = await Ream.parse(docxBytes).convertWithReport('pdf', { fonts }); // `losses` is where anything the file asks for and Ream cannot draw is reported. ``` -------------------------------- ### Ream.parse for legacy binary formats and convert to PDF/XLSX Source: https://github.com/alex-krassavin/reamkit/blob/main/docs/src/content/docs/guides/examples.md Shows how to parse legacy .doc, .xls, and .ppt files using the same Ream.parse entry point, and convert them to PDF or modern XLSX. ```APIDOC ## Ream.parse(bytes) ### Description Parses a binary Office file (.doc, .xls, .ppt) from bytes. The parser sniffs OLE2/CFB and selects the appropriate reader. ### Method parse ### Parameters - **bytes** (Uint8Array) - Required - The binary file content. ### Returns - **Ream** - The parsed document instance. ## Ream.convert('pdf') ### Description Converts the parsed legacy document to PDF. ### Method convert ### Parameters - **target** (string) - Required - The target format, 'pdf'. ### Returns - **Promise** - The PDF file content. ## Ream.convert('xlsx') ### Description Converts a parsed legacy workbook (.xls) to a modern XLSX workbook. ### Method convert ### Parameters - **target** (string) - Required - The target format, 'xlsx'. ### Returns - **Promise** - The XLSX file content. ``` -------------------------------- ### Parse legacy binary Office formats Source: https://github.com/alex-krassavin/reamkit/blob/main/docs/src/content/docs/guides/examples.md Shows that legacy .doc, .xls, and .ppt files are handled by the same Ream.parse entry point as modern formats. The conversion methods work identically for these legacy files. ```ts const doc = Ream.parse(bytes); // .doc / .xls / .ppt — sniffed like the rest const pdf = await doc.convert('pdf'); const xlsx = await doc.convert('xlsx'); // a .xls re-writes as a modern workbook ``` -------------------------------- ### Process review comments with Reamkit Source: https://github.com/alex-krassavin/reamkit/blob/main/docs/src/content/docs/guides/examples.md Demonstrates how to parse a document and convert it to various formats while handling review comments. Includes options for native sticky-note annotations in PDF and nested reply threads in HTML. ```ts import { Ream } from 'reamkit'; const doc = Ream.parse(docxBytes); const pdf = await doc.convert('pdf', { fonts }); // markers + Comments section + highlights const sticky = await doc.convert('pdf', { fonts, commentAnnotations: true }); // …plus /Text pop-ups const html = await doc.convert('html'); // replies nested, resolved threads flagged doc.flow.comments?.get('0'); // { content, author?, date?, authorId?, parentId?, done? } ``` -------------------------------- ### Normalize and edit DOCX files with convert('docx') Source: https://github.com/alex-krassavin/reamkit/blob/main/docs/src/content/docs/guides/examples.md Shows how to round-trip a DOCX file to normalize, sanitize, or programmatically edit it. Note that the output is a fresh, valid DOCX file and the process is semantic rather than byte-exact. ```ts import { Ream } from 'reamkit'; const doc = Ream.parse(bytes); // a .docx (xlsx has no docx writer) const out = await doc.convert('docx'); // `out` is a fresh, valid .docx — hand it to a download, an upload, or re-parse it. ``` -------------------------------- ### Convert presentation decks to Markdown with page breaks Source: https://github.com/alex-krassavin/reamkit/blob/main/docs/src/content/docs/guides/examples.md Converts a PPTX deck to Markdown, using horizontal rules to represent slide breaks. ```ts const deck = Ream.parse(pptxBytes); const md = await deck.convert('md', { pageBreaks: 'rule' }); // --- between slides ``` -------------------------------- ### Render Excel pivot tables Source: https://github.com/alex-krassavin/reamkit/blob/main/docs/src/content/docs/guides/examples.md Demonstrates parsing an Excel file and converting it to PDF and HTML while preserving pivot table styles and formatting. ```ts import { Ream } from 'reamkit'; const doc = Ream.parse(xlsxBytes); const pdf = await doc.convert('pdf', { fonts }); const html = await doc.convert('html'); ``` -------------------------------- ### Sheet Stand Frontend Logic Source: https://github.com/alex-krassavin/reamkit/blob/main/stand/html.html This script handles the UI state, polls the /api/state endpoint for build updates, and renders the document list and main view. ```javascript // The stand says what its columns are and which files to open beside them. let LAYERS = []; let LINKS = []; const remembered = (() => { try { return localStorage.getItem('stand-html-sort'); } catch { return null; } })(); const view = { name: decodeURIComponent(location.hash.slice(1)) || null, mode: 'grid', layer: 'ours', shown: '', sort: remembered === 'name' ? 'name' : 'worst', }; let last = null; const pct = (x) => (x * 100).toFixed(1) + '%'; const ago = (t) => { const s = Math.round((Date.now() - t) / 1000); return s < 60 ? s + 's ago' : s < 3600 ? Math.round(s / 60) + 'm ago' : Math.round(s / 3600) + 'h ago'; }; const esc = (s) => String(s).replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c]); const worst = (r) => r.scores.reduce((m, s) => Math.max(m, s.missing, s.extra), 0); const dims = (s) => (s ? `${s.width}×${s.height}` : '—'); async function poll() { try { const res = await fetch('/api/state', { cache: 'no-store' }); last = await res.json(); const bench = last.bench || {}; LAYERS = (bench.layers || []).map((l) => ({ ...l, cls: l.key })); LINKS = bench.links || []; if (bench.title) { document.title = bench.title; document.getElementById('title').textContent = bench.title; } render(); } catch { /* the server restarts; try again */ } setTimeout(poll, 1500); } function render() { if (!last) return; const score = (d) => (d.report ? worst(d.report) + (d.report.errors.length ? 2 : 0) : -1); const docs = [...last.docs].sort((a, b) => view.sort === 'name' ? a.name.localeCompare(b.name) : score(b) - score(a) || a.name.localeCompare(b.name), ); last.ordered = docs.map((d) => d.name); if (!view.name && docs.length) view.name = docs[0].name; for (const b of document.querySelectorAll('[data-sort]')) { b.className = b.dataset.sort === view.sort ? 'on' : ''; b.onclick = () => { view.sort = b.dataset.sort; try { localStorage.setItem('stand-html-sort', view.sort); } catch { /* a private window keeps nothing */ } render(); }; } const built = last.docs.filter((d) => d.report).length; document.getElementById('count').textContent = `${built}/${last.docs.length} built`; const list = docs .map((d) => { const r = d.report; const busy = last.building === d.name ? ' building…' : last.queue.includes(d.name) ? ' queued' : ''; let meta = 'not built'; if (r) { const sheets = r.perSheet ? `${r.sheets.length} sheet${r.sheets.length === 1 ? '' : 's'}` : `${r.sourcePages.count} sheet${r.sourcePages.count === 1 ? '' : 's'} · whole`; const errs = r.errors.length ? ' error' : ''; meta = `${sheets} · worst ${pct(worst(r))} · ${ago(r.builtAt)}${errs}`; } return `
${esc(d.name)}${busy}
${meta}
`; }) .join(''); document.getElementById('docs').innerHTML = list || '
No workbooks.
npm run stand:html:build -- book.xlsx
'; for (const el of document.querySelectorAll('.doc')) el.onclick = () => select(el.dataset.name); const doc = last.docs.find((d) => d.name === view.name); const key = JSON.stringify([view.name, view.mode, view.layer, doc && doc.report && doc.report.builtAt, last.building, last.building ? last.log.length : 0]); if (key === view.shown) return; view.shown = key; drawMain(doc); } function drawMain(doc) { const main = document.getElementById('main'); if (!doc) { main.innerHTML = '
Put a workbook on the stand: npm run stand:html:build -- book.xlsx
'; return; } const r = doc.report; const v = r ? r.builtAt : 0; const base = `/out/${encodeURIComponent(doc.name)}/`; const modeBtn = (m, label) => ``; const layerBtns = view.mode === 'flip' ? LAYERS.map((l, i) => ``).join('') : ''; const links = r ? LINKS.map((l) => `${esc(l.label)}`).join('') : ''; let html = `
${esc(doc.name)} ${modeBtn('grid', 'Side by side')}${modeBtn('flip', 'Flip')}${layerBtns} ${links} red gold has it, ours not · blue ours has it, gold not ↑↓ file · F flip (1:1) / side by side · 1–${LAYERS.length} layer · Space gold ⇄ ours · R rebuild
`; if (last.building === doc.name) html += ``; if (r && r.errors.length) html += `