惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 卡车司机

macOS系统下修改hosts文件,安装homebrew & nvm How to see log files in MySQL? git 设置和取消代理 使用本地下载和管理的免费 Windows 10 虚拟机测试 IE11 和旧版 Microsoft Edge 在Microsoft SQL SERVER Management Studio下如何完整输出NVARCHAR(MAX)字段或变量的内容 windows 10 x64系统下在vmware workstation pro 15安装macOS 10.15 Catelina, 并设置分辨率为3840x2160 在Windows 10系统下将Git项目签出到磁盘分区根目录的方法 群晖NAS(Synology NAS)环境下安装GitLab, 并在Windows 10环境下使用Git windows 10 专业版安装VMware虚拟机碰到的坑 PDF.js实现个性化PDF渲染(文本复制) Razor Page Library:开发独立通用RPL(内嵌wwwroot资源文件夹) 杂记 Code First Migrations in Team Environments SQL Server UPDATE JOIN visual studio 使用正则表达式实现代码批量查找和替换 AngularJs - Calling Directive Method from Controller Entity Framework Power Tools 执行数据库反向工程时报错.... SVN-无法查看log,提示Want to go offline,时间显示1970问题 windows server安装dotnet-sdk-2.2.108-win-x64.exe时报dll找不到
Java平台下利用aspose转word为PDF实现文档在线预览
卡车司机 · 2020-02-08 · via 博客园 - 卡车司机

2020-02-08 12:10  卡车司机  阅读(1723)  评论()    收藏  举报

原文链接: https://www.jianshu.com/p/86716c7122ef 

原始需求

java-web项目某页面,用户可上传word文档,上传后的文档名在页面的表格中展示,需要提供word文档的在线预览、下载功能。

找寻解决方式

需求点主要有三个,分别是word文件的上传、预览和下载,文件的上传和下载就不多说了,主要的难点在于预览。找资料找到了下面几种解决方式:

利用POI把word文档转换为html

现在的POI对于word文档处理是挺强大的,对于一些简单文档的处理问题不大,但是对于一些复杂文档的处理就有点心累了,而且用户上传的文档是相对比较复杂的文档,除了复杂的表格外还有很多图片,所以放弃这种方式。

利用微软或者Google的在线文档预览服务

这个方法也是不可行的,使用微软和Google的在线文档预览服务需要向他们提供word文档的链接,而客户的文件是需要保密,不能对外泄露的。

使用Apache OpenOffice的将word转换为PDF

Apache OpenOffice支持多种环境,而且是开源的,我没有使用这种方式,想了解的话可以参考:https://blog.csdn.net/a1786223749/article/details/79461070

使用apose将word转换为PDF

apose是一个很强大的office文档处理软件,可以完美的实现word文档转换为pdf文件,缺点就是软件是需要付费的。但是网上可以找到很多的破解版jar包,测试效果还是很不错的,而且使用简单,项目中使用的就是这种方式。

使用apose转换word为PDF

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * <b><code>Word2PdfUtil</code></b>
 * <p>
 * Description: word to pdf util.
 * <p>
 * <b>Creation Time:</b> 2018/5/15 15:10
 *
 * @author 
 * @date 2018 /5/15
 * @since JDK 1.7
 */
public class Word2PdfUtil {

    /**
     * The constant LOG.
     *
     */
    private static final Logger LOG = LoggerFactory.getLogger(Word2PdfUtil.class);

    /**
     * 获取license
     *
     * @return
     */
    private static boolean getLicense() {
        boolean result = false;
        try {
            // 凭证
            String licenseStr =
                    "<License>\n" +
                    "  <Data>\n" +
                    "    <Products>\n" +
                    "      <Product>Aspose.Total for Java</Product>\n" +
                    "      <Product>Aspose.Words for Java</Product>\n" +
                    "    </Products>\n" +
                    "    <EditionType>Enterprise</EditionType>\n" +
                    "    <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
                    "    <LicenseExpiry>20991231</LicenseExpiry>\n" +
                    "    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
                    "  </Data>\n" +
                    "  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
                    "</License>";
            InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));
            License asposeLic = new License();
            asposeLic.setLicense(license);
            result = true;
        } catch (Exception e) {
            LOG.error("error:", e);
        }
        return result;
    }

    /**
     * Word 2 pdf.
     *
     * @param multipartFile the multipart file
     * @param pdfFilePath   the pdf file path
     */
    public static void word2Pdf(MultipartFile multipartFile, String pdfFilePath) {
        FileOutputStream fileOS = null;
        // 验证License
        if (!getLicense()) {
            LOG.error("验证License失败!");
            return;
        }
        try {
            Document doc = new Document(multipartFile.getInputStream());
            fileOS = new FileOutputStream(new File(pdfFilePath));
            // 保存转换的pdf文件
            doc.save(fileOS, SaveFormat.PDF);
        } catch (Exception e) {
            LOG.error("error:", e);
        } finally {
            try {
                if(fileOS != null){
                    fileOS.close();
                }
            } catch (IOException e) {
                LOG.error("error:", e);
            }
        }
    }
}  

apose的Jar包和maven-pom.xml

链接:https://pan.baidu.com/s/19clf3JPKMkr_O9uUFF8C0Q 密码:bbu0

前端预览PDF文件

关于前端预览pdf文件可以使用pdf.js预览。另推荐一个Angular2+的PDF预览插件:https://github.com/VadimDez/ng2-pdf-viewer

遇到的问题

WORD转PDF功能在windows下转换正常,Linux下中文乱码

因为转换时需要系统里有word文档里使用的字体,当找不到该字体的时候就会乱码。
Linux下默认只有很少的中文字体,所以很可能会出现中文乱码问题。
解决办法也很简单,只需在Linux系统中添加常用的字体即可。

1. 查看系统支持的字体
fc-list

2. 查看系统支持的中文字体
fc-list :lang=zh

3. 创建字体目录(需要root权限,linux系统默认的字体存放路径为/usr/share/fonts,没有可自行创建)
cd /usr/share/fonts
mkdir windows

4. cp字体文件(将需要新增的字体文件拷贝到指定目录中)
cd windows
cp ~/fonts/* ./

5. 目录和字体文件设置为所有用户可见
chmod 755 ../windows
chmod 755 ./*

6. 应用字体

mkfontscale (如果提示 mkfontscale: command not found,需自行安装 # yum install mkfontscale )
mkfontdir
fc-cache -fv (如果提示 fc-cache: command not found,则需要安装# yum install fontconfig )

7.再次查看系统支持的字体
fc-list

作者:亓亓亓亓凌
链接:https://www.jianshu.com/p/86716c7122ef
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。