Java利用libreOffice(jodconverter)将office(ppt,Excel,word,text)文档转换成pdf

//使用libreOffice比使用OpenOffice转换稳定,libreOffice是OpenOffice的升级版本,但也不能完全盖晗OpenOffice,下面代码注释的部分即为OpenOffice将文档转换成PDF,使用libreOffice/OpenOffice都必须安装对应系统的libreOffice/OpenOffice软件。

package com.frank.demo.file.common.util;

 


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;


import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Component;


import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
import com.mongodb.gridfs.GridFSDBFile;


@Component
public class FlieToPdfUtilTwo {


@Autowired
private GridFsTemplate fileRepository;


// private static String openOfficePath = "C:\\Program Files
// (x86)\\OpenOffice 4"; // openoffice软件的安装默认路径
private static String libreOfficePath = "C:\\Program Files\\LibreOffice 5"; // libreOffice软件的安装默认路径
// 记录日志信息
private static Logger logger = LoggerFactory.getLogger(FlieToPdfUtilTwo.class);


public ByteArrayOutputStream fileToPdf(String fileId) throws Exception {
GridFSDBFile gridFSDBFile = fileRepository.findOne(new Query(Criteria.where("_id").is(fileId)));
if (null != gridFSDBFile) {
String type = this.getFileSufix(gridFSDBFile.getFilename());
String folder = System.getProperty("java.io.tmpdir");
String docs[] = { "doc", "docx", "ppt", "pptx", "xls", "xlsx", "t

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
可以使用Java中的Uno API来实现LibreOfficeOffice文档转换PDF的功能。具体实现步骤如下: 1. 首先需要安装LibreOffice,并启动LibreOffice服务。启动命令为: ``` soffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" ``` 2. 在Java中使用Uno API连接LibreOffice服务,代码示例如下: ``` XComponentContext xContext = Bootstrap.bootstrap(); XMultiComponentFactory xMCF = xContext.getServiceManager(); Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext); XComponentLoader xCompLoader = UnoRuntime.queryInterface(XComponentLoader.class, oDesktop); ``` 3. 加载需要转换Office文档,代码示例如下: ``` String inputUrl = "file:///path/to/input.docx"; PropertyValue[] loadProps = new PropertyValue[1]; loadProps[0] = new PropertyValue(); loadProps[0].Name = "Hidden"; loadProps[0].Value = Boolean.TRUE; XComponent xDoc = xCompLoader.loadComponentFromURL(inputUrl, "_blank", 0, loadProps); ``` 4. 将文档转换PDF格式,代码示例如下: ``` String outputUrl = "file:///path/to/output.pdf"; PropertyValue[] convertProps = new PropertyValue[2]; convertProps[0] = new PropertyValue(); convertProps[0].Name = "FilterName"; convertProps[0].Value = "writer_pdf_Export"; convertProps[1] = new PropertyValue(); convertProps[1].Name = "Overwrite"; convertProps[1].Value = Boolean.TRUE; XStorable xStore = UnoRuntime.queryInterface(XStorable.class, xDoc); xStore.storeToURL(outputUrl, convertProps); ``` 5. 最后需要关闭文档LibreOffice服务,代码示例如下: ``` xDoc.dispose(); System.exit(0); ``` 完整的代码示例如下: ``` import com.sun.star.comp.helper.Bootstrap; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XStorable; import com.sun.star.lang.XComponent; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XComponentContext; import com.sun.star.beans.PropertyValue; public class OfficeToPdfConverter { public static void main(String[] args) { try { // Connect to LibreOffice service XComponentContext xContext = Bootstrap.bootstrap(); XMultiComponentFactory xMCF = xContext.getServiceManager(); Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext); XComponentLoader xCompLoader = UnoRuntime.queryInterface(XComponentLoader.class, oDesktop); // Load input document String inputUrl = "file:///path/to/input.docx"; PropertyValue[] loadProps = new PropertyValue[1]; loadProps[0] = new PropertyValue(); loadProps[0].Name = "Hidden"; loadProps[0].Value = Boolean.TRUE; XComponent xDoc = xCompLoader.loadComponentFromURL(inputUrl, "_blank", 0, loadProps); // Convert document to PDF String outputUrl = "file:///path/to/output.pdf"; PropertyValue[] convertProps = new PropertyValue[2]; convertProps[0] = new PropertyValue(); convertProps[0].Name = "FilterName"; convertProps[0].Value = "writer_pdf_Export"; convertProps[1] = new PropertyValue(); convertProps[1].Name = "Overwrite"; convertProps[1].Value = Boolean.TRUE; XStorable xStore = UnoRuntime.queryInterface(XStorable.class, xDoc); xStore.storeToURL(outputUrl, convertProps); // Close document and exit xDoc.dispose(); System.exit(0); } catch (Exception e) { e.printStackTrace(); } } } ``` 需要注意的是,在使用Uno API连接LibreOffice服务时,需要在classpath中加入相应的jar包。具体jar包名称和路径可以参考LibreOffice的安装目录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追风落叶乔木生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值