Skip to content

Instantly share code, notes, and snippets.

@oetiker
Last active March 13, 2024 14:11
Show Gist options
  • Save oetiker/c58e8f43c91444065e5ba3c88265d3f2 to your computer and use it in GitHub Desktop.
Save oetiker/c58e8f43c91444065e5ba3c88265d3f2 to your computer and use it in GitHub Desktop.
libreoffice as an external renderer for gitea

LibreOffice as an external renderer for gitea

NOTE that libreoffice is a huge software package and thus highly vulnerable. I would certainly not recommend this setup for a public gitea instance where arbitrary users can upload documents!

Gitea can use external renderers to transform any file into html and then display this html. This aproach is nice, but it tends to get into trouble when the file you want to display has graphically complex content.

Libreoffice can convert quite a lot of filetypes to html, but the results are not all that convincing when it comes to graphical content.

So we use a different aproach. Use libreoffice to convert the file to pdf and then use pdftocairo to turn the files to svg and combine the svgs into a html file for gitea to display.

Setup

Install libreoffice and poppler

apt install libreoffice poppler-utils

Update your /etc/gitea/app.ini and add the /usr/local/bin/lo2pdf2svg2html.sh script and make it executable

chmod 755 /usr/local/bin/lo2pdf2svg2html.sh
# add this to your /etc/gitea/app.ini
[markup.lo2pdf2svg2html]
ENABLED = true
FILE_EXTENSIONS = .odt,.ods,.odg,.docx,.ppt
RENDER_COMMAND = /usr/local/bin/lo2pdf2svg2html.sh
IS_INPUT_FILE = true
RENDER_CONTENT_MODE = no-sanitizer
#!/bin/bash
#set -x
INPUT=$1
OUTPUT=$(basename $INPUT).pdf
libreoffice --headless --convert-to pdf $INPUT >/dev/null
num_pages=$(pdfinfo $OUTPUT | grep Pages | awk '{print $2}')
cat <<HEAD_END
<style>
.file-view.markup.lo2pdf2svg2html img {
width: 100%;
display: block;
background-color: white;
}
.file-view.markup.lo2pdf2svg2html div {
border-bottom-style: solid;
border-width: 1px;
border-color: #aaa;
text-align: right;
font-size: 90%;
}
</style>
HEAD_END
for (( i=1; i<=num_pages; i++ ))
do
echo "<div>Page $i/$num_pages</div>"
echo -n '<img src="data:image/svg+xml;base64,'
pdftocairo -svg -f $i -l $i $OUTPUT - | base64 --wrap=0
echo '"/>'
done
rm $OUTPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment