zxhaaa6 / awesome-unoconv

Node.js wrapper for converting Office files to PDF or HTML
MIT License
11 stars 1 forks source link

awesome-unoconv

Node.js wrapper for converting Office files to PDF or HTML

npm Travis npm

Setup

Requirement

Unoconv is required, which requires LibreOffice (or OpenOffice).

Note that: The latest versions of LibreOffice have a completely different folder structure, so unoconv does not find its dependencies. Tested with LibreOffice version 4.2.5.

$ brew install unoconv

Or pull Docker image unoconv.

Installation

$ npm install awesome-unoconv

Usage

Convert document to pdf directly.

const path = require('path');
const unoconv = require('awesome-unoconv');

const sourceFilePath = path.resolve('./myDoc.docx');
const outputFilePath = path.resolve('./myDoc.pdf');

unoconv
  .convert(sourceFilePath, outputFilePath)
  .then(result => {
    console.log(result); // return outputFilePath
  })
  .catch(err => {
    console.log(err);
  });

Convert document to pdf or html with options.

const path = require('path');
const unoconv = require('awesome-unoconv');

const sourceFilePath = path.resolve('./myDoc.docx');
const outputFilePath = path.resolve('./myDoc.pdf'); // or 'myDoc.html'

unoconv
  .convert(inputPath, { output: outputPath, format: 'pdf' })  // or format: 'html'
  .then(result => {
    console.log(result); // return outputFilePath
  })
  .catch(err => {
    console.log(err);
  });

Convert document to Buffer.

const fs = require('fs');
const path = require('path');
const unoconv = require('awesome-unoconv');

const sourceFilePath = path.resolve('./myDoc.docx');
const outputFilePath = path.resolve('./myDoc.pdf'); // or 'myDoc.html'

unoconv
  .convert(inputPath, { buffer: true, format: 'pdf' })  // or format: 'html'
  .then(buffer => {
    // return Buffer
    fs.writeFileSync(outputPath, buffer, { encoding: 'utf8' });
    console.log(`File save at ${outputPath}`);
  })
  .catch(err => {
    console.log(err);
  });

Test

There is a test case inside test folder.