Skip to content

Instantly share code, notes, and snippets.

@toshimasa-nanaki
Last active June 20, 2022 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toshimasa-nanaki/89b79648f205c505810c2431df3d2827 to your computer and use it in GitHub Desktop.
Save toshimasa-nanaki/89b79648f205c505810c2431df3d2827 to your computer and use it in GitHub Desktop.
JODConverter4.1.0 PDF出力テストコード
plugins {
id 'war'
}
sourceCompatibility = 17
tasks.withType(JavaCompile) {options.encoding = 'UTF-8'}
repositories.mavenCentral()
dependencies {
implementation group: 'org.jodconverter', name: 'jodconverter-local', version: '4.4.2'
}
package jp.example;
import java.io.File;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.OfficeException;
import org.jodconverter.core.office.OfficeManager;
import org.jodconverter.local.LocalConverter;
import org.jodconverter.local.office.LocalOfficeManager;
public class LibrePdfOutputTest {
// 変換前のExcelファイルの配置場所
public static final String SRC_FILE_PATH = "/Users/toshimasa/temp/test.xlsx";
// 変換後のPDFファイルの出力場所
public static final String DEST_FILE_PATH = "/Users/toshimasa/temp/test.pdf";
public static void main(String[] args) throws OfficeException {
File srcFile = new File(SRC_FILE_PATH);
File destFile = new File(DEST_FILE_PATH);
OfficeManager officeManager = LocalOfficeManager.make();
DocumentConverter converter = LocalConverter.make(officeManager);
/**
* Note
* このコードはテスト用のため一回一回LibreOfficeを上げ下げしています。
* 実際にWeb Applicationのリクエスト経由などで利用する場合は、
* ・Web Applicationの起動時にstart
* ・Web Applicationの終了時にstop
* とした方が効率的です。
* 参照:https://github.com/sbraconnier/jodconverter/wiki/Web-Application
*/
officeManager.start();
converter.convert(srcFile).to(destFile).execute();
officeManager.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment