Converting xlsx to pdf using documents4j

531 Views Asked by At

I'm writing a small application to convert office documents to pdf. Now I found Documents4j and as of now it's my best choice for .docx documents. It works very good.

Now I also want to use it for .xlsx -> .pdf conversions, but I'm having some trouble. I have the following code:

class OfficeToPdfConverter {
    fun convert(input: File, output: File) = try {
        LocalConverter.builder().build().convert(input).`as`(determineInput(input)).to(output)
            .`as`(DocumentType.PDF)
            .execute()
    } catch (e: Exception) {
        e.printStackTrace()
    }

    private fun determineInput(input: File) = when (input.extension) {
        "docx" -> DocumentType.DOCX
        "pptx" -> DocumentType.PPTX
        "xlsx" -> DocumentType.XLSX
        else -> DocumentType.DOCX
    }
}

For .docx this works. When I try to convert .xlsx or .pptx I get the following error messages:

  1. No converter for conversion of application/vnd.openxmlformats-officedocument.presentationml.presentation to application/pdf available
  2. No converter for conversion of application/vnd.openxmlformats-officedocument.presentationml.presentation to application/pdf available

I thought .xlsx and .pptx would just work, but I was a bit naïve I think.

Does anyone have experience with this, and how should I implement it?

0

There are 0 best solutions below