Launch Docker from Java to convert Office documents to PDF

1.First of all

2. Install Docker

Install Docker on CentOS. See official page for details https://docs.docker.com/engine/installation/linux/docker-ce/centos/

yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum makecache fast
yum install docker-ce
systemctl enable docker
systemctl start docker

3. Docker settings

(1). Build an image of CentOS7

docker pull centos:centos7
docker run -it centos:centos7 /bin/bash

(2). Introduce LibreOffice and IPA font

yum update -y
yum install libreoffice* ipa-*-fonts

(3). Create a working folder

mkdir /tmp/
exit

(4). Check the Docker CONTAINER ID

docker ps -a

(5). Commit with the name "office" Enter the CONTAINER ID confirmed earlier in * $ {CONTAINER ID} *.

docker commit ${CONTAINER ID} office

(6). Check if it works properly It is assumed that the "/var/tmp/test.xlsx" file exists on CentOS. If executed normally, "/var/tmp/test.pdf" will be generated.

docker run --rm=true -it -v /var/tmp:/tmp office libreoffice --headless --nologo --nofirststartwizard --convert-to pdf --outdir /tmp /tmp/test.xlsx

4. Java coding

This is a sample to start docker with java process class.


	/**
	 *Convert PDF via Docker
	 * @param excelFileName
	 * @param pdfFileName
	 * @return
	 * @throws InterruptedException
	 */
    public boolean convertPDF(String excelFileName) throws InterruptedException {

		//Start command specification
		String[] command = {
				"docker", "run", "--rm=true", "-v", "/var/tmp:/tmp", "office", "libreoffice", "--headless"
                , "--nologo", "--nofirststartwizard", "--convert-to", "pdf", "--outdir"
                ,"/tmp", "/tmp/"+excelFileName };

        ProcessBuilder pb = new ProcessBuilder(command);

		try {
            //Command execution
			Process process = pb.start();

            //Wait for the result of command execution
			int ret = process.waitFor();

            //Standard output
			InputStream is = process.getInputStream();	
			printInputStream(is);

            //Standard error
			InputStream es = process.getErrorStream();	
			printInputStream(es);

		}
		catch (Exception e) {
			return false;

		}

		return true;
    }
    
	/**
	 *Display standard error output
	 * @param is
	 * @throws IOException
	 */
    public static void printInputStream(InputStream is) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		try {
			for (;;) {
				String line = br.readLine();
				if (line == null) break;
				System.out.println(line);
			}
		} finally {
			br.close();
		}
	}

Recommended Posts

Launch Docker from Java to convert Office documents to PDF
Java applications convert Word (DOC / DOCX) documents to PDF
Convert from java UTC time to JST time
Java adds page numbers to existing PDF documents
[Java] Convert PDF version
Changes from Java 8 to Java 11
Sum from Java_1 to 100
From Java to Ruby !!
Convert Java enum enums and JSON to and from Jackson
Migration from Cobol to JAVA
Convert Java Powerpoint to XPS
Java adds table to PDF
New features from Java7 to Java8
Migrating from vargrant to docker
Connect from Java to PostgreSQL
Convert from ○ months to ○ years ○ months
How to convert Java radix
Using Docker from Java Gradle
[Java] Convert ArrayList to array
From Ineffective Java to Effective Java
[Java] How to convert a character string from String type to byte type
Java to extract PDF text content
Java adds hyperlinks to Word documents
[Java] Convert 1-to-N List to Map
Java to be involved from today
From Java to VB.NET-Writing Contrast Memo-
Java, interface to start from beginner
Add watermark to Java to PDF document
The road from JavaScript to Java
[Java] How to convert from String to Path type and get the path
[Android] Convert Android Java code to Kotlin
Java draws shapes in PDF documents
[Java] Conversion from array to List
Update MySQL from 5.7 to 8.0 with Docker
Java adds form fields to PDF
[Java] Convert array to ArrayList * Caution
Investigate the replacement from Docker to Podman.
[Rails] How to convert from erb to haml
Run Embulk on Docker to convert files
Convert all Android apps (Java) to Kotlin
[Java] Convert Object type null to String type
Connect from Java to MySQL using Eclipse
Convert SVG files to PNG files in Java
[Docker] Copy files from docker container to host
From installing Eclipse to executing Java (PHP)
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
[Java] Flow from source code to execution
Convert from color temperature to RGB (sRGB)
Introduction to monitoring from Java Touching Prometheus
Precautions when migrating from VB6.0 to JAVA
Add footnotes to Word documents in Java
Memo for migration from java to kotlin
Type conversion from java BigDecimal type to String type
Sample to create PDF from Excel with Ruby
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
[Java] From two Lists to one array list
Upsert from Java SDK to Azure Cosmos DB
Convert from C String pointer to Swift String type
Run R from Java I want to run rJava
Connect to Aurora (MySQL) from a Java application