Maintaining Font in DOCX to PDF Command Line Conversion - Ubuntu Server 20.04

I have Libreoffice installed in Ubuntu 20.04 server (no GUI). I am converting a Word DOCX file to PDF with a bash script using soffice, but the resulting PDF does not respect the original font of the Word doc (Garamond). Instead, it’s using DejaVuSerif. Here’s my original command:

soffice --headless --invisible --convert-to pdf "<filename>"

I tried installing a Garamond font in Ubuntu:

sudo apt-get install fonts-ebgaramond-extra

which put a Garamond font in my /usr/share/fonts folder:


However, Libreoffice does not use this font when converting the DOCX. I’m not sure if the font I installed is equivalent with the version of Garamond in Word, or if there are extra steps involved to install the font in Libreoffice.

Then I tried messing with the - -infilter option. I’m not sure if that’s the right option to use, but I saw some threads that mentioned controlling the font using - -infilter:

soffice --headless --invisible --infilter="writer_pdf_export:,,Garamond," --convert-to pdf "<filename>"

I tried many different combinations of parameters in - -infilter, but none of them seemed to have any effect. I tried using different names for the font: Garamond / EBGaramond / garamond / ebgaramond / EB Garamond.

I also tried adding some extra parameters to the - -convert-to option based on some examples I saw in another thread:

soffice --headless --invisible --convert-to "pdf:writer_pdf_Export:UTF8:LF:Garamond" "<filename>"

None of these attempts have had any effect on the resulting font in the PDF. Does anyone know if it’s possible to do this with soffice on the command line? Maybe the font can be changed in the resulting PDF after the conversion has run?

First step: Check, if ms-corefonts is installed. If they are not installed replacements are shown. Usually the names stay intact.

Second: try Libre and your pdf-viewer, if this works without headless etc.

  1. LibreOffice should use the same font in PDF export that is uses when you view the file interactively. So try opening the file on that system in GUI mode, and check if it uses the correct font. If it does, try exporting to PDF from GUI. Then (if at least it opens the DOCX correctly) possibly file a bug report with a sample problematic DOCX attached.
  2. Your experiments with filters make no sense.
    2.1. --infilter="writer_pdf_export:,,Garamond," is complete nonsense. It puts the export filter (writer_pdf_export) into --infilter, which is for input filters. It uses something from another filter (namely, Text (encoded)) - you just can’t randomly use one filter’s syntax with another. And in this case, it is even wrong logically: the font name used for plain text import is used to tell writer which font to use for the text that has no information about font itself; you try to use it for telling writer which font to use on export - for a format that has font information; and which part of document are you telling writer to mark with this font?
    2.2. --convert-to "pdf:writer_pdf_Export:UTF8:LF:Garamond" tries to set up export filter, but again tries to use arguments only applicable to another filter, and adds UTF8 and LF to the mix, which, again, have nothing to do with PDF export.

Again: if your file can be open correctly in GUI mode on the system in question, that wrong command line export is a bug, and needs a bug report; no amount of workarounds would help, only reporting the bug would enable it to be fixed.

Thanks for the response. Unfortunately this system has no GUI mode as it’s the Ubuntu server OS. It’s a lightweight VM that we install on client systems. We create document templates in Word (on our own Windows OS), and the VM generates documents from the templates for our clients (with a python library), which the clients can then view and edit in their own Windows OS.

I think the issue is that the Microsoft version of the font is not installed. LibreOffice does not seem to recognize the open-source version downloaded as equivalent to the Microsoft version. I was trying to mess around with the conversion command to get it to use the open source font, but I don’t have much experience with the soffice tool and have had trouble interpreting the reference docs, as you noticed.

Since Word is not installed on the VM itself, the Microsoft version of Garamond is not present on the VM, and I haven’t been able to find a way to download it separately. I suspect if this is possible, it may come with an added cost and some licensing issues.

Maybe I can install the open-source Garamond in Windows and adjust the base template to use that instead of the Microsoft version. LibreOffice may then recognize that font (which is downloaded in Ubuntu). I just thought of that so I’ll give it a try

1 Like

I got this to work by copying the font directly from my Windows system into the VM and changing the filename of the font:

Original location (need to use the UNC path to see the individual font files - otherwise they get bundled together in the special Windows Font folder):


Location in Ubuntu after copy:


And then the standard PDF conversion command:

soffice --headless --invisible --convert-to pdf "<filename>"

It didn’t work until I renamed the font file from GARABD.TTF to garamond.ttf.

Now I need to figure out if there’s a way to legally set this up for the client. I don’t think we’d be able to put the font file in the VM and then distribute it to the client. But the client has a copy of Word, so maybe if we copy their own font file into their already-installed copy of the VM (and only do so for this client) we can get around that legal issue?

After looking at some EULAs, I’m not even sure if it’s legal for me to copy the font file from my Windows system to my local Ubuntu VM. So I’ve deleted the files from my VM for now.

If anyone can point me to resources that will confirm or deny whether this method is legal, that would be much appreciated. Or if you know of the appropriate forum to ask this type of question.
I will do some more research and look for another forum to try and clarify

Here’s a starting point, the page for Microsoft Garamond font Garamond font family - Typography | Microsoft Docs with links to licensing

Thanks. I saw that link a few mins ago and tried to open the Garamond page, but I get a “Page Not Found” error.

By looking at the properties of the font file in Windows, it looks like the font is produced by Monotype Typography Inc.. I’ve sent a support request from their website and it says someone will get back to me to learn more about my needs.

After emailing Monotype Typography Inc. to ask about the license implications of giving the Virtual Machine access to the Garamond font file, it looks like there is no way to do that without violating the license. I floated a solution of keeping the font file on the Windows host, but putting it in a shared folder that the VM could access, but that apparently violates the license as well.

We are now in discussions to get a quote for our client for an “unlimited server license” for the Garamond font. With that type of license, we’d be able to put the font file in the VM without any legal issues.

You could also check, if you need the excact Garamond by Monotype or can switch to a variant like EB Garamont or even Cormorant where you get easier licenses.

There is quite a difference between Garamond and EB Garamond, repagination is likely.

With a GUI you could easily create a font substitution table in Tools > Options > LibreOffice > Fonts replacing Garamond with Linux Libertine Display G which might not have so many issues but is still a line and a bit more in the Dummy Text. I guess there is a configuration file in there somewhere that could be edited.