Dev.to WebDev ๐Ÿ›  Dev ๐Ÿ‘ 0 ๐Ÿ“– 4 min read

Reading and writing DOCX, PPTX and XLSX in the browser with Rust and WASM

I built Scissor Office, an office suite for documents, slides and spreadsheets that runs in a browser tab: https://office.scissor.studio/ It opens and saves DOCX, ODT, PPTX, ODP, XLSX and ODS without sending the file to

Reading and writing DOCX, PPTX and XLSX in the browser with Rust and WASM

I built Scissor Office, an office suite for documents, slides and spreadsheets that runs in a browser tab: https://office.scissor.studio/
It opens and saves DOCX, ODT, PPTX, ODP, XLSX and ODS without sending the file to a server. This post is about how that works and where it stops.

The Scissor Office document editor

Three Rust engines, compiled to WebAssembly

Each kind of file has its own engine, written in Rust and compiled with wasm-bindgen. These are the sizes the live site serves today:

Engine Raw Brotli
Documents 2.7 MB 1.05 MB
Presentations 1.96 MB 0.74 MB
Spreadsheets 3.08 MB 1.06 MB
Charts 0.31 MB 0.13 MB

A service worker caches them, so the app works offline after the first visit. The spreadsheet engine runs in a worker, so a big recalculation doesn't block typing.

A DOCX is a zip of XML

DOCX, PPTX, XLSX and the OpenDocument formats are all zip files full of XML parts. I wrote a small zip reader and writer on top of two crates: miniz_oxide for deflate and crc32fast for checksums. It supports just what office files need. ODF requires its mimetype entry to be first and stored uncompressed, so the writer stores it first. There is no zip64 and no encryption.

The XML is read with quick-xml. The readers and writers follow the published standards, Office Open XML (ISO/IEC 29500) and OpenDocument (ISO/IEC 26300).

The old binary formats are different. A .doc file is a compound file, so the engine reads that container first and then Word's FIB, piece table and formatting pages, following Microsoft's published MS-DOC specification. For .doc it reads only the main text with its formatting, pictures and links. Headers, footnotes and comments are not read from .doc files yet.

The same engine behind every converter

The site also has 14 small converters (DOCX to ODT, XLSX to CSV, PPT to PPTX and so on) and 5 tools, such as accept all tracked changes or extract images. They don't have their own conversion code. A converter page starts a worker that loads the same engine the app uses and calls its import and export functions:

async doc({ bytes, name, to }) {
  const m = await docEngine();
  const json = m.importDoc(new Uint8Array(bytes), name);
  const out = m.exportDoc(json, to);
  return { bytes: out, losses: docLosses(JSON.parse(json), to) };
},

importDoc turns any supported file into one document model as JSON, and exportDoc writes that model in the target format. losses is the list of things the target format can't hold. The page shows that list instead of dropping them without a word.

Keeping what the editor can't edit yet

The slide and spreadsheet editors don't edit everything a real file can hold. In PPTX, tables, charts, SmartArt and animations are kept when you save back to PPTX, but you can't edit them yet. In XLSX, drawings, comments, merged cells, conditional formats, data validation and pivot tables work the same way.

The presentations engine does this with a keep layer: parts the editor doesn't understand are carried through and written back next to the parts you changed.

Spreadsheet formulas are recalculated in the tab: over 300 functions, using calculation code vendored from IronCalc 0.9.1 (MIT OR Apache-2.0). To check it, the sheet engine runs IronCalc's 222 test workbooks against Excel's values.

How files stay on the device

Drafts are saved in the browser's IndexedDB for this site, on this device. They are not synced anywhere. In Chrome and Edge, Save writes back into the same file on disk. Other browsers download a copy.

The Content Security Policy starts with default-src 'self', and connect-src allows only this site and Google's sign-in and Drive APIs, for the optional Drive buttons. So even a bug can't upload a document anywhere else. You can check this in the Network tab: nothing is sent when you type, open or save.

PDF files are read with PDF.js 6.4.299 (Apache 2.0) in the tab, and the document engine rebuilds the text, headings, lists and pictures into an editable document. The other libraries are MIT or similarly permissive: quick-xml, miniz_oxide, pulldown-cmark for Markdown, rustybuzz for text shaping. The full list with versions is at /licenses/ on the site.

One bug from this week: PDF.js 6 reads text with for await over a ReadableStream. Safari before version 26 doesn't support that, so opening a PDF failed on iPhones. I now add the async iterator where it's missing, both on the page and in the PDF.js worker, before PDF.js loads.

Limits

  • Formatting fidelity is not Word's. Text, headings, lists, tables, pictures, footnotes, headers and footers, comments and tracked changes are kept in DOCX and ODT. Charts, text boxes and drawn shapes inside documents are not read yet.
  • PDF output goes through the browser's print dialog. There is no PDF writer of my own yet.
  • Password-protected files can't be opened yet.
  • Macros are not kept. An .xlsm file is saved as a copy without them.
  • PDF import works best on PDFs that contain real text. Scanned pages are just pictures.

Try it: https://office.scissor.studio/

It's free and needs no account. Here is what would help me most: open a real DOCX, PPTX or XLSX from your work, and tell me the first thing that comes out wrong.

๐Ÿ“ฐ Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.