0

java code to generate the conversion of word to pdf

package com.sonakshi;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import org.apache.commons.io.output.ByteArrayOutputStream;

import java.io.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class Hello{
    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();

        InputStream in = new BufferedInputStream(new FileInputStream("//home//sonakshi_user//Documents//WordDocToConvert.docx"));
        IConverter converter = LocalConverter.builder()
                .baseFolder(new File("//home//sonakshi_user//Documents//"))
                .workerPool(20, 25, 2, TimeUnit.SECONDS)
                .processTimeout(5, TimeUnit.SECONDS).build();

        Future<Boolean> conversion = converter
                .convert(in).as(DocumentType.MS_WORD)
                .to(bo).as(DocumentType.PDF)
                .prioritizeWith(1000) // optional
                .schedule();
        conversion.get();
        try (OutputStream outputStream = new FileOutputStream("//home//sonakshi_user//Documents//Output.pdf")) {
            bo.writeTo(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        in.close();
        bo.close();
    }
}

I am getting this below exception generated while conversion

log4j:WARN No appenders could be found for logger (org.zeroturnaround.exec.ProcessExecutor).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.IllegalStateException: class com.documents4j.conversion.msoffice.MicrosoftWordBridge could not be created by a (File, long, TimeUnit) constructor
    at com.documents4j.conversion.ExternalConverterDiscovery.make(ExternalConverterDiscovery.java:33)
    at com.documents4j.conversion.ExternalConverterDiscovery.makeAll(ExternalConverterDiscovery.java:43)
    at com.documents4j.conversion.ExternalConverterDiscovery.loadConfiguration(ExternalConverterDiscovery.java:86)
    at com.documents4j.conversion.DefaultConversionManager.<init>(DefaultConversionManager.java:22)
    at com.documents4j.job.LocalConverter.makeConversionManager(LocalConverter.java:79)
    at com.documents4j.job.LocalConverter.<init>(LocalConverter.java:51)
    at com.documents4j.job.LocalConverter$Builder.build(LocalConverter.java:186)
    at com.sonakshi.Hello.main(Hello.java:20)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at com.documents4j.conversion.ExternalConverterDiscovery.make(ExternalConverterDiscovery.java:31)
    ... 7 more
Caused by: com.documents4j.throwables.ConverterAccessException: Unable to run script: /home/sonakshi_user/Documents/word_start1713780447.vbs
    at com.documents4j.conversion.AbstractExternalConverter.runNoArgumentScript(AbstractExternalConverter.java:76)
    at com.documents4j.conversion.msoffice.AbstractMicrosoftOfficeBridge.runNoArgumentScript(AbstractMicrosoftOfficeBridge.java:51)
    at com.documents4j.conversion.msoffice.AbstractMicrosoftOfficeBridge.tryStart(AbstractMicrosoftOfficeBridge.java:34)
    at com.documents4j.conversion.msoffice.MicrosoftWordBridge.startUp(MicrosoftWordBridge.java:44)
    at com.documents4j.conversion.msoffice.MicrosoftWordBridge.<init>(MicrosoftWordBridge.java:39)
    ... 12 more
Caused by: org.zeroturnaround.exec.ProcessInitException: Could not execute [cmd, /S, /C, ""/home/sonakshi_user/Documents/word_start1713780447.vbs""] in /home/sonakshi_user/Documents. Error=2, No such file or directory
    at org.zeroturnaround.exec.ProcessInitException.newInstance(ProcessInitException.java:80)
    at org.zeroturnaround.exec.ProcessExecutor.invokeStart(ProcessExecutor.java:1002)
    at org.zeroturnaround.exec.ProcessExecutor.startInternal(ProcessExecutor.java:970)
    at org.zeroturnaround.exec.ProcessExecutor.execute(ProcessExecutor.java:906)
    at com.documents4j.conversion.AbstractExternalConverter.runNoArgumentScript(AbstractExternalConverter.java:72)
    ... 16 more
Caused by: java.io.IOException: Cannot run program "cmd" (in directory "/home/sonakshi_user/Documents"): error=2, No such file or directory
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
    at org.zeroturnaround.exec.ProcessExecutor.invokeStart(ProcessExecutor.java:997)
    ... 19 more
Caused by: java.io.IOException: error=2, No such file or directory
    at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
    at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:340)
    at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:271)
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
    ... 21 more
Exception in thread "Shutdown hook: com.documents4j.job.LocalConverter" java.lang.NullPointerException
    at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:100)
    at com.documents4j.job.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:134)

I have included many jar files as well. Kindly specify if I need to use specific one

macropod
  • 12,757
  • 2
  • 9
  • 21
Sonakshi
  • 1
  • 1

0 Answers0