0

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.

user3552342
  • 657
  • 1
  • 5
  • 14
  • I am getting the same excel file on click of PDF download link on browser but I can see that converted PDF file with same file size of excel file in the temp directory but that file is not opening and showing error. But when I changed the extension of PDF file to excel file it opened as excel file without any error which means file is not converting to the PDF file and just changing the file extenstion as pdf. – user3552342 Feb 01 '21 at 11:42

1 Answers1

0

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.

user3552342
  • 657
  • 1
  • 5
  • 14