Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

libreoffice-file-converter

Girilloid1.8kMIT3.2.0TypeScript support: included

Simple NodeJS wrapper for libreoffice CLI for converting office documents to different formats

converter, file converter, LibreOffice

readme

libreoffice-file-converter

Simple NodeJS wrapper for libreoffice CLI for converting office documents to different formats.

Dependency

Please install libreoffice in /Applications (Mac), with your favorite package manager (Linux), or with the msi (Windows).

How to use

Constructor

LibreOfficeFileConverter constructor accepts optional configuration object.

options.binaryPaths

Array of paths to LibreOffice binary executables.

options.childProcessOptions

child_process.ExecFileOptions object. Can be used to configure such things as timeout, etc.

options.debug

Enables debug output for LibreOffice command execution.

options.tmpOptions

tmp.DirOptions object. Can be used to configure behavior of tmp package, which is used to create temporary folders for LibreOffice data.

LibreOfficeFileConverter.convert

Converts provided file, Buffer or Readable stream to the request format.

input

Input type: buffer | file | stream.

Defines corresponding field for input: buffer | inputPath | stream.

output

Output type: buffer | file | stream.

Requires outputPath field to be present when set to file.

Defines return type: Promise<Buffer> | Promise<void> | Promise<Readable>.

format

Conversion format.

inputFilter

LibreOffice input filter, see docs.

outputFilter

LibreOffice output filter, see docs.

filter

LibreOffice output filter, see docs.

Deprecated, use outputFilter instead.

options

Overrides for LibreOfficeFileConverter instance options.

Examples

From Buffer to Buffer.

import { readFile } from 'fs/promises';
import { join } from 'path';
import { LibreOfficeFileConverter } from 'libreoffice-file-converter';

const inputPath = join(__dirname, './resources/example.doc');

const run = async () => {
  const libreOfficeFileConverter = new LibreOfficeFileConverter({
    childProcessOptions: {
      timeout: 60 * 1000,
    },
  });

  const inputBuffer = await readFile(inputPath);

  const outputBuffer = await libreOfficeFileConverter.convert({
    buffer: inputBuffer,
    format: 'pdf',
    input: 'buffer',
    output: 'buffer'
  })
};

run();

From Buffer to file.

import { readFile } from 'fs/promises';
import { join } from 'path';
import { LibreOfficeFileConverter } from 'libreoffice-file-converter';

const inputPath = join(__dirname, './resources/example.doc');
const outputPath = join(__dirname, './resources/output/result.pdf');

const run = async () => {
  const libreOfficeFileConverter = new LibreOfficeFileConverter({
    childProcessOptions: {
      timeout: 60 * 1000,
    },
  });

  const inputBuffer = await readFile(inputPath);

  await libreOfficeFileConverter.convert({
    buffer: inputBuffer,
    format: 'pdf',
    input: 'buffer',
    output: 'fille',
    outputPath,
  })
};

run();

From Buffer to Readable stream.

import { readFile } from 'fs/promises';
import { join } from 'path';
import { LibreOfficeFileConverter } from 'libreoffice-file-converter';

const inputPath = join(__dirname, './resources/example.doc');

const run = async () => {
  const libreOfficeFileConverter = new LibreOfficeFileConverter({
    childProcessOptions: {
      timeout: 60 * 1000,
    },
  });

  const inputBuffer = await readFile(inputPath);

  const outputStream = await libreOfficeFileConverter.convert({
    buffer: inputBuffer,
    format: 'pdf',
    input: 'buffer',
    output: 'stream',
  })
};

run();

From file to Buffer.

import { readFile } from 'fs/promises';
import { join } from 'path';
import { LibreOfficeFileConverter } from 'libreoffice-file-converter';

const inputPath = join(__dirname, './resources/example.doc');

const run = async () => {
  const libreOfficeFileConverter = new LibreOfficeFileConverter({
    childProcessOptions: {
      timeout: 60 * 1000,
    },
  });

  const outputBuffer = await libreOfficeFileConverter.convert({
    format: 'pdf',
    input: 'file',
    inputPath,
    output: 'buffer',
  })
};

run();

From file to file.

import { readFile } from 'fs/promises';
import { join } from 'path';
import { LibreOfficeFileConverter } from 'libreoffice-file-converter';

const inputPath = join(__dirname, './resources/example.doc');
const outputPath = join(__dirname, './resources/output/result.pdf');

const run = async () => {
  const libreOfficeFileConverter = new LibreOfficeFileConverter({
    childProcessOptions: {
      timeout: 60 * 1000,
    },
  });

  await libreOfficeFileConverter.convert({
    format: 'pdf',
    input: 'file',
    inputPath,
    output: 'file',
    outputPath,
  })
};

run();

From file to Readable stream.

import { readFile } from 'fs/promises';
import { join } from 'path';
import { LibreOfficeFileConverter } from 'libreoffice-file-converter';

