网站首页 > 技术文章 正文
实现文档在线预览的方式除了上篇文章《文档在线预览(一)通过将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文件验证结果:
猜你喜欢
- 2024-10-06 ?? JavaScript提取PDF图片 ?? js 导出pdf
- 2024-10-06 VUE前端编程:PDF插件填坑记 vue-to-pdf
- 2024-10-06 把HTML转成PDF的4个方案及实现方法
- 2024-10-06 使用reveal.js制作精美的网页版PPT
- 2024-10-06 硬核!《web前端开发规范手册》,高清版 PDF 开放下载,拿走不谢
- 2024-10-06 让 PDF文档看起来像扫描的一样 pdf变成扫描版
- 2024-10-06 阿里架构师花近十年时间整理出来的前端核心知识pdf(前端岗)
- 2024-10-06 一个解决支持HTML/CSS/JS网页转PDF(高质量)的终极解决方案
- 2024-10-06 基于springboot的多格式转PDF springboot word转pdf
- 2024-10-06 Java后端实现HTML网页报表导出pdf方案
你 发表评论:
欢迎- 501℃几个Oracle空值处理函数 oracle处理null值的函数
- 499℃Oracle分析函数之Lag和Lead()使用
- 495℃Oracle数据库的单、多行函数 oracle执行多个sql语句
- 482℃0497-如何将Kerberos的CDH6.1从Oracle JDK 1.8迁移至OpenJDK 1.8
- 478℃Oracle 12c PDB迁移(一) oracle迁移到oceanbase
- 474℃【数据统计分析】详解Oracle分组函数之CUBE
- 455℃Oracle有哪些常见的函数? oracle中常用的函数
- 452℃最佳实践 | 提效 47 倍,制造业生产 Oracle 迁移替换
- 最近发表
-
- Directus 火了!无代码 SQL 数据的协作应用利器!
- PHP和NodeJS的代码执行效率比较(php和nodejs的区别)
- 工商银行获得发明专利授权:“基于数据库映射动态接口的前端页面应用开发方法及装置”
- FAISS和Chroma:两种流行的向量数据库的比较
- 什么是数据库的索引?(数据库索引的定义和作用)
- Vercel和Neon“首次”推出用于前端云的无服务器SQL数据库
- 记一次前端逻辑绕过登录到内网挖掘
- 学Access好还是MySQL好?(access与mysql的语句区别)
- 惬意!清晨慢品 HTML canvas 标签题,面试知识轻松 get
- 前端实现知识图谱-force(d3.js)(前端知识树)
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端react (48)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端富文本编辑器 (47)
- 前端路由 (55)
- 前端数组 (65)
- 前端定时器 (47)
- Oracle RAC (73)
- oracle恢复 (76)
- oracle 删除表 (48)
- oracle 用户名 (74)
- oracle 工具 (55)
- oracle 内存 (50)
- oracle 导出表 (57)
- oracle 中文 (51)
- oracle链接 (47)
- oracle的函数 (57)
- 前端调试 (52)
- 前端登录页面 (48)
本文暂时没有评论,来添加一个吧(●'◡'●)