sajad torkamani

Install libreoffice

Assuming PHP is running on Alpine Linux, install the libreoffice package:

apk add --no-cache libreoffice

This will give you the soffice command-line tool which will let you use LibreOffice to convert Word documents to PDF.

Install related font packages

When using the soffice tool, you’ll need to either install all the fonts that the Word documents use or install suitable replacements. Instead of installing all possible fonts, you may be able to get away with imposing a constraint on your users so that all Word docs must use a particular subset of fonts.

Assuming we can guarantee that all the Word docs will use the Arial font only, we can install the Liberation font family:

RUN apk add --no-cache font-liberation

We’ll also install the ttf-dejavu package to handle glyphs.

RUN apk add --no-cache ttf-dejavu

And finally the fontconfig package so LibreOffice can use the installed fonts:

RUN apk add --no-cache fontconfig

Convert Word docs to PDF

Now that we have all the font-related packages installed, we can convert the Word docs to PDF:

soffice --headless --convert-to pdf --outdir <directory-path-to-pdf> <path-to-word-doc>