Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ name: Release
# and pushes the `v0.2.0` tag, creates the GitHub release and publishes to npm.
#
# Packages: @p10node/office-engine and the standalone SDK packages in
# packages/* (@p10node/diagram, mermaid, plantuml, graphviz, markdown) are
# packages/* (@p10node/diagram, mermaid, plantuml, graphviz, markdown, convert) are
# released together, all at the branch's version (they share the engine code).
#
# - Release notes: docs/releases/<tag>.md is optional. If present it goes on
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ import { render } from '@p10node/mermaid'
document.getElementById('out')!.innerHTML = render('graph LR\n A --> B').svg
```

## On a server

[`@p10node/convert`](packages/convert) runs the engine in Node or Bun, on worker threads, to convert files without LibreOffice: DOC / XLS / PPT, RTF, OpenDocument, Pages / Keynote / Numbers, Visio, CSV, Markdown and HTML to DOCX / XLSX / PPTX, and documents to text, Markdown, HTML or CSV. A function, a command and an HTTP service with Gotenberg's routes, in a Docker image of Node and the bundle. PDF follows with [#44](https://github.com/p10node/office-engine/issues/44).

```sh
office-convert report.doc # report.docx
curl -F files=@report.doc http://localhost:3000/convert -o report.docx
```

## License

[MIT](LICENSE) © p10node
3 changes: 2 additions & 1 deletion docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ bun run collab # collaboration hub for the playground's "Share live" (port
bun test # core tests on fixtures
bun run test:e2e # browser tests (headless Chromium: bunx playwright install chromium; needs large-deck.pptx, see fixtures.py)
bun run build # dist/: ESM lib + office-engine.embed.js + .d.ts
bun run build:packages # packages/*/dist: the standalone SDK packages (@p10node/diagram, mermaid, plantuml, graphviz, markdown)
bun run build:packages # packages/*/dist: the standalone SDK packages (@p10node/diagram, mermaid, plantuml, graphviz, markdown, convert)
python3 -m venv .venv && .venv/bin/pip install python-docx openpyxl python-pptx Pillow
.venv/bin/python scripts/fixtures.py --large # generate samples + heavy files (--large-deck: only the deck the browser tests need)
bun scripts/iwork-samples.ts # sample.pages / .key / .numbers (built by test/iwork-builder.ts)
Expand Down Expand Up @@ -141,6 +141,7 @@ Released together, at one version (the SDK packages compile in the engine's code
| `@p10node/plantuml` | `packages/plantuml` |
| `@p10node/graphviz` | `packages/graphviz` |
| `@p10node/markdown` | `packages/markdown` |
| `@p10node/convert` | `packages/convert` |

A package whose version is already on npm is skipped; one that fails to publish does not stop the others (the job fails at the end). Re-running the failed job publishes what is missing; so does publishing the tag again by hand once its branch is gone or its run is old: **Actions → Release → Run workflow**, with the tag (`gh workflow run release.yml -f tag=v0.2.0`). Typical case: a package's trusted publisher was fixed after the release ran.

Expand Down
25 changes: 25 additions & 0 deletions packages/convert/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# office-convert as an HTTP service. Built from the repository root:
# docker build -f packages/convert/Dockerfile -t office-convert .
# docker run --rm -p 3000:3000 office-convert
# Settings: PORT, HOST, WORKERS (conversions at once), TIMEOUT (seconds per file), MAX_SIZE (MB per request), MAX_FILES.

FROM oven/bun:1.4 AS build
WORKDIR /src
COPY package.json bun.lock bunfig.toml tsconfig.json LICENSE ./
RUN bun install --frozen-lockfile
COPY src src
COPY packages packages
COPY scripts/build-packages.ts scripts/
RUN bun scripts/build-packages.ts convert

# the package has no dependency: the runtime image is Node and the bundle
FROM node:26-slim
WORKDIR /app
COPY --from=build /src/packages/convert/package.json ./
COPY --from=build /src/packages/convert/dist ./dist
ENV NODE_ENV=production PORT=3000
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=5s CMD ["node", "-e", "fetch(`http://127.0.0.1:${process.env.PORT}/health`).then((r) => process.exit(r.ok ? 0 : 1), () => process.exit(1))"]
ENTRYPOINT ["node", "dist/cli.js"]
CMD ["serve"]
8 changes: 8 additions & 0 deletions packages/convert/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
**/node_modules
**/dist
artifacts
playground
e2e
docs
test
.git
102 changes: 102 additions & 0 deletions packages/convert/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# @p10node/convert

