Skip to content

Instantly share code, notes, and snippets.

@threeaccents
Last active June 2, 2016 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save threeaccents/fb549371dce23c15c85598064bfd708e to your computer and use it in GitHub Desktop.
Save threeaccents/fb549371dce23c15c85598064bfd708e to your computer and use it in GitHub Desktop.
const spawnSync = require('child_process').spawnSync;
const defaultDestPath = __dirname + '/pdf';
function convertToPdf(filepath, destpath) {
var resp = {};
if (destpath == undefined) {
destpath = defaultDestPath;
}
var cmd = 'soffice';
var args = ['--headless', '"-env:UserInstallation=file:///tmp/LibreOffice_Conversion_${USER}"', '--convert-to', 'pdf:writer_pdf_Export', filepath, '--outdir', destpath]
var spawn = spawnSync(cmd, args);
var resp = {};
if (spawn.error != undefined || spawn.stderr.toString() != '') {
if (spawn.error != undefined) {
resp.error = spawn.error.code;
resp.destpath = null;
}
if (spawn.stderr.toString() != '' != null) {
resp.stderr = spawn.stderr.toString();
resp.destpath = destpath;
}
} else if (spawn.stdout) {
resp.error = null;
resp.destpath = destpath;
}
return resp;
}
var res = convertToPdf("test.doc");
console.log(res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment