Node.js wrapper for converting Office files to PDF or HTML
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.
$ npm install awesome-unoconv
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);
});
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);
});
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);
});
There is a test case inside test folder.