Merge pull request #2813 from simong/export-libreoffice
Allow LibreOffice to be used when exporting a padpull/2817/head
commit
5652adb4eb
|
@ -86,10 +86,14 @@
|
||||||
may cause problems during deployment. Set to 0 to disable caching */
|
may cause problems during deployment. Set to 0 to disable caching */
|
||||||
"maxAge" : 21600, // 60 * 60 * 6 = 6 hours
|
"maxAge" : 21600, // 60 * 60 * 6 = 6 hours
|
||||||
|
|
||||||
/* This is the path to the Abiword executable. Setting it to null, disables abiword.
|
/* This is the absolute path to the Abiword executable. Setting it to null, disables abiword.
|
||||||
Abiword is needed to advanced import/export features of pads*/
|
Abiword is needed to advanced import/export features of pads*/
|
||||||
"abiword" : null,
|
"abiword" : null,
|
||||||
|
|
||||||
|
/* This is the absolute path to the soffice executable. Setting it to null, disables LibreOffice exporting.
|
||||||
|
LibreOffice can be used in lieu of Abiword to export pads */
|
||||||
|
"soffice" : null,
|
||||||
|
|
||||||
/* This is the path to the Tidy executable. Setting it to null, disables Tidy.
|
/* This is the path to the Tidy executable. Setting it to null, disables Tidy.
|
||||||
Tidy is used to improve the quality of exported pads*/
|
Tidy is used to improve the quality of exported pads*/
|
||||||
"tidyHtml" : null,
|
"tidyHtml" : null,
|
||||||
|
|
|
@ -30,9 +30,15 @@ var os = require('os');
|
||||||
var hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");
|
var hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");
|
||||||
var TidyHtml = require('../utils/TidyHtml');
|
var TidyHtml = require('../utils/TidyHtml');
|
||||||
|
|
||||||
|
var convertor = null;
|
||||||
|
|
||||||
//load abiword only if its enabled
|
//load abiword only if its enabled
|
||||||
if(settings.abiword != null)
|
if(settings.abiword != null)
|
||||||
var abiword = require("../utils/Abiword");
|
convertor = require("../utils/Abiword");
|
||||||
|
|
||||||
|
// Use LibreOffice if an executable has been defined in the settings
|
||||||
|
if(settings.soffice != null)
|
||||||
|
convertor = require("../utils/LibreOffice");
|
||||||
|
|
||||||
var tempDirectory = "/tmp";
|
var tempDirectory = "/tmp";
|
||||||
|
|
||||||
|
@ -70,71 +76,11 @@ exports.doExport = function(req, res, padId, type)
|
||||||
}
|
}
|
||||||
else if(type == "txt")
|
else if(type == "txt")
|
||||||
{
|
{
|
||||||
var txt;
|
exporttxt.getPadTXTDocument(padId, req.params.rev, false, function(err, txt)
|
||||||
var randNum;
|
|
||||||
var srcFile, destFile;
|
|
||||||
|
|
||||||
async.series([
|
|
||||||
//render the txt document
|
|
||||||
function(callback)
|
|
||||||
{
|
|
||||||
exporttxt.getPadTXTDocument(padId, req.params.rev, false, function(err, _txt)
|
|
||||||
{
|
|
||||||
if(ERR(err, callback)) return;
|
|
||||||
txt = _txt;
|
|
||||||
callback();
|
|
||||||
});
|
|
||||||
},
|
|
||||||
//decide what to do with the txt export
|
|
||||||
function(callback)
|
|
||||||
{
|
|
||||||
//if this is a txt export, we can send this from here directly
|
|
||||||
res.send(txt);
|
|
||||||
callback("stop");
|
|
||||||
},
|
|
||||||
//send the convert job to abiword
|
|
||||||
function(callback)
|
|
||||||
{
|
|
||||||
//ensure html can be collected by the garbage collector
|
|
||||||
txt = null;
|
|
||||||
|
|
||||||
destFile = tempDirectory + "/etherpad_export_" + randNum + "." + type;
|
|
||||||
abiword.convertFile(srcFile, destFile, type, callback);
|
|
||||||
},
|
|
||||||
//send the file
|
|
||||||
function(callback)
|
|
||||||
{
|
|
||||||
res.sendFile(destFile, null, callback);
|
|
||||||
},
|
|
||||||
//clean up temporary files
|
|
||||||
function(callback)
|
|
||||||
{
|
|
||||||
async.parallel([
|
|
||||||
function(callback)
|
|
||||||
{
|
|
||||||
fs.unlink(srcFile, callback);
|
|
||||||
},
|
|
||||||
function(callback)
|
|
||||||
{
|
|
||||||
//100ms delay to accomidate for slow windows fs
|
|
||||||
if(os.type().indexOf("Windows") > -1)
|
|
||||||
{
|
|
||||||
setTimeout(function()
|
|
||||||
{
|
|
||||||
fs.unlink(destFile, callback);
|
|
||||||
}, 100);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fs.unlink(destFile, callback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
], callback);
|
|
||||||
}
|
|
||||||
], function(err)
|
|
||||||
{
|
{
|
||||||
if(err && err != "stop") ERR(err);
|
if(ERR(err)) return;
|
||||||
})
|
res.send(txt);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -183,11 +129,11 @@ exports.doExport = function(req, res, padId, type)
|
||||||
TidyHtml.tidy(srcFile, callback);
|
TidyHtml.tidy(srcFile, callback);
|
||||||
},
|
},
|
||||||
|
|
||||||
//send the convert job to abiword
|
//send the convert job to the convertor (abiword, libreoffice, ..)
|
||||||
function(callback)
|
function(callback)
|
||||||
{
|
{
|
||||||
destFile = tempDirectory + "/etherpad_export_" + randNum + "." + type;
|
destFile = tempDirectory + "/etherpad_export_" + randNum + "." + type;
|
||||||
abiword.convertFile(srcFile, destFile, type, callback);
|
convertor.convertFile(srcFile, destFile, type, callback);
|
||||||
},
|
},
|
||||||
//send the file
|
//send the file
|
||||||
function(callback)
|
function(callback)
|
||||||
|
|
|
@ -123,8 +123,8 @@ function getHTMLFromAtext(pad, atext, authorColors)
|
||||||
var newLength = props.push(propName);
|
var newLength = props.push(propName);
|
||||||
anumMap[a] = newLength -1;
|
anumMap[a] = newLength -1;
|
||||||
|
|
||||||
css+=".removed {text-decoration: line-through; " +
|
css+=".removed {text-decoration: line-through; " +
|
||||||
"-ms-filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity=80)'; "+
|
"-ms-filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity=80)'; "+
|
||||||
"filter: alpha(opacity=80); "+
|
"filter: alpha(opacity=80); "+
|
||||||
"opacity: 0.8; "+
|
"opacity: 0.8; "+
|
||||||
"}\n";
|
"}\n";
|
||||||
|
@ -287,7 +287,7 @@ function getHTMLFromAtext(pad, atext, authorColors)
|
||||||
|
|
||||||
var s = taker.take(chars);
|
var s = taker.take(chars);
|
||||||
|
|
||||||
//removes the characters with the code 12. Don't know where they come
|
//removes the characters with the code 12. Don't know where they come
|
||||||
//from but they break the abiword parser and are completly useless
|
//from but they break the abiword parser and are completly useless
|
||||||
s = s.replace(String.fromCharCode(12), "");
|
s = s.replace(String.fromCharCode(12), "");
|
||||||
|
|
||||||
|
@ -401,7 +401,7 @@ function getHTMLFromAtext(pad, atext, authorColors)
|
||||||
pieces.push('<br><br>');
|
pieces.push('<br><br>');
|
||||||
}
|
}
|
||||||
}*/
|
}*/
|
||||||
else//means we are getting closer to the lowest level of indentation or are at the same level
|
else//means we are getting closer to the lowest level of indentation or are at the same level
|
||||||
{
|
{
|
||||||
var toClose = lists.length > 0 ? listLevels[listLevels.length - 2] - line.listLevel : 0
|
var toClose = lists.length > 0 ? listLevels[listLevels.length - 2] - line.listLevel : 0
|
||||||
if( toClose > 0){
|
if( toClose > 0){
|
||||||
|
@ -455,7 +455,7 @@ function getHTMLFromAtext(pad, atext, authorColors)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var k = lists.length - 1; k >= 0; k--)
|
for (var k = lists.length - 1; k >= 0; k--)
|
||||||
{
|
{
|
||||||
if(lists[k][1] == "number")
|
if(lists[k][1] == "number")
|
||||||
|
@ -484,14 +484,17 @@ exports.getPadHTMLDocument = function (padId, revNum, noDocType, callback)
|
||||||
stylesForExportCSS += css;
|
stylesForExportCSS += css;
|
||||||
});
|
});
|
||||||
// Core inclusion of head etc.
|
// Core inclusion of head etc.
|
||||||
var head =
|
var head =
|
||||||
(noDocType ? '' : '<!doctype html>\n') +
|
(noDocType ? '' : '<!doctype html>\n') +
|
||||||
'<html lang="en">\n' + (noDocType ? '' : '<head>\n' +
|
'<html lang="en">\n' + (noDocType ? '' : '<head>\n' +
|
||||||
'<title>' + Security.escapeHTML(padId) + '</title>\n' +
|
'<title>' + Security.escapeHTML(padId) + '</title>\n' +
|
||||||
'<meta charset="utf-8">\n' +
|
'<meta name="generator" content="Etherpad">\n' +
|
||||||
'<style> * { font-family: arial, sans-serif;\n' +
|
'<meta name="author" content="Etherpad">\n' +
|
||||||
'font-size: 13px;\n' +
|
'<meta name="changedby" content="Etherpad">\n' +
|
||||||
'line-height: 17px; }' +
|
'<meta charset="utf-8">\n' +
|
||||||
|
'<style> * { font-family: arial, sans-serif;\n' +
|
||||||
|
'font-size: 13px;\n' +
|
||||||
|
'line-height: 17px; }' +
|
||||||
'ul.indent { list-style-type: none; }' +
|
'ul.indent { list-style-type: none; }' +
|
||||||
|
|
||||||
'ol { list-style-type: none; padding-left:0;}' +
|
'ol { list-style-type: none; padding-left:0;}' +
|
||||||
|
@ -577,8 +580,8 @@ exports.getPadHTMLDocument = function (padId, revNum, noDocType, callback)
|
||||||
'ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol{ text-indent: 140px; }' +
|
'ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol{ text-indent: 140px; }' +
|
||||||
'ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol{ text-indent: 150px; }' +
|
'ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol > ol{ text-indent: 150px; }' +
|
||||||
|
|
||||||
stylesForExportCSS +
|
stylesForExportCSS +
|
||||||
'</style>\n' + '</head>\n') +
|
'</style>\n' + '</head>\n') +
|
||||||
'<body>';
|
'<body>';
|
||||||
var foot = '</body>\n</html>\n';
|
var foot = '</body>\n</html>\n';
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,93 @@
|
||||||
|
/**
|
||||||
|
* Controls the communication with LibreOffice
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var async = require("async");
|
||||||
|
var fs = require("fs");
|
||||||
|
var os = require("os");
|
||||||
|
var path = require("path");
|
||||||
|
var settings = require("./Settings");
|
||||||
|
var spawn = require("child_process").spawn;
|
||||||
|
|
||||||
|
// Conversion tasks will be queued up, so we don't overload the system
|
||||||
|
var queue = async.queue(doConvertTask, 1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a file from one type to another
|
||||||
|
*
|
||||||
|
* @param {String} srcFile The path on disk to convert
|
||||||
|
* @param {String} destFile The path on disk where the converted file should be stored
|
||||||
|
* @param {String} type The type to convert into
|
||||||
|
* @param {Function} callback Standard callback function
|
||||||
|
*/
|
||||||
|
exports.convertFile = function(srcFile, destFile, type, callback) {
|
||||||
|
queue.push({"srcFile": srcFile, "destFile": destFile, "type": type, "callback": callback});
|
||||||
|
};
|
||||||
|
|
||||||
|
function doConvertTask(task, callback) {
|
||||||
|
var tmpDir = os.tmpdir();
|
||||||
|
|
||||||
|
async.series([
|
||||||
|
// Generate a PDF file with LibreOffice
|
||||||
|
function(callback) {
|
||||||
|
var soffice = spawn(settings.soffice, [
|
||||||
|
'--headless',
|
||||||
|
'--invisible',
|
||||||
|
'--nologo',
|
||||||
|
'--nolockcheck',
|
||||||
|
'--convert-to', task.type,
|
||||||
|
task.srcFile,
|
||||||
|
'--outdir', tmpDir
|
||||||
|
]);
|
||||||
|
|
||||||
|
var stdoutBuffer = '';
|
||||||
|
|
||||||
|
// Delegate the processing of stdout to another function
|
||||||
|
soffice.stdout.on('data', function(data) {
|
||||||
|
stdoutBuffer += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Append error messages to the buffer
|
||||||
|
soffice.stderr.on('data', function(data) {
|
||||||
|
stdoutBuffer += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Throw an exception if libreoffice failed
|
||||||
|
soffice.on('exit', function(code) {
|
||||||
|
if (code != 0) {
|
||||||
|
return callback("LibreOffice died with exit code " + code + " and message: " + stdoutBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
callback();
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
// Move the PDF file to the correct place
|
||||||
|
function(callback) {
|
||||||
|
var filename = path.basename(task.srcFile);
|
||||||
|
var pdfFilename = filename.substr(0, filename.lastIndexOf('.')) + '.' + task.type;
|
||||||
|
var pdfPath = path.join(tmpDir, pdfFilename);
|
||||||
|
fs.rename(pdfPath, task.destFile, callback);
|
||||||
|
}
|
||||||
|
], function(err) {
|
||||||
|
// Invoke the callback for the local queue
|
||||||
|
callback();
|
||||||
|
|
||||||
|
// Invoke the callback for the task
|
||||||
|
task.callback(err);
|
||||||
|
});
|
||||||
|
}
|
|
@ -152,6 +152,11 @@ exports.minify = true;
|
||||||
*/
|
*/
|
||||||
exports.abiword = null;
|
exports.abiword = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The path of the libreoffice executable
|
||||||
|
*/
|
||||||
|
exports.soffice = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The path of the tidy executable
|
* The path of the tidy executable
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue