Showing posts with label docx4j. Show all posts
Showing posts with label docx4j. Show all posts

Wednesday, January 10, 2024

Sample Word to PDF convert with docx4j

Below is sample application to convert Word file to Pdf using dox4j

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.poc.convert</groupId>
<artifactId>convert</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>convert</name>
<description>Poc Project to convert word to pdf</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- dox4j -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-Internal</artifactId>
<version>8.3.9</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>8.3.9</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-MOXy</artifactId>
<version>8.3.9</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>8.3.9</version>
</dependency>

<!--- ****** -->
<!-- API, java.xml.bind module -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.2</version>
</dependency>

<!-- java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException -->
<!-- Runtime, com.sun.xml.bind module -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
</dependency>

<!-- java.lang.NoClassDefFoundError: javax/activation/DataSource -->
<!-- Caused by: java.lang.ClassNotFoundException: javax.activation.DataSource-->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>

<!-- java.lang.NoClassDefFoundError: javax/mail/internet/MimeMultipart-->
<!-- Caused by: java.lang.ClassNotFoundException: javax.mail.internet.MimeMultipart-->

<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>

<!-- Documents4jLocalServices-->
<!-- https://mvnrepository.com/artifact/org.docx4j/docx4j-documents4j-local -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-documents4j-local</artifactId>
<version>11.4.9</version>
</dependency>

<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.5</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
</dependency>



<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


Above POM all dependencies 

Below Convert Class contains the Logic

--------------------------------------------------------------------------------------------------------


package com.poc.convert.convert;


import com.documents4j.api.DocumentType;

import org.docx4j.Docx4J;

import org.docx4j.documents4j.local.Documents4jLocalServices;

import org.docx4j.openpackaging.exceptions.Docx4JException;

import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


import java.io.*;


public class Convert {

    private static final Logger LOGGER = LoggerFactory.getLogger(Convert.class);


    public static Documents4jLocalServices exporter = new Documents4jLocalServices();


    private static volatile Convert instance;


    private Convert() {

    }


    public static Convert getInstance() {

        if (instance == null) {

            synchronized (Convert.class) {

                if (instance == null) {

                    instance = new Convert();

                }

            }

        }

        return instance;

    }


    /**

     * Convert word .docx format file to pdf file.

     *

     * @param inputWordFile Input word .docx file.

     * @param outputPdfFile Converted .pdf file.

     * @throws IOException     Exception when reading input word ,docx file or writing to .pdf output file

     * @throws Docx4JException Excpetion occcuring during converting word file to Pdf file.

     */

    public void convertWordDocxtoPdf(String inputWordFile, String outputPdfFile) throws IOException, Docx4JException {

        LOGGER.info("Start Converting Word {} file to {}", inputWordFile, outputPdfFile);

        InputStream templateInputStream = new FileInputStream(inputWordFile);

        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(templateInputStream);

        MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();


        FileOutputStream os = new FileOutputStream(outputPdfFile);

        Docx4J.toPDF(wordMLPackage, os);

        os.flush();

        os.close();

        LOGGER.info("Successfully converted {} to {}", inputWordFile, outputPdfFile);

    }


    public void convertWordDocxtoPdfWithLocalServer(String inputWordFile, String outputPdfFile) throws IOException, Docx4JException {

        LOGGER.info("Start Converting with local server Word {} file to {}", inputWordFile, outputPdfFile);

        try (FileOutputStream fos = new FileOutputStream(outputPdfFile)) {

            if (inputWordFile.toLowerCase().endsWith(".docx")) {

                exporter.export(new File(inputWordFile), fos, DocumentType.MS_WORD);

            }

            LOGGER.info("Start Flushing ***********");

            fos.flush();

            fos.close();

            LOGGER.info("End Flushing ***********");

//            System.exit(0);

        }

        LOGGER.info("Successfully converted with local server {} to {}", inputWordFile, outputPdfFile);

    }


