Recently I had to convert generated .docx files to .pdf files for more convenient distribution. The Word documents contained some custom formatting and additional pictures. I tried several Java libraries for doing this job (Docx4j, XDocReport and Apache POI) but all of them couldn't generate the output I got from manually converting the .docx files with Microsoft Word's native export functionality. On GitHub, I found a nice command-line tool for converting the documents to pdf files: OfficeToPDF. In this blog post, I'll show you a quick example on how to use this CLI tool. This helps us to convert docx to pdf using Java without losing formatting.
First off there are some technical requirements you need to fulfill:
These requirements are a strong indicator of running this solution on a Windows machine. To try the following example on your machine, you need to download the .exe from the GitHub project site and have a .docx for the conversion at hand.
Calling the .exe from Java might look like the following (the CLI expects just two parameters: the path of the .docx file and the path of the generated .pdf ):