javaspringjodconverterwicket-8

org.artofsolving.jodconverter api not converting Excel file into PDF file in wicket 8 application


I am trying to convert excel file into PDF file in wicket 8 application by using these below APIs. But PDF file is not converting into excel file and I am getting that same excel file on the PDF download link instead of PDF file and there is no exception or error on convert() method.

<dependency>
    <groupId>net.sf.jodconverter</groupId>
    <artifactId>jodconverter</artifactId>
    <version>3.0-beta-4</version>
</dependency>

or

<dependency>
    <groupId>com.artofsolving</groupId>
    <artifactId>jodconverter</artifactId>
    <version>2.2.1</version>
</dependency>

Using this below code to convert excel file to PDF file

public File convertToPDFFile(ByteArrayOutputStream fromExcelFile, String sheetName, OOConfig ooConfig, FileFormat fileFormat) throws Exception {
    File tempFile = null;
    File resultPDFFile = null;
    try {
        tempFile = File.createTempFile(sheetName, fileFormat.getFileExtension());
        tempFile.setWritable(true);

        FileOutputStream fout = new FileOutputStream(tempFile);
        fromExcelFile.writeTo(fout);

        ExternalOfficeManagerConfiguration eomcTest = new ExternalOfficeManagerConfiguration();
        eomcTest.setConnectOnStart(true);
        eomcTest.setConnectionProtocol("SOCKET");
        eomcTest.setPortNumber(8100);

        OfficeManager officeManager = eomcTest.buildOfficeManager();
        officeManager.start();
        OfficeDocumentConverter officeDocConverter = new OfficeDocumentConverter(officeManager);
        resultPDFFile = File.createTempFile(sheetName, TypeOfFile.PDF.getFileExtension());
        officeDocConverter.convert(tempFile, resultPDFFile);
        fout.close();
        officeManager.stop();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (tempFile != null) {
            tempFile.delete();
            tempFile = null;
        }
    }
    return resultPDFFile;
}

Kindly anyone let me know why jodconverter not converting excel file into pdf file.

Any suggestions will be highly appreciable.


Solution

  • This above code worked for me after debugging deeper in the local system.

    For that need to install OpenOffice3 or version 4 and can run below command to run the OpenOffice as a service in windows OS.

    Go to CMD and navigate to OpenOffice program dir path: Example: "C:\OpenOffice.org 3\program" and below command in CMD to run the OpenOffice as a service to run the Jodconverter code for file conversion.

    start soffice -headless -accept=socket,host=0,port=8100;urp;
    

    This can help to run the OpenOffice as a server in the local system for Windows OS and can debug this code in deeper to resolve any issue or to make any changes.

    I hope it can help someone who wants to execute this code in the local system for your application with OpenOffice.