    public void convertWordDocxtoPdfWithLibre(String inputWordFile, String outputPdfFile) throws IOException {

        LOGGER.info("Start Converting Word with Libre {} file to {}", inputWordFile, outputPdfFile);


        String command = "libreoffice --headless --convert-to pdf " + inputWordFile + " --outdir " + outputPdfFile;

        Runtime.getRuntime().exec(command);


        LOGGER.info("Successfully converted with Libre {} to {}", inputWordFile, outputPdfFile);

    }

}

----------------------------------------------------------------------------------------------------------------


package com.poc.convert.convert;

import com.documents4j.api.DocumentType;
import org.docx4j.Docx4J;
import org.docx4j.documents4j.local.Documents4jLocalServices;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

public class Convert {
private static final Logger LOGGER = LoggerFactory.getLogger(Convert.class);

public static Documents4jLocalServices exporter = new Documents4jLocalServices();

private static volatile Convert instance;

private Convert() {
}

public static Convert getInstance() {
if (instance == null) {
synchronized (Convert.class) {
if (instance == null) {
instance = new Convert();
}
}
}
return instance;
}

/**
* Convert word .docx format file to pdf file.
*
* @param inputWordFile Input word .docx file.
* @param outputPdfFile Converted .pdf file.
* @throws IOException Exception when reading input word ,docx file or writing to .pdf output file
* @throws Docx4JException Excpetion occcuring during converting word file to Pdf file.
*/
public void convertWordDocxtoPdf(String inputWordFile, String outputPdfFile) throws IOException, Docx4JException {
LOGGER.info("Start Converting Word {} file to {}", inputWordFile, outputPdfFile);
InputStream templateInputStream = new FileInputStream(inputWordFile);
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(templateInputStream);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

FileOutputStream os = new FileOutputStream(outputPdfFile);
Docx4J.toPDF(wordMLPackage, os);
os.flush();
os.close();
LOGGER.info("Successfully converted {} to {}", inputWordFile, outputPdfFile);
}

public void convertWordDocxtoPdfWithLocalServer(String inputWordFile, String outputPdfFile) throws IOException, Docx4JException {
LOGGER.info("Start Converting with local server Word {} file to {}", inputWordFile, outputPdfFile);
try (FileOutputStream fos = new FileOutputStream(outputPdfFile)) {
if (inputWordFile.toLowerCase().endsWith(".docx")) {
exporter.export(new File(inputWordFile), fos, DocumentType.MS_WORD);
}
LOGGER.info("Start Flushing ***********");
fos.flush();
fos.close();
LOGGER.info("End Flushing ***********");
// System.exit(0);
}
LOGGER.info("Successfully converted with local server {} to {}", inputWordFile, outputPdfFile);
}

public void convertWordDocxtoPdfWithLibre(String inputWordFile, String outputPdfFile) throws IOException {
LOGGER.info("Start Converting Word with Libre {} file to {}", inputWordFile, outputPdfFile);

String command = "libreoffice --headless --convert-to pdf " + inputWordFile + " --outdir " + outputPdfFile;
Runtime.getRuntime().exec(command);

LOGGER.info("Successfully converted with Libre {} to {}", inputWordFile, outputPdfFile);
}
}


3. This is the default Springboot main classs
This class simply call the logic of the Convert class where takes InputFilePath and OutputFilePath as arguments

package com.poc.convert.convert;

import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;

@SpringBootApplication
public class ConvertApplication {

private static final Logger LOGGER = LoggerFactory.getLogger(ConvertApplication.class);

public static void main(String[] args) {
SpringApplication.run(ConvertApplication.class, args);

String inputWordFile = "";
String outputPdfFile = "";

if (args[0].length() > 0) {
inputWordFile = args[0];
} else {
LOGGER.info("Please define Input file");
}

if (args[1].length() > 0) {
outputPdfFile = args[1];
} else {
LOGGER.info("Please define Output file");
}


try {
Convert.getInstance().convertWordDocxtoPdf(inputWordFile, outputPdfFile);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (Docx4JException e) {
throw new RuntimeException(e);
}

}

}


With this you can execute Jar file with below command.
{space} --> empty space character, separate parts of the commands

1. command to execute jar
java {space} -jar {space} {name of the jar file} {space} {input file path} {output file path}

java -jar convert-0.0.1-SNAPSHOT.jar D:/sample.docx D:/sample.pdf