最近需要做个文件上传,遇到office办公类文档需求需要能够在现预览,文件存储采用的是阿里云oss,但是阿里云在线预览功能收费比较贵,所以放弃了,因为谷歌支持PDF在线预览,所以改用生成PDF文档实现在线预览功能。
百度了一下实现office办公类文档转换成PDF文档的博客,收集到以下几种方案,在别人的博客中这样说道:
  
 
我综合对比了一下,咳咳,以最小成本为参考,最终选择两种方案openoffice和libreoffice,我在实际操作中,在用openoffice的时候出现了一些问题:
最后测试了libreoffice的转换功能,完美转换,其他的我没有测,暂时只是发现了这一个问题,但是解决了我目前所遇到的问题,也算有个交差了。
个人习惯用docker,所以接下来都是以docker的方式来安装
下载libreoffice
  
 
jdk官网
windows系统下的字体包
  
 
tar -zcvf chinese.tar.gz *
文件显示如下:
 
 
 #基于centos7版本镜像
FROM centos:7
#以下设置中文语言环境与修改时区
ENV LANG=zh_CN.UTF-8 /
        LANGUAGE=zh_CN:zh /
        LC_ALL=zh_CN.UTF-8
RUN yum update -y && /
        yum reinstall -y glibc-common && /
        yum install -y telnet net-tools && /
        yum clean all && /
        rm -rf /tmp/* rm -rf /var/cache/yum/* && /
        localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8 && /
        ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#加入windows字体包
ADD chinese.tar.gz /usr/share/fonts/
#将下载好的包解压到相应文件下
ADD LibreOffice_6.4.3_Linux_x86-64_rpm.tar.gz /home/
#执行安装
RUN cd /home/LibreOffice_6.4.3.2_Linux_x86-64_rpm/RPMS/ /
        && yum localinstall *.rpm -y /
        #安装依赖
        && yum install ibus -y /
        #加入中文字体支持并赋权限
        && cd /usr/share/fonts/ /
        && chmod -R 755 /usr/share/fonts /
        && yum install mkfontscale -y /
        && mkfontscale /
        && yum install fontconfig -y /
        && mkfontdir /
        && fc-cache -fv /
        && mkdir /usr/local/java/ /
        #清理缓存,减少镜像大小
        && yum clean all
#加入安装java环境
ADD jdk-8u161-linux-x64.tar.gz /usr/local/java/
RUN ln -s /usr/local/java/jdk1.8.0_161 /usr/local/java/jdk
#配置环境变量
ENV JAVA_HOME /usr/local/java/jdk
ENV JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH ${JAVA_HOME}/bin:$PATH
CMD ["bash"] 
 docker build -t libreoffice:v1 .
首先先登录到harbor仓库
docker login --username=zhangsan --password=123456 https://www.example.com docker tag libreoffice:v1 https://www.example.com/test/libreoffice:latest docker push https://www.example.com/test/libreoffice:latest
等待上传成功, https://www.example.com/test 为你私有镜像仓库地址,以及你项目前缀
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-core</artifactId>
    <version>4.2.0</version>
</dependency>
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-local</artifactId>
    <version>4.2.0</version>
</dependency>
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-spring-boot-starter</artifactId>
    <version>4.2.0</version>
</dependency>
 <dependency>
    <groupId>org.libreoffice</groupId>
    <artifactId>ridl</artifactId>
    <version>5.4.2</version>
</dependency> 
 jodconverter:
  local:
    enabled: true
    kill-existing-process: true
    max-tasks-per-process: 100
    office-home: @pom.office.home@
    portNumbers: 8100,8101,8102,8103,8104 
 <profiles>
        <profile>
            <!-- windows环境 -->
            <id>win</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <pom.office.home>C:/Program Files/LibreOffice</pom.office.home>
            </properties>
        </profile>
        <profile>
            <!-- linux环境 -->
            <id>linux</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <pom.office.home>/opt/libreoffice6.4</pom.office.home>
            </properties>
        </profile>
    </profiles> 
 import org.jodconverter.DocumentConverter; @Resource private DocumentConverter documentConverter; // 具体转换方法,参数是java.io.File documentConverter.convert(sourceFile).to(targetFile).execute();
新建Dockerfile放在src目录
FROMhttps://www.example.com/test/libreoffice:latest MAINTAINER zhouxinlei VOLUME /tmp EXPOSE 8901 ADD demo-1.0-SNAPSHOT.jar app.jar ENTRYPOINT ["java","-Xms360m","-Xmx360m","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar","--spring.profiles.active=prod"]
docker build -t demo:v1 .
docker run -d -p 8080:8901 -v /home/:/home/ demo:v1
  
 
  
 
测试成功
本次测试加开发。总耗时8个小时,也是自己走了一些坑,希望能为大家带来收获,少走一些坑。
下期为大家带来 itext 操作pdf文档,支持在线生成,智能填充,智能缩放字体,行间距的控制。下期满满的干货哦,都是自己操作PDF时候总结的算法。
本文由 凛冬王昭君 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: May 3,2020