文档在线预览word、pdf、excel文件转html以实现文档在线预览
目录
- 一、前言
- 1、aspose
- 2 、poi + pdfbox
- 3 spire
- 二、将文件转换成html字符串
- 1、将word文件转成html字符串
- 1.1 使用aspose
- 1.2 使用poi
- 1.3 使用spire
- 2、将pdf文件转成html字符串
- 2.1 使用aspose
- 2.2 使用 poi + pbfbox
- 2.3 使用spire
- 3、将excel文件转成html字符串
- 3.1 使用aspose
- 3.2 使用poi + pdfbox
- 3.3 使用spire
- 三、将文件转换成html,并生成html文件
- FileUtils类将html字符串生成html文件示例:
- 1、将word文件转换成html文件
- 1.1 使用aspose
- 1.2 使用poi + pdfbox
- 1.3 使用spire
- 2、将pdf文件转换成html文件
- 2.1 使用aspose
- 2.2 使用poi + pdfbox
- 2.3 使用spire
- 3、将excel文件转换成html文件
- 3.1 使用aspose
- 3.2 使用poi
- 3.3 使用spire
- 四、总结
- 1、将word文件转成html字符串
一、前言
以下代码分别提供基于aspose、pdfbox、spire来实现来实现txt、word、pdf、ppt、word等文件转图片的需求。
1、aspose
Aspose 是一家致力于.Net ,Java,SharePoint,JasperReports和SSRS组件的提供商,数十个国家的数千机构都有用过aspose组件,创建、编辑、转换或渲染 Office、OpenOffice、PDF、图像、ZIP、CAD、XPS、EPS、PSD 和更多文件格式。注意aspose是商用组件,未经授权导出文件里面都是是水印(尊重版权,远离破解版)。
需要在项目的pom文件里添加如下依赖
<dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>23.1</version></dependency><dependency><groupId>com.aspose</groupId><artifactId>aspose-pdf</artifactId><version>23.1</version></dependency><dependency><groupId>com.aspose</groupId><artifactId>aspose-cells</artifactId><version>23.1</version></dependency><dependency><groupId>com.aspose</groupId><artifactId>aspose-slides</artifactId><version>23.1</version></dependency>
2 、poi + pdfbox
因为aspose和spire虽然好用,但是都是是商用组件,所以这里也提供使用开源库操作的方式的方式。
POI是Apache软件基金会用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
Apache PDFBox是一个开源Java库,支持PDF文档的开发和转换。 使用此库,您可以开发用于创建,转换和操作PDF文档的Java程序。
需要在项目的pom文件里添加如下依赖
<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.4</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.0</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.0</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>5.2.0</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-excelant</artifactId><version>5.2.0</version></dependency>
3 spire
spire一款专业的Office编程组件,涵盖了对Word、Excel、PPT、PDF等文件的读写、编辑、查看功能。spire提供免费版本,但是存在只能导出前3页以及只能导出前500行的限制,只要达到其一就会触发限制。需要超出前3页以及只能导出前500行的限制的这需要购买付费版(尊重版权,远离破解版)。这里使用免费版进行演示。
spire在添加pom之前还得先添加maven仓库来源
<repository><id>com.e-iceblue</id><name>e-iceblue</name><url>https://repo.e-iceblue.cn/repository/maven-public/</url></repository>
接着在项目的pom文件里添加如下依赖
免费版:
<dependency><groupId>e-iceblue</groupId><artifactId>spire.office.free</artifactId><version>5.3.1</version></dependency>
付费版版:
<dependency><groupId>e-iceblue</groupId><artifactId>spire.office</artifactId><version>5.3.1</version></dependency>
二、将文件转换成html字符串
1、将word文件转成html字符串
1.1 使用aspose
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;}
验证结果:
1.2 使用poi
public String wordToHtmlStr(String wordPath) throws TransformerException, IOException, ParserConfigurationException {String htmlStr = null;String ext = wordPath.substring(wordPath.lastIndexOf("."));if (ext.equals(".docx")) {htmlStr = word2007ToHtmlStr(wordPath);} else if (ext.equals(".doc")){htmlStr = word2003ToHtmlStr(wordPath);} else {throw new RuntimeException("文件格式不正确");}return htmlStr;}public String word2007ToHtmlStr(String wordPath) throws IOException {// 使用内存输出流try(ByteArrayOutputStream out = new ByteArrayOutputStream()){word2007ToHtmlOutputStream(wordPath, out);return out.toString();}}private void word2007ToHtmlOutputStream(String wordPath,OutputStream out) throws IOException {ZipSecureFile.setMinInflateRatio(-1.0d);InputStream in = Files.newInputStream(Paths.get(wordPath));XWPFDocument document = new XWPFDocument(in);XHTMLOptions options = XHTMLOptions.create().setIgnoreStylesIfUnused(false).setImageManager(new Base64EmbedImgManager());// 使用内存输出流XHTMLConverter.getInstance().convert(document, out, options);}private String word2003ToHtmlStr(String wordPath) throws TransformerException, IOException, ParserConfigurationException {org.w3c.dom.Document htmlDocument = word2003ToHtmlDocument(wordPath);// Transform document to stringStringWriter writer = new StringWriter();TransformerFactory tf = TransformerFactory.newInstance();Transformer transformer = tf.newTransformer();transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");transformer.setOutputProperty(OutputKeys.METHOD, "html");transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");transformer.transform(new DOMSource(htmlDocument), new StreamResult(writer));return writer.toString();}private org.w3c.dom.Document word2003ToHtmlDocument(String wordPath) throws IOException, ParserConfigurationException {InputStream input = Files.newInputStream(Paths.get(wordPath));HWPFDocument wordDocument = new HWPFDocument(input);WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());wordToHtmlConverter.setPicturesManager((content, pictureType, suggestedName, widthInches, heightInches) -> {System.out.println(pictureType);if (PictureType.UNKNOWN.equals(pictureType)) {return null;}BufferedImage bufferedImage = ImgUtil.toImage(content);String base64Img = ImgUtil.toBase64(bufferedImage, pictureType.getExtension());// 带图片的word,则将图片转为base64编码,保存在一个页面中StringBuilder sb = (new StringBuilder(base64Img.length() + "data:;base64,".length()).append("data:;base64,").append(base64Img));return sb.toString();});// 解析word文档wordToHtmlConverter.processDocument(wordDocument);return wordToHtmlConverter.getDocument();}
1.3 使用spire
public String wordToHtmlStr(String wordPath) throws IOException {try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {Document document = new Document();document.loadFromFile(wordPath);document.saveToFile(outputStream, FileFormat.Html);return outputStream.toString();}}
2、将pdf文件转成html字符串
2.1 使用aspose
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();}
验证结果:
2.2 使用 poi + pbfbox
public 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();}
2.3 使用spire
public String pdfToHtmlStr(String pdfPath) throws IOException, ParserConfigurationException {try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {PdfDocument pdf = new PdfDocument();pdf.loadFromFile(pdfPath);return outputStream.toString();}}
3、将excel文件转成html字符串
3.1 使用aspose
public static String excelToHtmlStr(String excelPath) throws Exception {FileInputStream fileInputStream = new FileInputStream(excelPath);Workbook workbook = new XSSFWorkbook(fileInputStream);DataFormatter dataFormatter = new DataFormatter();FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();Sheet sheet = workbook.getSheetAt(0);StringBuilder htmlStringBuilder = new StringBuilder();htmlStringBuilder.append("<html><head><title>Excel to HTML using Java and POI library</title>");htmlStringBuilder.append("<style>table, th, td { border: 1px solid black; }</style>");htmlStringBuilder.append("</head><body><table>");for (Row row : sheet) {htmlStringBuilder.append("<tr>");for (Cell cell : row) {CellType cellType = cell.getCellType();if (cellType == CellType.FORMULA) {formulaEvaluator.evaluateFormulaCell(cell);cellType = cell.getCachedFormulaResultType();}String cellValue = dataFormatter.formatCellValue(cell, formulaEvaluator);htmlStringBuilder.append("<td>").append(cellValue).append("</td>");}htmlStringBuilder.append("</tr>");}htmlStringBuilder.append("</table></body></html>");return htmlStringBuilder.toString();}
返回的html字符串:
<html><head><title>Excel to HTML using Java and POI library</title><style>table, th, td { border: 1px solid black; }</style></head><body><table><tr><td>序号</td><td>姓名</td><td>性别</td><td>联系方式</td><td>地址</td></tr><tr><td>1</td><td>张晓玲</td><td>女</td><td>11111111111</td><td>上海市浦东新区xx路xx弄xx号</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦东新区xx路xx弄xx号</td></tr><tr><td>1</td><td>张晓玲</td><td>女</td><td>11111111111</td><td>上海市浦东新区xx路xx弄xx号</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦东新区xx路xx弄xx号</td></tr><tr><td>1</td><td>张晓玲</td><td>女</td><td>11111111111</td><td>上海市浦东新区xx路xx弄xx号</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦东新区xx路xx弄xx号</td></tr><tr><td>1</td><td>张晓玲</td><td>女</td><td>11111111111</td><td>上海市浦东新区xx路xx弄xx号</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦东新区xx路xx弄xx号</td></tr><tr><td>1</td><td>张晓玲</td><td>女</td><td>11111111111</td><td>上海市浦东新区xx路xx弄xx号</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦东新区xx路xx弄xx号</td></tr><tr><td>1</td><td>张晓玲</td><td>女</td><td>11111111111</td><td>上海市浦东新区xx路xx弄xx号</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦东新区xx路xx弄xx号</td></tr><tr><td>1</td><td>张晓玲</td><td>女</td><td>11111111111</td><td>上海市浦东新区xx路xx弄xx号</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦东新区xx路xx弄xx号</td></tr></table></body></html>
3.2 使用poi + pdfbox
public String excelToHtmlStr(String excelPath) throws Exception {FileInputStream fileInputStream = new FileInputStream(excelPath);try (Workbook workbook = WorkbookFactory.create(new File(excelPath))){DataFormatter dataFormatter = new DataFormatter();FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);StringBuilder htmlStringBuilder = new StringBuilder();htmlStringBuilder.append("<html><head><title>Excel to HTML using Java and POI library</title>");htmlStringBuilder.append("<style>table, th, td { border: 1px solid black; }</style>");htmlStringBuilder.append("</head><body><table>");for (Row row : sheet) {htmlStringBuilder.append("<tr>");for (Cell cell : row) {CellType cellType = cell.getCellType();if (cellType == CellType.FORMULA) {formulaEvaluator.evaluateFormulaCell(cell);cellType = cell.getCachedFormulaResultType();}String cellValue = dataFormatter.formatCellValue(cell, formulaEvaluator);htmlStringBuilder.append("<td>").append(cellValue).append("</td>");}htmlStringBuilder.append("</tr>");}htmlStringBuilder.append("</table></body></html>");return htmlStringBuilder.toString();}}
3.3 使用spire
public String excelToHtmlStr(String excelPath) throws Exception {try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {Workbook workbook = new Workbook();workbook.loadFromFile(excelPath);workbook.saveToStream(outputStream, com.spire.xls.FileFormat.HTML);return outputStream.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文件
word原文件效果:
1.1 使用aspose
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();}}
转换成html的效果:
1.2 使用poi + pdfbox
public void wordToHtml(String wordPath, String htmlPath) throws TransformerException, IOException, ParserConfigurationException {htmlPath = FileUtil.getNewFileFullPath(wordPath, htmlPath, "html");String ext = wordPath.substring(wordPath.lastIndexOf("."));if (ext.equals(".docx")) {word2007ToHtml(wordPath, htmlPath);} else if (ext.equals(".doc")){word2003ToHtml(wordPath, htmlPath);} else {throw new RuntimeException("文件格式不正确");}}public void word2007ToHtml(String wordPath, String htmlPath) throws TransformerException, IOException, ParserConfigurationException {//try(OutputStream out = Files.newOutputStream(Paths.get(path))){try(FileOutputStream out = new FileOutputStream(htmlPath)){word2007ToHtmlOutputStream(wordPath, out);}}private void word2007ToHtmlOutputStream(String wordPath,OutputStream out) throws IOException {ZipSecureFile.setMinInflateRatio(-1.0d);InputStream in = Files.newInputStream(Paths.get(wordPath));XWPFDocument document = new XWPFDocument(in);XHTMLOptions options = XHTMLOptions.create().setIgnoreStylesIfUnused(false).setImageManager(new Base64EmbedImgManager());// 使用内存输出流XHTMLConverter.getInstance().convert(document, out, options);}public void word2003ToHtml(String wordPath, String htmlPath) throws TransformerException, IOException, ParserConfigurationException {org.w3c.dom.Document htmlDocument = word2003ToHtmlDocument(wordPath);// 生成html文件地址try(OutputStream outStream = Files.newOutputStream(Paths.get(htmlPath))){DOMSource domSource = new DOMSource(htmlDocument);StreamResult streamResult = new StreamResult(outStream);TransformerFactory factory = TransformerFactory.newInstance();Transformer serializer = factory.newTransformer();serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");serializer.setOutputProperty(OutputKeys.INDENT, "yes");serializer.setOutputProperty(OutputKeys.METHOD, "html");serializer.transform(domSource, streamResult);}}private org.w3c.dom.Document word2003ToHtmlDocument(String wordPath) throws IOException, ParserConfigurationException {InputStream input = Files.newInputStream(Paths.get(wordPath));HWPFDocument wordDocument = new HWPFDocument(input);WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());wordToHtmlConverter.setPicturesManager((content, pictureType, suggestedName, widthInches, heightInches) -> {System.out.println(pictureType);if (PictureType.UNKNOWN.equals(pictureType)) {return null;}BufferedImage bufferedImage = ImgUtil.toImage(content);String base64Img = ImgUtil.toBase64(bufferedImage, pictureType.getExtension());// 带图片的word,则将图片转为base64编码,保存在一个页面中StringBuilder sb = (new StringBuilder(base64Img.length() + "data:;base64,".length()).append("data:;base64,").append(base64Img));return sb.toString();});// 解析word文档wordToHtmlConverter.processDocument(wordDocument);return wordToHtmlConverter.getDocument();}
转换成html的效果:
1.3 使用spire
public void wordToHtml(String wordPath, String htmlPath) {htmlPath = FileUtil.getNewFileFullPath(wordPath, htmlPath, "html");Document document = new Document();document.loadFromFile(wordPath);document.saveToFile(htmlPath, FileFormat.Html);}
转换成html的效果:
因为使用的是免费版,存在页数和字数限制,需要完整功能的的可以选择付费版本。PS:这回76页的文档居然转成功了前50页。
2、将pdf文件转换成html文件
图片版pdf原文件效果:
文字版pdf原文件效果:
2.1 使用aspose
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文件验证结果:
2.2 使用poi + pdfbox
public void pdfToHtml(String pdfPath, String htmlPath) throws IOException, ParserConfigurationException {String path = FileUtil.getNewFileFullPath(pdfPath, htmlPath, "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原文件效果:
2.3 使用spire
public void pdfToHtml(String pdfPath, String htmlPath) throws IOException, ParserConfigurationException {htmlPath = FileUtil.getNewFileFullPath(pdfPath, htmlPath, "html");PdfDocument pdf = new PdfDocument();pdf.loadFromFile(pdfPath);pdf.saveToFile(htmlPath, com.spire.pdf.FileFormat.HTML);}
图片版PDF文件验证结果:
因为使用的是免费版,所以只有前三页是正常的。。。有超过三页需求的可以选择付费版本。
文字版PDF原文件效果:
报错了无法转换。。。
java.lang.NullPointerExceptionat com.spire.pdf.PdfPageWidget.spr┢⅛(Unknown Source)at com.spire.pdf.PdfPageWidget.getSize(Unknown Source)at com.spire.pdf.PdfPageBase.spr†™—(Unknown Source)at com.spire.pdf.PdfPageBase.getActualSize(Unknown Source)at com.spire.pdf.PdfPageBase.getSection(Unknown Source)at com.spire.pdf.general.PdfDestination.spr︻┎—(Unknown Source)at com.spire.pdf.general.PdfDestination.spr┻┑—(Unknown Source)at com.spire.pdf.general.PdfDestination.getElement(Unknown Source)at com.spire.pdf.primitives.PdfDictionary.setProperty(Unknown Source)at com.spire.pdf.bookmarks.PdfBookmark.setDestination(Unknown Source)at com.spire.pdf.bookmarks.PdfBookmarkWidget.spr┭┘—(Unknown Source)at com.spire.pdf.bookmarks.PdfBookmarkWidget.getDestination(Unknown Source)at com.spire.pdf.PdfDocumentBase.spr╻⅝(Unknown Source)at com.spire.pdf.widget.PdfPageCollection.spr┦⅝(Unknown Source)at com.spire.pdf.widget.PdfPageCollection.removeAt(Unknown Source)at com.spire.pdf.PdfDocumentBase.spr┞⅝(Unknown Source)at com.spire.pdf.PdfDocument.loadFromFile(Unknown Source)
3、将excel文件转换成html文件
excel原文件效果:
3.1 使用aspose
public void excelToHtml(String excelPath, String htmlPath) throws Exception {htmlPath = FileUtil.getNewFileFullPath(excelPath, htmlPath, "html");Workbook workbook = new Workbook(excelPath);com.aspose.cells.HtmlSaveOptions options = new com.aspose.cells.HtmlSaveOptions();workbook.save(htmlPath, options);}
转换成html的效果:
3.2 使用poi
public void excelToHtml(String excelPath, String htmlPath) throws Exception {String path = FileUtil.getNewFileFullPath(excelPath, htmlPath, "html");try(FileOutputStream fileOutputStream = new FileOutputStream(path)){String htmlStr = excelToHtmlStr(excelPath);byte[] bytes = htmlStr.getBytes();fileOutputStream.write(bytes);}}public String excelToHtmlStr(String excelPath) throws Exception {FileInputStream fileInputStream = new FileInputStream(excelPath);try (Workbook workbook = WorkbookFactory.create(new File(excelPath))){DataFormatter dataFormatter = new DataFormatter();FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);StringBuilder htmlStringBuilder = new StringBuilder();htmlStringBuilder.append("<html><head><title>Excel to HTML using Java and POI library</title>");htmlStringBuilder.append("<style>table, th, td { border: 1px solid black; }</style>");htmlStringBuilder.append("</head><body><table>");for (Row row : sheet) {htmlStringBuilder.append("<tr>");for (Cell cell : row) {CellType cellType = cell.getCellType();if (cellType == CellType.FORMULA) {formulaEvaluator.evaluateFormulaCell(cell);cellType = cell.getCachedFormulaResultType();}String cellValue = dataFormatter.formatCellValue(cell, formulaEvaluator);htmlStringBuilder.append("<td>").append(cellValue).append("</td>");}htmlStringBuilder.append("</tr>");}htmlStringBuilder.append("</table></body></html>");return htmlStringBuilder.toString();}}
转换成html的效果:
3.3 使用spire
public void excelToHtml(String excelPath, String htmlPath) throws Exception {htmlPath = FileUtil.getNewFileFullPath(excelPath, htmlPath, "html");Workbook workbook = new Workbook();workbook.loadFromFile(excelPath);workbook.saveToFile(htmlPath, com.spire.xls.FileFormat.HTML);}
转换成html的效果:
四、总结
从上述的效果展示我们可以发现其实转成html效果不是太理想,很多细节样式没有还原,这其实是因为这类转换往往都是追求目标是通过使用文档中的语义信息并忽略其他细节来生成简单干净的 HTML,所以在转换过程中复杂样式被忽略,比如居中、首行缩进、字体,文本大小,颜色。举个例子在转换是 会将应用标题 1 样式的任何段落转换为 h1 元素,而不是尝试完全复制标题的样式。所以转成html的显示效果往往和原文档不太一样。这意味着对于较复杂的文档而言,这种转换不太可能是完美的。但如果都是只使用简单样式文档或者对文档样式不太关心的这种方式也不妨一试。
PS:如果想要展示效果好的话,其实可以将上篇文章《文档在线预览(一)通过将txt、word、pdf转成图片实现在线预览功能》说的内容和本文结合起来使用,即将文档里的内容都生成成图片(很可能是多张图片),然后将生成的图片全都放到一个html页面里 ,用html+css来保持样式并实现多张图片展示,再将html返回。开源组件kkfilevie就是用的就是这种做法。
kkfileview展示效果如下:
下图是kkfileview返回的html代码,从html代码我们可以看到kkfileview其实是将文件(txt文件除外)每页的内容都转成了图片,然后将这些图片都嵌入到一个html里,再返回给用户一个html页面。
相关文章:

文档在线预览word、pdf、excel文件转html以实现文档在线预览
目录 一、前言 1、aspose2 、poi pdfbox3 spire二、将文件转换成html字符串 1、将word文件转成html字符串 1.1 使用aspose1.2 使用poi1.3 使用spire2、将pdf文件转成html字符串 2.1 使用aspose2.2 使用 poi pbfbox2.3 使用spire3、将excel文件转成html字符串 3.1 使用aspose…...
FFmpeg视音频分离器----向雷神学习
雷神博客地址:https://blog.csdn.net/leixiaohua1020/article/details/39767055 本程序可以将封装格式中的视频码流数据和音频码流数据分离出来。 在该例子中, 将FLV的文件分离得到H.264视频码流文件和MP3 音频码流文件。 注意: 这个是简化版…...
CentOS 8开启bbr
CentOS 8 默认内核版本为 4.18.x,内核版本高于4.9 就可以直接开启 BBR,所以CentOS 8 启用BBR非常简单不需要再去升级内核。 开启bbr echo "net.core.default_qdiscfq" >> /etc/sysctl.conf echo "net.ipv4.tcp_congestion_contro…...

Redis的安装与基本使用
文章目录 Linux 环境下安装Redis下载Redis 安装包解压安装包安装Redis进入redis安装包下编译并且安装到指定目录下 启动redis配置远程访问找到Redis.config文件 Windows 环境下安装Redis说明官方提供方式安装或启用WSL2在WSL(Ubuntu)上安装Redis启动Redi…...

2014 款金旅牌小型客车 发动机怠速抖动、加速无力
故障现象 一辆2014款金旅牌小型客车,搭载JM491Q-ME发动机,累计行驶里程约为20万km。车主反映,最近该车发动机怠速抖动、加速无力,且经常缺少冷却液。 故障诊断 根据车主描述的故障现象,初步判断该车气缸垫损坏&#…...

R语言逻辑回归、决策树、随机森林、神经网络预测患者心脏病数据混淆矩阵可视化...
全文链接:https://tecdat.cn/?p33760 众所周知,心脏疾病是目前全球最主要的死因。开发一个能够预测患者心脏疾病存在的计算系统将显著降低死亡率并大幅降低医疗保健成本。机器学习在全球许多领域中被广泛应用,尤其在医疗行业中越来越受欢迎。机器学习可…...
网站被劫持了怎么办
网站被劫持了怎么办 建议新建一个index.html文件,文件中只写几个数字,上传到网站根目录,然后访问网站域名,看看是不是正常,从而可以确定是程序问题还是域名被劫持的问题。 如果是域名被劫持,你可以登录你的…...
【面试题精讲】Java包装类缓存机制
有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准https://blog.zysicyj.top 首发博客地址[1] 面试题手册[2] 系列文章地址[3] 1. 什么是 Java 包装类缓存机制? Java 中的包装类(Wrapper Class)是为了将…...
网络相关知识
0 socket SOCK_DGRAM #无连接UDP SOCK_STREAM #面向连接TCP 1 UDP 1.1 检测UDP yum install -y nc 使用netcat测试连通性 服务器端启动 UDP 30003 端口 nc -l -u 30003 客户端连接服务器的30003端口(假设服务的IP地址是119.23.67.12) nc -u 119…...
商品冷启动推荐综述
About Me: LuckBoyPhd/Resume (github.com) (1)一种基于三部图网络的协同过滤算法 推荐系统是电子商务领域最重要的技术之一,而协同过滤算法又是推荐系统用得最广泛的.提出了一种基于加权三部图网络的协同过滤算法,用户、产品及标签都被考虑到算法中,并且研究了标签结点的度对…...

GEO生信数据挖掘(二)下载基因芯片平台文件及注释
检索到目标数据集后,开始数据挖掘,本文以阿尔兹海默症数据集GSE1297为例 目录 下载平台文件 1.AnnotGPL参数改为TRUE,联网下载芯片平台的soft文件。(国内网速奇慢经常中断) 2.手工去GEO官网下载 转换芯片探针ID为gene name 拓…...

淘宝电商必备的大数据应用
在日常生活中,大家总能听到“大数据”“人工智能”的说法。现在的大数据技术应用,从大到巨大科学研究、社会信息审查、搜索引擎,小到社交联结、餐厅推荐等等,已经渗透到我们生活中的方方面面。到底大数据在电商行业可以怎么用&…...

Docker版部署RocketMQ开启ACL验证
一、拉取镜像 docker pull apache/rocketmq:latest 二、准备挂载目录 mkdir /usr/local/rocketmq/data mkdir /usr/local/rocketmq/conf 三、运行 docker run \ -d \ -p 9876:9876 \ -v /usr/local/rocketmq/data/logs:/home/rocketmq/logs \ -v /usr/local/rocketmq/data…...

【RabbitMQ实战】04 RabbitMQ的基本概念:Exchange,Queue,Channel等
一、简介 Message Queue的需求由来已久,80年代最早在金融交易中,高盛等公司采用Teknekron公司的产品,当时的Message queuing软件叫做:the information bus(TIB)。 TIB被电信和通讯公司采用,路透…...
APACHE NIFI学习之—RouteOnAttribute
RouteOnAttribute 描述: 使用属性表达式语言根据其属性路由数据流,每个表达式必须返回Boolean类型的值(true或false)。 标签: attributes, routing, Attribute Expression Language, regexp, regex, Regular Expression, Expression Language, 属性, 路由, 表达式, 正则…...

防火墙网络接口下面多个外网地址,只有第一地址可以访问通其他不通
环境: 主备防火墙 8.0.75 AF-2000-FH2130B-SC 问题描述: 两台防火墙双击热备,高可用防火墙虚拟网络接口Eth4下面有多个外网地址,只有第一地址可以访问通其他不通 解决方案: 1.检查防火墙路由设置(未解决…...

【HTTP】URL结构、HTTP请求和响应的报文格式、HTTP请求的方法、常见的状态码、GET和POST有什么区别、Cookie、Session等重点知识汇总
目录 URL格式 HTTP请求和响应报文的字段? HTTP请求方法 常见的状态码 GET 和 POST 的区别 Cookie 和 Session URL格式 ?:是用来分割URL的主体部分(通常是路径)和查询字符串(query string)…...

苹果mac电脑显示内存不足如何解决?
忍痛删应用、删文档、删照片视频等等一系列操作都是众多Mac用户清理内存空间的方法之一,悲催的是一顿“猛如虎的操作”下,释放出来的内存空间却少的可怜,原因很简单,这样释放内存空间是无效的。如何合理有效的清理内存空间&#x…...
如何在Windows 10上安装Go并搭建本地编程环境
引言 Go是在谷歌的挫折中诞生的编程语言。开发人员不得不不断地选择一种执行效率高但需要长时间编译的语言,或者选择一种易于编程但在生产环境中运行效率低的语言。Go被设计为同时提供这三种功能:快速编译、易于编程和在生产中高效执行。 虽然Go是一种通用的编程语…...

[Realtek sdk-3.4.14b]RTL8197FH-VG 2.4G to WAN吞吐量低于60%的问题分析及解决方案
问题描述 RTL8197FH-VG 2.4G wifi to WAN吞吐量低于65%的标准,正常2T2R的wifi 300Mbps x 65% = 195Mbps,但是实际只能跑到160Mbps,这个时候CPU的idl已经为0,sirq占用率达到98%左右 网络拓扑 一台PC通过2.4G WiFi连接到RTL8197FH-VG,另外一台PC直接通过WAN口连接到RTL8197…...
谷歌浏览器插件
项目中有时候会用到插件 sync-cookie-extension1.0.0:开发环境同步测试 cookie 至 localhost,便于本地请求服务携带 cookie 参考地址:https://juejin.cn/post/7139354571712757767 里面有源码下载下来,加在到扩展即可使用FeHelp…...
进程地址空间(比特课总结)
一、进程地址空间 1. 环境变量 1 )⽤户级环境变量与系统级环境变量 全局属性:环境变量具有全局属性,会被⼦进程继承。例如当bash启动⼦进程时,环 境变量会⾃动传递给⼦进程。 本地变量限制:本地变量只在当前进程(ba…...

黑马Mybatis
Mybatis 表现层:页面展示 业务层:逻辑处理 持久层:持久数据化保存 在这里插入图片描述 Mybatis快速入门 
【入坑系列】TiDB 强制索引在不同库下不生效问题
文章目录 背景SQL 优化情况线上SQL运行情况分析怀疑1:执行计划绑定问题?尝试:SHOW WARNINGS 查看警告探索 TiDB 的 USE_INDEX 写法Hint 不生效问题排查解决参考背景 项目中使用 TiDB 数据库,并对 SQL 进行优化了,添加了强制索引。 UAT 环境已经生效,但 PROD 环境强制索…...

JUC笔记(上)-复习 涉及死锁 volatile synchronized CAS 原子操作
一、上下文切换 即使单核CPU也可以进行多线程执行代码,CPU会给每个线程分配CPU时间片来实现这个机制。时间片非常短,所以CPU会不断地切换线程执行,从而让我们感觉多个线程是同时执行的。时间片一般是十几毫秒(ms)。通过时间片分配算法执行。…...
CMake控制VS2022项目文件分组
我们可以通过 CMake 控制源文件的组织结构,使它们在 VS 解决方案资源管理器中以“组”(Filter)的形式进行分类展示。 🎯 目标 通过 CMake 脚本将 .cpp、.h 等源文件分组显示在 Visual Studio 2022 的解决方案资源管理器中。 ✅ 支持的方法汇总(共4种) 方法描述是否推荐…...

ABAP设计模式之---“简单设计原则(Simple Design)”
“Simple Design”(简单设计)是软件开发中的一个重要理念,倡导以最简单的方式实现软件功能,以确保代码清晰易懂、易维护,并在项目需求变化时能够快速适应。 其核心目标是避免复杂和过度设计,遵循“让事情保…...
SQL慢可能是触发了ring buffer
简介 最近在进行 postgresql 性能排查的时候,发现 PG 在某一个时间并行执行的 SQL 变得特别慢。最后通过监控监观察到并行发起得时间 buffers_alloc 就急速上升,且低水位伴随在整个慢 SQL,一直是 buferIO 的等待事件,此时也没有其他会话的争抢。SQL 虽然不是高效 SQL ,但…...

Selenium常用函数介绍
目录 一,元素定位 1.1 cssSeector 1.2 xpath 二,操作测试对象 三,窗口 3.1 案例 3.2 窗口切换 3.3 窗口大小 3.4 屏幕截图 3.5 关闭窗口 四,弹窗 五,等待 六,导航 七,文件上传 …...

三分算法与DeepSeek辅助证明是单峰函数
前置 单峰函数有唯一的最大值,最大值左侧的数值严格单调递增,最大值右侧的数值严格单调递减。 单谷函数有唯一的最小值,最小值左侧的数值严格单调递减,最小值右侧的数值严格单调递增。 三分的本质 三分和二分一样都是通过不断缩…...