logos

With Andrii Grytsenko


Technical Diary - With Andrii Grytsenko

Trick: DOC to PDF on Linux

Here is little trick how to convert *.doc files into the *.pdf on Linux.

PRE-REQUIREMENTS:

oowriter has to be already installed. It is provided by Open Office system(for Debian its openoffice.org-writer package).

Unfortunately, I can’t remember the source where this macros is came from. Anyway here my script:

#!/bin/bash
# Written by Andrii Grytsenko
# pp: AndriiGrytsenko.net

if [ $# -lt 1 ]; then
    echo "Example: pdf2doc [doc_file] [output_dir]"
    echo "in case output_dir is missing generate into current dir"
fi

DOCDIR=$(dirname $1)
DOCFILE=$(basename $1)
PDFFILE=$(echo $DOCFILE | sed -e 's/\.doc$/\.pdf/g')
CURRENT_DIR=$(pwd)
OUT_DIR=$2

# if output_dir is missing set current
if [ -z $OUT_DIR ]; then
    OUT_DIR=$CURRENT_DIR
fi

if  echo $DOCDIR | grep -E "^\.$" > /dev/null; then
        DOCDIR=$CURRENT_DIR
elif ! echo $DOCDIR | grep -E "^/" > /dev/null; then
        DOCDIR=$CURRENT_DIR/$DOCDIR
fi

cd $DOCDIR
/usr/bin/oowriter -invisible "macro:///Standard.Module1.ConvertWordToPDF($DOCDIR/$DOCFILE)"
sleep 2 # for big files generation can take more time
mv $PDFFILE $OUT_DIR/
cd $CURRENT_DIR

Put file into the catalog from PATH env variable, into the /usr/bin for example. And call doc2pdf.

To generate question.pdf from question.doc and put it into the /tmp, run this:

doc2pdf ~/tmp2/questions.doc /tmp/

PROFIT:

andrii@agrytsenko:~$ ls -l /tmp/questions.pdf
-rw-r--r-- 1 andrii vboxusers 53226 Jul 16 14:17 /tmp/questions.pdf

Categories

Translate