javams-wordconvertersdocuments4j

documents4j conveter to pdf doesn't work without Microsoft word?


i want to convert docx file to pdf with documents4j but i dont want to use MS word

is there any alternative solution ?

i used Apache poi but it didn't work correctly in Persian language

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

    InputStream in = new BufferedInputStream(new FileInputStream("d:\\c.docx"));
    IConverter converter = LocalConverter.builder()
            .baseFolder(new File("D:\\a"))
            .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("D:\\c.pdf")) {
        bo.writeTo(outputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    in.close();
    bo.close();
}
}

my error :

[main] INFO com.documents4j.conversion.msoffice.MicrosoftWordBridge - From-Microsoft-Word-Converter was started successfully
[main] INFO com.documents4j.job.LocalConverter - The documents4j local converter has started successfully
[pool-1-thread-1] INFO com.documents4j.conversion.msoffice.MicrosoftWordBridge - Requested conversion from D:\a\b3106f03-c187-4059-bbd0-46b7bb4a1404\temp1 (application/vnd.com.documents4j.any-msword) to D:\a\b3106f03-c187-4059-bbd0-46b7bb4a1404\temp2 (application/pdf)
Exception in thread "main" java.util.concurrent.ExecutionException: Could not complete conversion
    at com.documents4j.job.FailedConversionFuture.get(FailedConversionFuture.java:35)
    at com.documents4j.job.FailedConversionFuture.get(FailedConversionFuture.java:10)
    at com.documents4j.job.AbstractFutureWrappingPriorityFuture.get(AbstractFutureWrappingPriorityFuture.java:205)
    at com.documents4j.job.AbstractFutureWrappingPriorityFuture.get(AbstractFutureWrappingPriorityFuture.java:10)
    at com.example.demo.DemoApplication.main(DemoApplication.java:31)
Caused by: com.documents4j.throwables.ConverterAccessException: The converter seems to be shut down
    at com.documents4j.util.Reaction$ConverterAccessExceptionBuilder.make(Reaction.java:117)
    at com.documents4j.util.Reaction$ExceptionalReaction.apply(Reaction.java:75)
    at com.documents4j.conversion.ExternalConverterScriptResult.resolve(ExternalConverterScriptResult.java:69)
    at com.documents4j.conversion.ProcessFutureWrapper.evaluateExitValue(ProcessFutureWrapper.java:48)
    at com.documents4j.conversion.ProcessFutureWrapper.get(ProcessFutureWrapper.java:36)
    at com.documents4j.conversion.ProcessFutureWrapper.get(ProcessFutureWrapper.java:11)
    at com.documents4j.job.AbstractFutureWrappingPriorityFuture.run(AbstractFutureWrappingPriorityFuture.java:78)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.base/java.lang.Thread.run(Thread.java:844)

Solution

  • documents4j is a converting documents via a bridge to MS Word. If you do not have an instance running, this will not work, unfortunately.