Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 2fcc3f670a6e8f5c5e6d58399d463f251b49ec83 @gfloyd committed Dec 17, 2012
Showing with 271 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +63 −0 README.md
  3. +182 −0 index.js
  4. +25 −0 package.json
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,63 @@
+# node-unoconv
+
+A node.js wrapper for converting documents with [unoconv](http://dag.wieers.com/home-made/unoconv/).
+
+## Requirements
+
+[Unoconv](http://dag.wieers.com/home-made/unoconv/) is required, which requires [LibreOffice](http://www.libreoffice.org/) (or OpenOffice.)
+
+## Install
+
+Install with:
+
+ npm install node-unoconv
+
+## Converting documents
+
+ var unoconv = require('unoconv');
+
+ unoconv.convert('document.docx', 'pdf', function (err, result) {
+ // result is returned as a Buffer
+ fs.writeFile('converted.pdf', result);
+ });
+
+## Starting a listener
+
+You can also start a unoconv listener to avoid launching Libre/OpenOffice on every conversion:
+
+ unoconv.listen();
+
+## API
+
+### unoconv.convert(file, outputFormat, [options], callback)
+
+Converts `file` to the specified `outputFormat`. `options` is an object with the following properties:
+
+* `bin` Path to the unoconv binary
+* `port` Unoconv listener port to connect to
+
+`callback` gets the arguments `err` and `result`. `result` is returned as a Buffer object.
+
+
+### unoconv.listen([options])
+
+Starts a new unoconv listener. `options` accepts the same parameters as `convert()`.
+
+Returns a `ChildProcess` object. You can handle errors by listening to the `stderr` property:
+
+ var listener = unoconv.listen({ port: 2002 });
+
+ listener.stderr.on('data', function (data) {
+ console.log('stderr: ' + data.toString('utf8));
+ });
+
+### unoconv.detectSupportedFormats([options], callback)
+
+This function parses the output of `unoconv --show` to attempt to detect supported output formats.
+
+`options` is an object with the following properties:
+
+* `bin` Path to the unoconv binary
+
+`callback` gets the arguments `err` and `result`. `result` is an object containing a collection of supported document types and output formats.
+
182 index.js
@@ -0,0 +1,182 @@
+'use strict';
+
+var _ = require('underscore'),
+ childProcess = require('child_process'),
+ mime = require('mime');
+
+var unoconv = exports = module.exports = {};
+
+/**
+* Convert a document.
+*
+* @param {String} file
+* @param {String} outputFormat
+* @param {Object|Function} port
+* @param {Function} callback
+* @api public
+*/
+unoconv.convert = function(file, outputFormat, options, callback) {
+ var self = this,
+ args,
+ bin = 'unoconv',
+ child,
+ stdout = [],
+ stderr = [];
+
+ if (_.isFunction(options)) {
+ callback = options;
+ options = null;
+ }
+
+ args = [
+ '-f' + outputFormat,
+ '--stdout'
+ ];
+
+ if (options && options.port) {
+ args.push('-p' + options.port)
+ }
+
+ args.push(file);
+
+ if (options && options.bin) {
+ bin = options.bin;
+ }
+
+ child = childProcess.spawn(bin, args, function (err, stdout, stderr) {
+ if (err) {
+ return callback(err);
+ }
+
+ if (stderr) {
+ return callback(new Error(stderr.toString()));
+ }
+
+ callback(null, stdout);
+ });
+
+ child.stdout.on('data', function (data) {
+ stdout.push(data);
+ });
+
+ child.stderr.on('data', function (data) {
+ stderr.push(data);
+ });
+
+ child.on('exit', function () {
+ if (stderr.length) {
+ return callback(new Error(Buffer.concat(stderr).toString()));
+ }
+
+ callback(null, Buffer.concat(stdout));
+ });
+};
+
+/**
+* Start a listener.
+*
+* @param {Object} options
+* @return {ChildProcess}
+* @api public
+*/
+unoconv.listen = function (options) {
+ var self = this,
+ args,
+ bin = 'unoconv';
+
+ args = [ '--listener' ];
+
+ if (options && options.port) {
+ args.push('-p' + options.port);
+ }
+
+ if (options && options.bin) {
+ bin = options.bin;
+ }
+
+ return childProcess.spawn(bin, args);
+};
+
+/**
+* Detect supported conversion formats.
+*
+* @param {Object|Function} options
+* @param {Function} callback
+*/
+unoconv.detectSupportedFormats = function (options, callback) {
+ var self = this,
+ docType,
+ detectedFormats = {
+ document: [],
+ graphics: [],
+ presentation: [],
+ spreadsheet: []
+ },
+ bin = 'unoconv';
+
+ if (_.isFunction(options)) {
+ callback = options;
+ options = null;
+ }
+
+ if (options && options.bin) {
+ bin = options.bin;
+ }
+
+ childProcess.execFile(bin, [ '--show' ], function (err, stdout, stderr) {
+ if (err) {
+ return callback(err);
+ }
+
+ // For some reason --show outputs to stderr instead of stdout
+ var lines = stderr.split('\n');
+
+ lines.forEach(function (line) {
+ if (line === 'The following list of document formats are currently available:') {
+ docType = 'document';
+ } else if (line === 'The following list of graphics formats are currently available:') {
+ docType = 'graphics';
+ } else if (line === 'The following list of presentation formats are currently available:') {
+ docType = 'presentation';
+ } else if (line === 'The following list of spreadsheet formats are currently available:') {
+ docType = 'spreadsheet';
+ } else {
+ var format = line.match(/^(.*)-/);
+
+ if (format) {
+ format = format[1].trim();
+ }
+
+ var extension = line.match(/\[(.*)\]/);
+
+ if (extension) {
+ extension = extension[1].trim().replace('.', '');
+ }
+
+ var description = line.match(/-(.*)\[/);
+
+ if (description) {
+ description = description[1].trim();
+ }
+
+ if (format && extension && description) {
+ detectedFormats[docType].push({
+ 'format': format,
+ 'extension': extension,
+ 'description': description,
+ 'mime': mime.lookup(extension)
+ });
+ }
+ }
+ });
+
+ if (detectedFormats.document.length < 1 &&
+ detectedFormats.graphics.length < 1 &&
+ detectedFormats.presentation.length < 1 &&
+ detectedFormats.spreadsheet.length < 1) {
+ return callback(new Error('Unable to detect supported formats'));
+ }
+
+ callback(null, detectedFormats);
+ });
+};
@@ -0,0 +1,25 @@
+{
+ "name": "node-unoconv",
+ "version": "0.1.0",
+ "description": "Wrapper for converting documents with unoconv.",
+ "homepage": "https://github.com/gfloyd/node-unoconv",
+ "author": {
+ "name": "Graham Floyd",
+ "email": "grahamf@gmail.com"
+ },
+ "main": "./index",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/gfloyd/node-unoconv.git"
+ },
+ "keywords": [
+ "unoconv",
+ "document",
+ "conversion"
+ ],
+ "dependencies": {
+ "underscore": "1.4.x",
+ "mime": "1.2.x"
+ },
+ "license": "MIT"
+}

0 comments on commit 2fcc3f6

Please sign in to comment.