const inputPath = join(__dirname, './resources/example.doc');

const run = async () => {
  const libreOfficeFileConverter = new LibreOfficeFileConverter({
    childProcessOptions: {
      timeout: 60 * 1000,
    },
  });

  const outputStream = await libreOfficeFileConverter.convert({
    format: 'pdf',
    input: 'file',
    inputPath,
    output: 'stream',
  })
};

run();

From Readable stream to Buffer.

import { createReadStream } from 'fs';
import { join } from 'path';
import { LibreOfficeFileConverter } from 'libreoffice-file-converter';

const inputPath = join(__dirname, './resources/example.doc');

const run = async () => {
  const libreOfficeFileConverter = new LibreOfficeFileConverter({
    childProcessOptions: {
      timeout: 60 * 1000,
    },
  });

  const inputStream = createReadStream(inputPath);

  const outputBuffer = await libreOfficeFileConverter.convert({
    format: 'pdf',
    input: 'stream',
    output: 'buffer',
    stream: inputStream,
  })
};

run();

From Readable stream to file.

import { createReadStream } from 'fs';
import { join } from 'path';
import { LibreOfficeFileConverter } from 'libreoffice-file-converter';

const inputPath = join(__dirname, './resources/example.doc');
const outputPath = join(__dirname, './resources/output/result.pdf');

const run = async () => {
  const libreOfficeFileConverter = new LibreOfficeFileConverter({
    childProcessOptions: {
      timeout: 60 * 1000,
    },
  });

  const inputStream = createReadStream(inputPath);

  await libreOfficeFileConverter.convert({
    format: 'pdf',
    input: 'stream',
    output: 'file',
    outputPath,
    stream: inputStream,
  })
};

run();

From Readable stream to Readable stream.

import { createReadStream } from 'fs';
import { join } from 'path';
import { LibreOfficeFileConverter } from 'libreoffice-file-converter';

const inputPath = join(__dirname, './resources/example.doc');

const run = async () => {
  const libreOfficeFileConverter = new LibreOfficeFileConverter({
    childProcessOptions: {
      timeout: 60 * 1000,
    },
  });

  const inputStream = createReadStream(inputPath);

  const outputStream = await libreOfficeFileConverter.convert({
    format: 'pdf',
    input: 'stream',
    output: 'stream',
    stream: inputStream,
  })
};

run();

changelog

Changelog

3.2.0 (2025-01-27)

Features

  • Update type comments and cleanup (5a7a9db)

3.1.1 (2024-09-14)

Bug Fixes

  • #9 Remove double quotes from args (6786bb7)

3.1.0 (2024-09-13)

Features

3.0.2 (2024-08-30)

Bug Fixes

  • #7 Fix incorrect resolving of processed file path when input path doesn't have an extension (0ed7c0a)

3.0.1 (2024-08-22)

3.0.0 (2024-08-22)

⚠ BREAKING CHANGES

  • convertBuffer, convertFile and convertStream are replaced with a single convert method

Features

  • Advanced conversion configuration (e1f79c9)

Bug Fixes

  • Get rid of structureClone for better compatibility with node < 17 (b3a0ae5)
  • Preserve dots in file name (c20e94b)
  • Use platform specific separator (8dc3cd7)
  • Use platform specific separator for path creation (f5bb4fe)

2.3.4 (2024-08-08)

Bug Fixes

  • Preserve jsdoc comments in d.ts (1383f40)

2.3.3 (2024-06-22)

Bug Fixes

  • Await cleanup onnly on exception (9da4857)

2.3.2 (2024-06-16)

Bug Fixes

  • Better cleanup on error for stream operations (27682aa)

2.3.1 (2024-06-16)

Bug Fixes

  • Cleanup temp dirs on error (258cf08)
  • LibreOffice configuration read error is recognized as a convert error (bb87baf)

2.3.0 (2024-06-10)

Features

  • Additional soffice paths for linux (fcdb467)

Bug Fixes

  • Improved arguments passing (30c205a)

2.2.0 (2024-05-27)

Features

  • Improve deno compatibility (5715194)

2.1.1 (2024-05-21)

Bug Fixes

2.1.0 (2024-01-13)

Features

  • Throw libreoffice error when it fails to convert file (a255ff0)

2.0.0 (2024-01-13)

Features

  • Drop support of NodeJS versions < 14 (a49dfe3)
  • Remove deprecated method (00b6194)
  • Replace tmp with tmp-promise (d2fadbe)

1.2.1 (2023-08-31)

1.2.0 (2023-08-03)

Features

1.1.1 (2023-06-27)

1.1.0 (2023-06-26)

1.0.4 (2023-05-17)

Bug Fixes

  • Return dts, add commonjs/esm exports (42a20a3)

1.0.3 (2023-05-16)