Skip to content

fiws/effect-libreoffice

Repository files navigation

effect-libreoffice

NPM Version GitHub License Effect: yes

An Effect-based wrapper for document conversion using LibreOffice compiled to WebAssembly.

Overview

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.

Installation

Install the library along with its peer dependencies:

pnpm add effect-libreoffice effect @effect/platform

You must also install the WebAssembly converter package:

pnpm add @matbee/libreoffice-converter

Usage

Functional API (Pipeable)

For 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)

Direct Service Usage

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);

Available Service Methods

The LibreOffice.LibreOffice service exposes the following WASM-based operations:

  • convert: Convert documents to a different format
  • getPageCount: Get the number of pages/parts in a document
  • getDocumentInfo: Get document type and valid output formats
  • renderPage: Render a single page as an image
  • renderPagePreviews: Render multiple page previews
  • renderPageFullQuality: Render a page at full quality natively
  • getDocumentText: Extract text content from a document
  • getPageNames: Get page or slide names from a document
  • openDocument / editorOperation / closeDocument: Session-based document editing

About

Convert anything libreoffice supports using Effect TS

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors