How to Convert WebdavDocument (DOCX) to PDF?

I am using Webdav addon in my project which allows a user to upload and edit a Word DOCX document.

The attribute that stores the DOCX document in my entity is of type “com.haulmont.webdav.entity.WebdavDocument”.

I want to now generate a PDF file from that WebdavDocument and store in fileDescriptor attribute.

How can this be done ?

Hi, @robert.gilbert

First, you need to obtain a FileDescriptor of the docx document:

FileDescriptor fileDescriptor = document.getLastVersion().getFileDescriptor();

Then get an InputStream of this file. FileLoader can be used for this purpose:

try {
    InputStream inputStream = fileLoader.openStream(fileDescriptor);
    ...
} catch (FileStorageException e) {
    // log error
}

Using this InputStream, you can convert docx to pdf. Apache POI library can be used for conversion. Here is a related topic on Stackoverflow.
As you got the pdf document, you can save it to file storage using the FileLoader.saveStream() method.

More information about managing files in CUBA can be found in the documentation.

Regards,
Gleb

1 Like