1.0.5 • Published 2 years ago

node-unoconv v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

node-unoconv

Lightweight NodeJS wrapper for unoconv, a Universal Office Converter.

npm npm bundle size npm

📦 Installation

⚙️ Required dependencies

The node-unoconv package requires unoconv and its dependencies to be installed.

Installing LibreOffice or OpenOffice software

Office software is required to run unoconv. LibreOffice is recommended but OpenOffice is still supported.

⚙️ Installing node-unoconv package

After you install required dependencies, the node-unoconv package can be installed using commands below:

npm install node-unoconv

# or using yarn

yarn add node-unoconv

📃 Usage

There are few ways to import node-unoconv package to your NodeJS project:

ES6

// ES6 import
import unoconv from 'node-unoconv';

unoconv.convert(input, options?); // Converting a file
unoconv.listen(options?); // Starting a conversion listener
// Using named exports
import { convert, listen } from 'node-unoconv';

convert(input, options?); // Converting a file
listen(options?); // Starting a conversion listener

Require

// Using Require
const unoconv = require('node-unoconv');

unoconv.convert(input, options?); // Converting a file
unoconv.listen(options?); // Starting a conversion listener
// Alternative way to require convert or listen methods
const { convert, listen } = require('node-unoconv');

convert(input, options?); // Converting a file
listen(options?); // Starting a conversion listener

Converting files

Converts an input file to a format specified in options object (defaults to pdf). By default, a promise is returned that resolves with a buffer or rejects with an error message. A callback function is also supported.

// Convert a file using a promise
unoconv.convert('file.doc')
  .then((buffer) => {
    console.log(buffer.length);
  }.catch((err) => {
    console.error(err);
  });
  
// or with async/await

const buffer = await unoconv.convert('file.doc');

Conversion with an output path and a callback

const callback = (path, error) => {
  if (error) {
    console.error('Error', error);
    return;
  }
  
  console.log('Path:', path);
};

const options = {
  callback,
  output: 'file.pdf'
};
unoconv.convert(path);

Starting a listener

Starts a standalone LibreOffice or OpenOffice listener.
If server or port options are not specified, it uses defaults values 127.0.0.1 and 2002.

unoconv.listen();

Starting a listener on a different address and port

const options = {
  port: 2003,
  server: '127.0.0.1'
};

unoconv.listen(options);

🛠 Options

OptionTypeDefaultDescription
connectionstringUNO connection string to be used by the client to connect to an LibreOffice instance, or used by the listener to make LibreOffice listen.
outputstringfile.pdfOutput basename, filename or directory.
doctypestringdocumentSpecify the LibreOffice document type of the backend format. Possible document types are: document, graphics, presentation, spreadsheet.
passwordstringProvide a password to decrypt the document.
serverstring127.0.0.1Server (address) to listen on (as listener) or to connect to (as client).
portnumber2002Port to listen on (as listener) or to connect to (as client).
pipeUse a pipe as an alternative connection mechanism to talk to LibreOffice.
exportstring[]Set specific export filter options (related to the used LibreOffice filter).
fieldstring[]Replace user-defined text field with value.
formatstringpdfSpecify the output format for the document. You can get a list of possible output formats per document type by using the --show option. Default document type is 'pdf'.
importstringutf-8Set specific import filters options (related to the used LibreOffice import filter based on the input filename).
inputFilterNamestringxmlSet input filter name, useful when converting stdin or files without an extension.
listenerboolfalseStart unoconv as listener for unoconv clients to connect to. It's recommended to start the listener with listen() method.
disableHtmlUpdateLinksboolfalseDisables the recheck for updating links missed by libreoffice
noLaunchboolfalseBy default if no listener is running, unoconv will launch its own (temporary) listener to make sure the conversion works. This option will abort the conversion if no listener is found, rather than starting our own listener.
preserveboolfalseKeep timestamp and permissions of the original document.
printerstring[]Printer options - PaperFormat: specify printer paper format, eg. PaperFormat=A3- PaperOrientation: specify printer paper orientation, eg. PaperOrientation=landscape- PapserSize: specify printer paper size, paper format should set to USER, size=widthxheight, e.g. eg. PaperSize=130x200 means width=130, height=200

Multiple option values

Multiple values can be passed to an option as an array or as an object literal. See the example below, both ways result in the same command arguments passed to unoconv.

const options = {
  printer: ['PaperFormat=A3', 'PaperOrientation=landscape'],
};

// or

const options = {
  printer: {
    PaperFormat: 'A3',
    PaperOrientation: 'landscape',
  },
};

Example options

const options = {
  connection: 'socket,host=localhost,port=2002;urp;StarOffice.ComponentContext',
  output: 'PDF',
  doctype: 'document',
  password: 'Abc123!@#',
  server: '127.0.0.1',
  port: 2002,
  export: { PageRange: '1-2' },
  field: { foo: 'bar' },
  format: 'PDF',
  import: { Quality: 50 },
  inputFilterName: 'odt',
  disableHtmlUpdateLinks: false,
  noLaunch: true,
  preserve: true,
  printer: ['PaperFormat=A3', 'PaperOrientation=landscape'],
}
{
  connection: '-c',
  disableHtmlUpdateLinks: '--disable-html-update-links',
  doctype: '-d',
  export: '-e',
  field: '-F',
  format: '-f',
  import: '-i',
  importFilterName: '-I',
  listener: '-l',
  noLaunch: '-n',
  output: '-o',
  password: '--password',
  pipe: '--pipe',
  port: '-p',
  preserve: '--preserve',
  printer: '--printer',
  server: '--server',
  show: '--show',
  stdin: '--stdin',
  stdout: '--stdout',
  template: '-t',
  timeout: '-T',
  unsafeQuietUpdate: '--unsafe-quiet-update',
  userProfile: '--user-profile',
  verbose: '--verbose',
};

🚀 Alternatives

PackageSizeLast commitDownloads/mo
damian66/node-unoconvnpm bundle sizeGitHub last commitnpm
gfloyd/node-unoconvnpm bundle sizeGitHub last commitnpm
zxhaaa6/awesome-unoconvnpm bundle sizeGitHub last commitnpm
vworld4u/office-converternpm bundle sizeGitHub last commitnpm
ryanhanwu/Unoconv-Promisenpm bundle sizeGitHub last commitnpm
1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

3 years ago

1.0.1

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago

1.0.0

3 years ago