Office files converted on a server, without LibreOffice: Word, Excel and PowerPoint 97-2003, RTF, OpenDocument, Apple Pages / Keynote / Numbers, Visio, CSV, Markdown, HTML, diagrams and source files to **DOCX / XLSX / PPTX**, and any document to **text, Markdown, HTML or CSV**. A function, a command (`office-convert`) and an HTTP service that answers on Gotenberg's routes. Node ≥ 22 or Bun, no dependency, no browser.

Part of [office-engine](https://github.com/p10node/office-engine): the same engine that opens these files in the browser, on worker threads.

**PDF is not written yet.** It needs the layout of [#44](https://github.com/p10node/office-engine/issues/44) (slides first, #46). Until then Gotenberg's `/forms/libreoffice/convert` answers `501`, and this service replaces LibreOffice where the result is an Office file or text, as `soffice --convert-to docx` or unoserver do.

## Install

```sh
npm i -g @p10node/convert # or: bunx @p10node/convert …
```

## Command

```sh
office-convert report.doc # report.docx next to it
office-convert report.doc out/report.docx # the format from the output's extension (an output that exists: -o and --force)
office-convert *.xls *.ods -o converted/ # several at once, into a directory
office-convert sales.xlsx -t csv --sheet Q3 # a sheet as CSV
cat notes.md | office-convert - --name notes.md -t html -o - > notes.html
office-convert serve -p 3000 # the HTTP service
```

| Option | Default |
|-------------------|------------------------------------------------------------|
| `-t, --to` | the Office format the file opens as (.doc → .docx, .csv → .xlsx) |
| `-o, --out` | next to each input; a file (one input, `-` for stdout) or a directory |
| `--sheet` | CSV / TSV: the sheet the file was saved on; an index or a name |
| `--encoding` | text files: a guess (UTF-8, UTF-16, Windows-1252…) |
| `-f, --force` | existing files are not overwritten; an input never is, under any name |
| `-j, --jobs` | files at once: the number of CPUs |
| `--timeout` | 120 s per file (0: no limit) |
| `--max-size` | 100 MB per file |

The exit status is 1 when a file failed (the others are still written), 2 for a wrong command line.

## What converts to what

| Input | Opens as | Can be written as |
|------------------------------------------------------------------------|----------|------------------------------|
| .docx .docm .dotx, .doc, .rtf, .odt .ott .fodt, .pages, .eml .msg*, .md, .txt, .html, .mmd .puml .dot, source files | `docx` | `docx`, `txt`, `md`, `html` |
| .xlsx .xlsm .xltx, .xls, .ods .ots .fods, .numbers, .csv .tsv | `xlsx` | `xlsx`, `csv`, `tsv` |
| .pptx .pptm .potx .ppsx, .ppt, .odp .otp .fodp, .key, .vsdx | `pptx` | `pptx`, `txt` |

\* once e-mail messages are in the engine ([#55](https://github.com/p10node/office-engine/pull/55)).

The format is read from the file's bytes; the name only helps for text formats. An Office file keeps its variant (a `.docm` stays `.docm`). CSV output has the values as shown (formats applied), formulas computed when the file has no value for them, and a byte order mark so Excel reads it as UTF-8.

## HTTP service

```sh
docker build -f packages/convert/Dockerfile -t office-convert . # from the repository root
docker run --rm -p 3000:3000 office-convert
```

```sh
curl -F files=@report.doc http://localhost:3000/convert -o report.docx
curl -F files=@a.odt -F files=@b.xls http://localhost:3000/convert -o converted.zip
curl -F files=@sales.xlsx -F to=csv -F sheet=Q3 http://localhost:3000/convert -o q3.csv
curl --data-binary @notes.md 'http://localhost:3000/convert?to=html&name=notes.md' -o notes.html
```

| Route | |
|------------------------------------|----------------------------------------------------------------------------------|
| `POST /convert` | multipart: the files (any field name), `to`, `sheet`, `encoding`, `language`; or one file as the body, `to` / `name` in the query |
| `POST /forms/libreoffice/convert` | Gotenberg's route, to PDF: `501` until PDF is written |
| `GET /health` | `{ "status": "up", "details": … }`, as Gotenberg answers |
| `GET /version` | the version, as text |

One file gives the converted file, several a ZIP. Gotenberg's headers are read: `Gotenberg-Output-Filename` names the result, `Gotenberg-Trace` comes back (a new id when there is none). What a file lost on the way (more columns than a sheet holds…) is in `Office-Convert-Warning` headers, percent-encoded.

| Status | When |
|--------|----------------------------------------------------------------------|
| 400 | no file, too many files, a format the file cannot be written as |
| 405 | not a POST |
| 413 | the request is larger than `MAX_SIZE`, or the converted file would be larger than 1 GB |
| 422 | the file cannot be read: damaged, encrypted, not a format the engine knows |
| 501 | PDF |
| 503 | the file took longer than `TIMEOUT`, or every thread is busy and the queue is full (`Retry-After`) |

Settings, as flags of `office-convert serve` or environment variables: `PORT` (3000), `HOST` (0.0.0.0), `WORKERS` (the number of CPUs), `TIMEOUT` (120 s), `MAX_SIZE` (100 MB per request), `MAX_FILES` (20 per request). Each conversion runs on a worker thread: one past its time, or whose client hung up, ends with its thread, and when one file of a request fails the others are given up. At most 100 files, and 1 GB of them, wait for a thread, and 1 GB of request bodies are held at once (`maxPending` of `createHandler`); more is answered `503`. A gzip-compressed file unpacks to 1 GB at most. The engine's own limits for crafted files (ZIP entries, unpacked sizes, cells, chart points) apply as in the browser.

## API

```ts
import { convert, Converter, createHandler, listen } from '@p10node/convert'

// on this thread
const { data, name, type } = await convert(bytes, { name: 'report.doc', to: 'docx' })

// on a pool of worker threads, with a time limit per file
const converter = new Converter({ workers: 4, timeout: 60_000 })
const csv = await converter.convert(bytes, { name: 'sales.xlsx', to: 'csv', sheet: 'Q3', signal })

// the HTTP routes as a fetch handler: Bun.serve({ fetch }), Hono, Deno, a Worker…
const handler = createHandler(converter, { maxBytes: 50 * 1024 * 1024 })
await listen(handler, { port: 3000 }) // Bun.serve under Bun, node:http under Node
```

`convert` resolves with `{ data: Uint8Array, name, type, ext, kind, from?, warnings }` and rejects with a `ConvertError` (`code`: `unsupported`, `not-implemented`, `too-large`) for a request it cannot do, an `Error` for a file it cannot read. Its limits: `maxOutput` (the file written, 1 GB) and `maxUnpacked` (a gzip-compressed input, 1 GB). `Converter` takes `workers`, `timeout`, `queue` (100), `bytes` (1 GB of input running and waiting) and `memory` (MB of heap per thread, Node only), copies the bytes it is given, and adds `TimeoutError`, `BusyError` and `ThreadError` (the thread ran out of memory or crashed).
49 changes: 49 additions & 0 deletions packages/convert/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@p10node/convert",
"version": "0.1.0",
"description": "Office files converted on a server without LibreOffice: DOC, XLS, PPT, RTF, OpenDocument, Pages, Keynote, Numbers, Visio, CSV, Markdown, HTML to DOCX / XLSX / PPTX, and documents to text, Markdown, HTML or CSV. A function, a CLI and an HTTP service with Gotenberg's routes; no dependency",
"type": "module",
"license": "MIT",
"files": [
"dist"
],
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"bin": {
"office-convert": "./dist/cli.js"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
"engines": {
"node": ">=22",
"bun": ">=1.3"
},
"sideEffects": false,
"keywords": [
"docx",
"xlsx",
"pptx",
"doc",
"xls",
"ppt",
"odt",
"convert",
"gotenberg",
"libreoffice",
"server"
],
"homepage": "https://github.com/p10node/office-engine/tree/main/packages/convert#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/p10node/office-engine.git",
"directory": "packages/convert"
},
"publishConfig": {
"access": "public"
}
}
Loading