Java使用openOffice转PDF以及PDF文件预览乱码问题

1 篇文章 0 订阅
1 篇文章 0 订阅

Java使用openOffice转PDF以及PDF文件预览乱码问题

使用openOffice,支持doc, docx, .xls, .xlsx, .ppt, .pptx转pdf

一:依赖

 <dependency>
        <groupId>com.artofsolving</groupId>
        <artifactId>jodconverter</artifactId>
        <version>2.2.1</version>
  </dependency>

二:下载openOffice安装包(安装在文章后面)
https://www.openoffice.org/download/
windows:windows
LinuxLinux
三:代码


import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import lombok.extern.log4j.Log4j2;

import java.io.File;
import java.io.IOException;

/**
 * office转化为pdf
 */
@Log4j2
public class WordToPDFUtils {

    //openoffice软件的安装路径(linux默认安装路径)
    private static String openOfficePath = "/opt/openoffice4";

    public static void officeToPDF(String sourceFile, String destFile) {
        try {
            File inputFile = new File(sourceFile);
            if (!inputFile.exists()) {
               log.error("找不到源文件");
               throw new GeneralException("找不到源文件");
            }
            File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            String OpenOffice_HOME = openOfficePath;
            if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {
                OpenOffice_HOME += "\\";
            }
            // 连接OpenOffice的服务  安装之后默认ip和端口
            String command = OpenOffice_HOME
                    + "soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(
                    "127.0.0.1", 8100);
            connection.connect();
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
            converter.convert(inputFile, outputFile);
            connection.disconnect();
            return 0;
        } catch (Exception e) {
        	log.error("转换失败:{}"+e.getMessage());
            e.printStackTrace();
        }
        log.info("转换成功");
    }
}

Linux 环境中文乱码问题

首先解释乱码问题,因为在Linux环境缺少Windows字体集
一:安装(Linux7.0+openOffice)
1 上传安装包到安装目录
在这里插入图片描述
2 解压

tar -zxvf Apache_OpenOffice_4.1.6_Linux_x86-64_install-rpm_zh-CN.tar.gz 

解压之后生成 zh-CN 目录
3 进入zh-CN目录下的RPMS
运行

yum localinstall *.rpm

成功之后会在当前目录生成 desktop-integration
4 运行

yum localinstall openoffice-4.1.6-9790.x86_64.rpm

成功之后会在/opt目录下面生成openOffice4目录
在这里插入图片描述
启动openOffice

nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

查看进程

ps -A|grep soffice

乱码问题重点(字体集.TTF和.TTC)
1 查看系统字体文件目录

cat /etc/fonts/fonts.conf

在这里插入图片描述
2 将windows系统的C:\WINDOWS\Fonts字体拷贝到Linux/usr/share/fonts/win目录下面(在fonts目录下面新建win目录)
3 权限

chmod 755 /usr/share/fonts/win/

4 在win目录下面执行

mkfontscale 
mkfontdir
fc-cache(建立缓存信息)

5 安装完字体文件后openoffice一定要重启,不需要重启linux,否则字体不生效

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值