Java使用libreoffice实现office文件转换成pdf格式,支持windows和linux
2018-01-29 21:19
811 查看
最近写了个使用libreoffice软件转换office文件为pdf的小功能,支持word,Excel,PPT,图片,文本文件转换pdf,还有别的啥的没测过,Excel转换效果不是很好,要是有改进再更。下面是源码import org.apache.commons.io.FileUtils;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
/**
* @author lbh
*/
public class Test3 {
private static Logger logger = LoggerFactory.getLogger(Test3.class);
/**
* 打开libreOffice服务的方法
*
* @return
*/
public String getLibreOfficeHome() {
String osName = System.getProperty("os.name");
if (Pattern.matches("Linux.*", osName)) {
//获取linux系统下libreoffice主程序的位置
logger.info("获取Linux系统LibreOffice路径");
return "/opt/libreoffice 5/program/soffice";
} else if (Pattern.matches("Windows.*", osName)) {
//获取windows系统下libreoffice主程序的位置
logger.info("获取windows系统LibreOffice路径");
return "C:\\Program Files\\LibreOffice 5";
}
return null;
}
/**
* 转换libreoffice支持的文件为pdf
* @param inputfile
* @param outputfile
*/
public void libreOffice2PDF(File inputfile, File outputfile) {
String LibreOffice_HOME = getLibreOfficeHome();
String fileName = inputfile.getName();
logger.info(new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date()) + "文件" + inputfile.getName());
System.out.println(fileName.substring(fileName.lastIndexOf(".")));
if (fileName.substring(fileName.lastIndexOf(".")).equalsIgnoreCase(".txt")) {
System.out.println("处理txt文件");
new Test3().TXTHandler(inputfile);
}
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
// libreOffice的安装目录
configuration.setOfficeHome(new File(LibreOffice_HOME));
// 端口号
configuration.setPortNumber(8100);
configuration.setTaskExecutionTimeout(1000 * 60 * 25L);
// 设置任务执行超时为10分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);
// 设置任务队列超时为24小时
OfficeManager officeManager = configuration.buildOfficeManager();
officeManager.start();
logger.info(new Date().toString() + "开始转换......");
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.getFormatRegistry();
try {
converter.convert(inputfile, outputfile);
} catch (Exception e) {
e.printStackTrace();
logger.info("转换失败");
} finally {
officeManager.stop();
}
logger.info(new Date().toString() + "转换结束....");
}
/**
* 测试的方法
* @param args
*/
public static void main(String[] args) {
//使用Files类遍历图片文件夹的文件
Path path = Paths.get("web/WEB-INF/temporary/Picture_format");
try {
DirectoryStream<Path> paths = Files.newDirectoryStream(path);
for (Path p : paths) {
File f = p.toFile();
System.out.println(path.getParent().toString() + "/pdf/" + f.getName().substring(0, f.getName().lastIndexOf(".")) + ".pdf");
new Test3().libreOffice2PDF(f, new File(path.getParent().toString() + "/pdf/" + f.getName().substring(0, f.getName().lastIndexOf(".")).toString() + ".pdf"));
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 转换txt文件编码的方法
*
* @param file
* @return
*/
public File TXTHandler(File file) {
//或GBK
String code = "gb2312";
byte[] head = new byte[3];
try {
InputStream inputStream = new FileInputStream(file);
inputStream.read(head);
if (head[0] == -1 && head[1] == -2) {
code = "UTF-16";
} else if (head[0] == -2 && head[1] == -1) {
code = "Unicode";
} else if (head[0] == -17 && head[1] == -69 && head[2] == -65) {
code = "UTF-8";
}
inputStream.close();
System.out.println(code);
if (code.equals("UTF-8")) {
return file;
}
String str = FileUtils.readFileToString(file, code);
FileUtils.writeStringToFile(file, str, "UTF-8");
System.out.println("转码结束");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
}
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
/**
* @author lbh
*/
public class Test3 {
private static Logger logger = LoggerFactory.getLogger(Test3.class);
/**
* 打开libreOffice服务的方法
*
* @return
*/
public String getLibreOfficeHome() {
String osName = System.getProperty("os.name");
if (Pattern.matches("Linux.*", osName)) {
//获取linux系统下libreoffice主程序的位置
logger.info("获取Linux系统LibreOffice路径");
return "/opt/libreoffice 5/program/soffice";
} else if (Pattern.matches("Windows.*", osName)) {
//获取windows系统下libreoffice主程序的位置
logger.info("获取windows系统LibreOffice路径");
return "C:\\Program Files\\LibreOffice 5";
}
return null;
}
/**
* 转换libreoffice支持的文件为pdf
* @param inputfile
* @param outputfile
*/
public void libreOffice2PDF(File inputfile, File outputfile) {
String LibreOffice_HOME = getLibreOfficeHome();
String fileName = inputfile.getName();
logger.info(new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date()) + "文件" + inputfile.getName());
System.out.println(fileName.substring(fileName.lastIndexOf(".")));
if (fileName.substring(fileName.lastIndexOf(".")).equalsIgnoreCase(".txt")) {
System.out.println("处理txt文件");
new Test3().TXTHandler(inputfile);
}
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
// libreOffice的安装目录
configuration.setOfficeHome(new File(LibreOffice_HOME));
// 端口号
configuration.setPortNumber(8100);
configuration.setTaskExecutionTimeout(1000 * 60 * 25L);
// 设置任务执行超时为10分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);
// 设置任务队列超时为24小时
OfficeManager officeManager = configuration.buildOfficeManager();
officeManager.start();
logger.info(new Date().toString() + "开始转换......");
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.getFormatRegistry();
try {
converter.convert(inputfile, outputfile);
} catch (Exception e) {
e.printStackTrace();
logger.info("转换失败");
} finally {
officeManager.stop();
}
logger.info(new Date().toString() + "转换结束....");
}
/**
* 测试的方法
* @param args
*/
public static void main(String[] args) {
//使用Files类遍历图片文件夹的文件
Path path = Paths.get("web/WEB-INF/temporary/Picture_format");
try {
DirectoryStream<Path> paths = Files.newDirectoryStream(path);
for (Path p : paths) {
File f = p.toFile();
System.out.println(path.getParent().toString() + "/pdf/" + f.getName().substring(0, f.getName().lastIndexOf(".")) + ".pdf");
new Test3().libreOffice2PDF(f, new File(path.getParent().toString() + "/pdf/" + f.getName().substring(0, f.getName().lastIndexOf(".")).toString() + ".pdf"));
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 转换txt文件编码的方法
*
* @param file
* @return
*/
public File TXTHandler(File file) {
//或GBK
String code = "gb2312";
byte[] head = new byte[3];
try {
InputStream inputStream = new FileInputStream(file);
inputStream.read(head);
if (head[0] == -1 && head[1] == -2) {
code = "UTF-16";
} else if (head[0] == -2 && head[1] == -1) {
code = "Unicode";
} else if (head[0] == -17 && head[1] == -69 && head[2] == -65) {
code = "UTF-8";
}
inputStream.close();
System.out.println(code);
if (code.equals("UTF-8")) {
return file;
}
String str = FileUtils.readFileToString(file, code);
FileUtils.writeStringToFile(file, str, "UTF-8");
System.out.println("转码结束");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
}
相关文章推荐
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- java中使用libreoffice将word转换成pdf格式
- 使用java实现windows与linux之间的文件传输
- jacob 实现Office Word文件格式转换 docTotxt,docTopdf,docxTodoc,pdfTotxt ...
- (三)java实现文档的在线浏览-使用JODConverter将office文档转换为pdf
- 使用jacob调用Windows的com对象,转换Office文件为pdf、html等
- 使用java实现windows与linux文件互相传递
- 使用Linux命令行将txt格式文件转换成pdf
- jacob 实现Office Word文件格式转换 docTotxt,docTopdf,docxTodoc,pdfTotxt ...
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- Office技巧:CAD转换PDF格式文件使用方法
- c#实现word,excel转pdf代码及部分Office 2007文件格式转换为xps和pdf代码整理
- windows下脚本转到linux下,文件保存格式要转换
- 如何在DOS/Windows和Linux/Unix之间进行文件格式转换
- java仿百度文库-使用SWFTools转换pdf文件
- 识别常见编码格式文件并转换成UTF-8编码 的java实现 源码
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表
- Linux下使用samba服务器实现与Windows的目录文件共享
- 使用JSmooth将java程序转换成windows上的.exe文件(彻底解决相对目录问题)
- 利用OpenOffice.org和Swftools进行实现—doc-->pdf-->swf--格式的转换