[JAVA] Document conversion in OpenOffice

Introduction

You may want to convert a document to another format document. For example, I want to convert Excel to PDF. For PC, Excel in Office can easily convert other formats by saving them. image.png

If you want to convert automatically by a program instead of user operation, you can do it by using Openoffice.

OpenOffice installation

image.png

command:

sudo su -

wget https://sourceforge.net/projects/openofficeorg.mirror/files/4.1.6/binaries/ja/Apache_OpenOffice_4.1.6_Linux_x86-64_install-rpm_ja.tar.gz

tar -xvzf Apache_OpenOffice_4.1.6_Linux_x86-64_install-rpm_ja.tar.gz

cd ja/RPMS

rpm -Uvih *rpm

#Installation confirmation
ll /opt/openoffice4

#Create a symbol link
ln -s /opt/openoffice4/program/soffice /usr/local/bin/soffice

#Japanese font installation
sudo yum install ipa-gothic-fonts ipa-pgothic-fonts
sudo fc-cache -fv

Start OpenOffice

# en_US.UTF-If it is 8, Japanese characters are garbled, so set Japanese
export LANG=ja_JP.UTF-8

#Start-up
nohup soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

#Start confirmation
ps aux | grep soffice

Convert with command

This is a sample to convert testfile.txt to PDF file. soffice --headless --convert-to pdf testfile.txt

Programmatically convert

Library

Convert with a library called jodconverter jodconverter:https://github.com/sbraconnier/jodconverter/

Library introduction

// https://mvnrepository.com/artifact/org.jodconverter/jodconverter-local
compile group: 'org.jodconverter', name: 'jodconverter-local', version: '4.2.2'

Conversion sample

DocConverter.java


import java.io.File;

import org.jodconverter.DocumentConverter;
import org.jodconverter.LocalConverter;
import org.jodconverter.office.LocalOfficeManager;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;

public class DocConverter {

	public static void main(String[] args) throws OfficeException {

		OfficeManager officeManager = LocalOfficeManager.make();
		DocumentConverter converter = LocalConverter.make(officeManager);
		try {
			officeManager.start();

			File inputFile = new File("/data/test.xlsx");
			File outputFile = new File("/data/test.pdf");
			// Convert...
			converter.convert(inputFile).to(outputFile).execute();
		} finally {
			officeManager.stop();
		}
	}
}

Liferay can convert documents just by setting

Control Panel ⇒ Settings ⇒ Server Management ⇒ External Services image.png

URL:https://www.openoffice.org/

that's all

Recommended Posts

Document conversion in OpenOffice
[Easy-to-understand explanation! ] Reference type type conversion in Java
Output Notes document as XML document in Java
How to do base conversion in Java
Java creates a table in a Word document
Immutable (immutable) List object conversion function in Java8
Hex and UIColor mutual conversion in Swift