

文档在线预览(二)word、pdf文件转html以实现文档在线预览 - 知北游z
source link: https://www.cnblogs.com/fhey/p/17442536.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

文档在线预览(二)word、pdf文件转html以实现文档在线预览
实现文档在线预览的方式除了上篇文章《文档在线预览(一)通过将txt、word、pdf转成图片实现在线预览功能》说的将文档转成图片的实现方式外,还有转成pdf,前端通过pdf.js、pdfobject.js等插件来实现在线预览,以及本文将要说到的将文档转成html的方式来实现在线预览。代码基于 aspose-words(用于word转html),pdfbox(用于pdf转html),所以事先需要在项目里下面两个依赖:
<dependency>
<groupId>com.luhuiguo</groupId>
<artifactId>aspose-words</artifactId>
<version>23.1</version></dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.4</version>
</dependency>
一、将文件转换成html字符串
1、将word文件转成html字符串
public static String wordToHtmlStr(String wordPath) {
try {
Document doc = new Document(wordPath); // Address是将要被转化的word文档
String htmlStr = doc.toString();
return htmlStr;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
验证结果:

2、将pdf文件转成html字符串
public static String pdfToHtmlStr(String pdfPath) throws IOException, ParserConfigurationException {
PDDocument document = PDDocument.load(new File(pdfPath));
Writer writer = new StringWriter();
new PDFDomTree().writeText(document, writer);
writer.close();
document.close();
return writer.toString();
}
验证结果:

二、将文件转换成html,并生成html文件
有时我们是需要的不仅仅返回html字符串,而是需要生成一个html文件这时应该怎么做呢?一个改动量小的做法就是使用org.apache.commons.io包下的FileUtils工具类写入目标地址:
FileUtils类将html字符串生成html文件示例:
首先需要引入pom:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
相关代码:
String htmlStr = FileConvertUtil.pdfToHtmlStr("D:\\书籍\\电子书\\小说\\历史小说\\最后的可汗.doc");
FileUtils.write(new File("D:\\test\\doc.html"), htmlStr, "utf-8");
除此之外,还可以对上面的代码进行一些调整,已实现生成html文件,代码调整如下:
1、将word文件转换成html文件
public static void wordToHtml(String wordPath, String htmlPath) {
try {
File sourceFile = new File(wordPath);
String path = htmlPath + File.separator + sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf(".")) + ".html";
File file = new File(path); // 新建一个空白pdf文档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(wordPath); // Address是将要被转化的word文档
HtmlSaveOptions options = new HtmlSaveOptions();
options.setExportImagesAsBase64(true);
options.setExportRelativeFontSize(true);
doc.save(os, options);
} catch (Exception e) {
e.printStackTrace();
}
}
验证结果:

2、将pdf文件转换成html文件
public static void pdfToHtml(String pdfPath, String htmlPath) throws IOException, ParserConfigurationException {
File file = new File(pdfPath);
String path = htmlPath + File.separator + file.getName().substring(0, file.getName().lastIndexOf(".")) + ".html";
PDDocument document = PDDocument.load(new File(pdfPath));
Writer writer = new PrintWriter(path, "UTF-8");
new PDFDomTree().writeText(document, writer);
writer.close();
document.close();
}
图片版PDF文件验证结果:

文字版PDF文件验证结果:

Recommend
-
13
Vue+element-ui文件上传阿里云oss+预览word 、excel、ppt、pdf、png、jpg ...
-
7
Spring Boot的上传文件,相信你一定会了。如果还不会的小伙伴,可以先看看之前的分享: 文件上传实现之后,通常最常见的另外两个操作就是下载和预览,下载只需要知道地址,就简单搞定了,那么预览怎么做?你知道吗? 今天小编就来推荐一个用Spri...
-
5
如何用 Java 实现 word、excel 等文档在线预览?发布于 36 分钟前java实现办公文件在线预览功能是一个大家在工作中也许会遇到的需求,网上些公司专门提供这样的服务...
-
8
文件文档在线预览-kkFileView – 开源派 kkFileView是国人开源的一款文件文档在线预览项目,支持主流办公文档的在线预览,如 doc、docx、Excel、pdf、txt、zip、rar、 图片等格式。项目基于spring boot搭建,遵守Apache 2.0开源协议...
-
7
本文是js文件导出系列第二期,主要讲讲如何在线导出pdf文件,及office文件如何在线预览。目前前端实现这些技术已经比较成熟,都有比较成熟的技术库,下面来和大家一起过一下吧。 纯前端导出pdf 这里就不造轮子了,说一说纯前端导出pdf的几个流...
-
7
Asp.Net在线预览Word文档的解决方案与思路 前几天有个老项目找到我,有多老呢?比我工作年...
-
12
React 实现 PDF 文件在线预览 - 手把手教你写 React PDF 预览功能Wayne前端工程师最近更新 2022年06月02日
-
8
文档在线预览(三)使用js前端实现word、excel、pdf、ppt 在线预览 ...
-
4
最近因为工作需求,调研了实现文档在线预览功能的方式,总结了一下,大概的实现方式由后端统一转成图片或者pdf或者html返回前端,再由前端进行展示。还有就是前端根据不同文件类型使用对应的在线预览组件进行在线预览。因为每种实现方式的内容都比较多,限于篇幅...
-
6
虚拟机设计团队把类加载阶段中的“通过一个类的全限定名来获取描述此类的二进制字节流”这个...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK