rejetto forum

How about introducing a document converter to your template & server?

0 Members and 1 Guest are viewing this topic.

Offline NaitLee

  • Tireless poster
  • ****
    • Posts: 203
  • Computer-brain boy
    • View Profile
How about introducing a document converter (to html) to your template & server?
This can be quite useful if user wants to share documents (like .docx .odt formatted) with HFS.

There are all the codes for such a feature. Besides, you need to install LibreOffice to your server OS.
Code: [Select]

{.set|LibreOfficePath|C:\Program Files\LibreOffice\program\soffice.exe.}
{.break|if={.not|{.!EnableDocConvert.}.}|reason=Doc Convert not enabled.}
{.break|if={.not|{.exists|{.^LibreOfficePath.}.}.}|reason=No LibreOffice installed.}

{.set|target|{.vfs to disk|{.urlvar|path.}.}.}

{.save|{.^folder.}\index.html|{.replace|@@filename@@|{.^file.}|{.no pipe|{.$docview.html.}.}.}.}

{.set|cmd|"{.^LibreOfficePath.}" --convert-to html --outdir "{.^folder.}" --convert-images-to "gif" "{.^target.}\{.^file.}".}

<!DOCTYPE html>
function getdoc () {
var xhr = new XMLHttpRequest();
var docfile = './'+'@@filename@@'.replace(/\..*$/, '')+'.html';'HEAD', docfile);
xhr.onload = function() {
if (xhr.status == '404') { // Conversion not completed yet
setTimeout(function() { getdoc(); }, 1000);
} else if (xhr.status == '200') { // Done, go
<h1 style="text-align: center;">{.!Converting document, please wait....}</h1>

var converttohtml = function (file, path) {
notice('{.!Converting.} '+file+' {.!to .html format..} {.!Please wait.}', '{.!Conversion Started.}');
var xhr = new XMLHttpRequest();'POST', '/~ajax.convertdoctohtml?file='+file+'&path='+path);
xhr.onload = function() {
var response = xhr.responseText.trim();
if (response.substring('Doc Convert not enabled') >= -1) {
popup('{.!Conversion failed..}<br />{.!Doc Convert not enabled.}', '?alert');
} else if (response.substring('No LibreOffice installed') >= -1) {
popup('{.!Conversion failed..}<br />{.!Server has no LibreOffice installed..}', '?alert');
} else {
// popup('{.!Conversion started.}<br />{.!You may wait for a while to see specified .html file..}', '?alert');
setTimeout(function() {
previewfile('?open', path+file+'.html');
}, 1000);

// Then use converttohtml('file.docx', '/my shared files/folder/'); to convert your file.
// There will be a folder named "file.docx.html" created, it contains an index.html for auto-refreshing and the converted file.html.

These codes presents in new preview version of Takeback,
 you may test it. Don't forget to enable that feature by editing it first.
Some code parts above contains APIs in Takeback. Change them to yours as well.

After all, just have fun!  ;)

Edit: Updated the code to avoid filename being parsed as response code.
« Last Edit: September 29, 2020, 08:00:49 AM by NaitLee »
"Computation is not forbidden magic."
Takeback Template | PHFS