An Effect-based wrapper for document conversion using LibreOffice compiled to WebAssembly.
Starting with version 2.x.x, effect-libreoffice executes conversions directly within your Node.js application using @matbee/libreoffice-converter. It requires no local installation of LibreOffice and no external servers, making it highly portable and easy to use.
Install the library along with its peer dependencies:
pnpm add effect-libreoffice effect @effect/platformYou must also install the WebAssembly converter package:
pnpm add @matbee/libreoffice-converterFor a functional approach, use the Conversion module to define conversion pipelines.
import { Conversion, LibreOffice } from "effect-libreoffice";
import { NodeContext } from "@effect/platform-node";
import { Effect, Layer } from "effect";
// Create a conversion pipeline
const program = Conversion.fromFile("input.docx").pipe(
Conversion.toFile("output.pdf", { format: "pdf" }),
);
// Provide the required layers
const MainLayer = Layer.mergeAll(
LibreOffice.layer,
NodeContext.layer
);
program.pipe(
Effect.provide(MainLayer),
Effect.runPromise,
);The Conversion API supports a variety of sources and targets:
- Sources:
fromFile(path),fromBuffer(data),fromStream(stream),fromUrl(url) - Targets:
toFile(path, options),toStream(options),toUrl(url, options)
(Note: fromUrl and toUrl require an HttpClient layer like FetchHttpClient.layer or NodeHttpClient.layerUndici to be provided)
For lower-level access, you can use the LibreOffice service directly. This provides methods like convert, getPageCount, getDocumentInfo, and more.
import { NodeContext } from "@effect/platform-node";
import { Effect, Layer } from "effect";
import { FileSystem } from "@effect/platform";
import { LibreOffice } from "effect-libreoffice";
const program = Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const libre = yield* LibreOffice.LibreOffice;
// Read the input document into a Uint8Array
const inputData = yield* fs.readFile("input.docx");
// Convert it using the LibreOffice engine
const result = yield* libre.convert(inputData, {
inputFormat: "docx",
outputFormat: "pdf",
});
// Write the resulting Uint8Array to disk
yield* fs.writeFile("output.pdf", result.data);
// You can also access other document utilities:
const pageCount = yield* libre.getPageCount(inputData, { inputFormat: "docx" });
console.log(`The document has ${pageCount} pages.`);
});
const MainLayer = Layer.mergeAll(
LibreOffice.layer,
NodeContext.layer
);
program.pipe(Effect.provide(MainLayer), Effect.runPromise);The LibreOffice.LibreOffice service exposes the following WASM-based operations:
convert: Convert documents to a different formatgetPageCount: Get the number of pages/parts in a documentgetDocumentInfo: Get document type and valid output formatsrenderPage: Render a single page as an imagerenderPagePreviews: Render multiple page previewsrenderPageFullQuality: Render a page at full quality nativelygetDocumentText: Extract text content from a documentgetPageNames: Get page or slide names from a documentopenDocument/editorOperation/closeDocument: Session-based document editing