经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » HTML » 查看文章
文档在线预览(二)word、pdf文件转html以实现文档在线预览
来源:cnblogs  作者:知北游z  时间:2023/5/30 10:28:39  对本文有异议

@


实现文档在线预览的方式除了上篇文章《文档在线预览(一)通过将txt、word、pdf转成图片实现在线预览功能》说的将文档转成图片的实现方式外,还有转成pdf,前端通过pdf.js、pdfobject.js等插件来实现在线预览,以及本文将要说到的将文档转成html的方式来实现在线预览。代码基于 aspose-words(用于word转html),pdfbox(用于pdf转html),所以事先需要在项目里下面两个依赖:

  1. <dependency>
  2. <groupId>com.luhuiguo</groupId>
  3. <artifactId>aspose-words</artifactId>
  4. <version>23.1</version></dependency>
  5. <dependency>
  6. <groupId>org.apache.pdfbox</groupId>
  7. <artifactId>pdfbox</artifactId>
  8. <version>2.0.4</version>
  9. </dependency>

一、将文件转换成html字符串

1、将word文件转成html字符串

  1. public static String wordToHtmlStr(String wordPath) {
  2. try {
  3. Document doc = new Document(wordPath); // Address是将要被转化的word文档
  4. String htmlStr = doc.toString();
  5. return htmlStr;
  6. } catch (Exception e) {
  7. e.printStackTrace();
  8. }
  9. return null;
  10. }

验证结果:
请添加图片描述

2、将pdf文件转成html字符串

  1. public static String pdfToHtmlStr(String pdfPath) throws IOException, ParserConfigurationException {
  2. PDDocument document = PDDocument.load(new File(pdfPath));
  3. Writer writer = new StringWriter();
  4. new PDFDomTree().writeText(document, writer);
  5. writer.close();
  6. document.close();
  7. return writer.toString();
  8. }

验证结果:
请添加图片描述

二、将文件转换成html,并生成html文件

有时我们是需要的不仅仅返回html字符串,而是需要生成一个html文件这时应该怎么做呢?一个改动量小的做法就是使用org.apache.commons.io包下的FileUtils工具类写入目标地址:

FileUtils类将html字符串生成html文件示例:

首先需要引入pom:

  1. <dependency>
  2. <groupId>commons-io</groupId>
  3. <artifactId>commons-io</artifactId>
  4. <version>2.8.0</version>
  5. </dependency>

相关代码:

  1. String htmlStr = FileConvertUtil.pdfToHtmlStr("D:\\书籍\\电子书\\小说\\历史小说\\最后的可汗.doc");
  2. FileUtils.write(new File("D:\\test\\doc.html"), htmlStr, "utf-8");

除此之外,还可以对上面的代码进行一些调整,已实现生成html文件,代码调整如下:

1、将word文件转换成html文件

  1. public static void wordToHtml(String wordPath, String htmlPath) {
  2. try {
  3. File sourceFile = new File(wordPath);
  4. String path = htmlPath + File.separator + sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf(".")) + ".html";
  5. File file = new File(path); // 新建一个空白pdf文档
  6. FileOutputStream os = new FileOutputStream(file);
  7. Document doc = new Document(wordPath); // Address是将要被转化的word文档
  8. HtmlSaveOptions options = new HtmlSaveOptions();
  9. options.setExportImagesAsBase64(true);
  10. options.setExportRelativeFontSize(true);
  11. doc.save(os, options);
  12. } catch (Exception e) {
  13. e.printStackTrace();
  14. }
  15. }

验证结果:
请添加图片描述

2、将pdf文件转换成html文件

  1. public static void pdfToHtml(String pdfPath, String htmlPath) throws IOException, ParserConfigurationException {
  2. File file = new File(pdfPath);
  3. String path = htmlPath + File.separator + file.getName().substring(0, file.getName().lastIndexOf(".")) + ".html";
  4. PDDocument document = PDDocument.load(new File(pdfPath));
  5. Writer writer = new PrintWriter(path, "UTF-8");
  6. new PDFDomTree().writeText(document, writer);
  7. writer.close();
  8. document.close();
  9. }

图片版PDF文件验证结果:
请添加图片描述

文字版PDF文件验证结果:
请添加图片描述

原文链接:https://www.cnblogs.com/fhey/p/17442536.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号