docx、ppt、xls、pdf文件转html

您所在的位置:网站首页 ppt转换html docx、ppt、xls、pdf文件转html

docx、ppt、xls、pdf文件转html

2023-11-28 15:46| 来源: 网络整理| 查看: 265

场景:

后台上传的文档要再前端app上在线预览

解决办法:

将文档转成html用于前端显示

maven引入

fr.opensagres.xdocreport fr.opensagres.xdocreport.document 1.0.5 fr.opensagres.xdocreport org.apache.poi.xwpf.converter.xhtml 1.0.5 org.apache.poi poi-ooxml 3.9 org.apache.poi poi-scratchpad 3.9

具体代码:

1、docx转html

/** * docx文件转html * @param tempContextUrl 项目访问名 * @return */ public int Word2007ToHtml(String tempContextUrl) { int rv = 0; try { String path = presentationDto.getWordPath(); //word路径 String wordPath = path.substring(0, path.indexOf("upload")+6); //word文件名 String wordName = path.substring(path.lastIndexOf(File.separator)+1,path.lastIndexOf(".")); //后缀 String suffix = path.substring(path.lastIndexOf(".")); //生成html路径 String htmlPath = wordPath + File.separator + System.currentTimeMillis() + "_show" + File.separator; //生成html文件名 String htmlName = System.currentTimeMillis() + ".html"; //图片路径 String imagePath = htmlPath + "image" + File.separator; //判断html文件是否存在 File htmlFile = new File(htmlPath + htmlName); //word文件 File wordFile = new File(wordPath + File.separator + wordName + suffix); // 1) 加载word文档生成 XWPFDocument对象 InputStream in = new FileInputStream(wordFile); XWPFDocument document = new XWPFDocument(in); // 2) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录) File imgFolder = new File(imagePath); XHTMLOptions options = XHTMLOptions.create(); options.setExtractor(new FileImageExtractor(imgFolder)); //html中图片的路径 相对路径 options.URIResolver(new BasicURIResolver("image")); options.setIgnoreStylesIfUnused(false); options.setFragment(true); // 3) 将 XWPFDocument转换成XHTML //生成html文件上级文件夹 File folder = new File(htmlPath); if(!folder.exists()){ folder.mkdirs(); } OutputStream out = new FileOutputStream(htmlFile); XHTMLConverter.getInstance().convert(document, out, options); // 4) 转换为项目访问路径 String absolutePath = htmlFile.getAbsolutePath(); htmlPath = tempContextUrl + absolutePath.substring(absolutePath.indexOf("upload")); presentationDto.setHtmlPath(htmlPath); } catch (FileNotFoundException e) { e.printStackTrace(); return rv; } catch (XWPFConverterException e) { e.printStackTrace(); return rv; } catch (IOException e) { e.printStackTrace(); return rv; } rv = 1; return rv; } 2、xls转html

private int xlsToHtml(String tempContextUrl){ int rv = 0; String path = presentationDto.getWordPath(); //word路径 String wordPath = path.substring(0, path.indexOf("upload")+6) + File.separator; //word文件名 String wordName = path.substring(path.lastIndexOf(File.separator)+1); try { InputStream input=new FileInputStream(wordPath+wordName); HSSFWorkbook excelBook=new HSSFWorkbook(input); ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter (DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument() ); excelToHtmlConverter.processWorkbook(excelBook); List pics = excelBook.getAllPictures(); if (pics != null) { for (int i = 0; i < pics.size(); i++) { Picture pic = (Picture) pics.get (i); try { pic.writeImageContent (new FileOutputStream (wordPath + pic.suggestFullFileName() ) ); } catch (FileNotFoundException e) { e.printStackTrace(); } } } Document htmlDocument =excelToHtmlConverter.getDocument(); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); DOMSource domSource = new DOMSource (htmlDocument); StreamResult streamResult = new StreamResult (outStream); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty (OutputKeys.ENCODING, "utf-8"); serializer.setOutputProperty (OutputKeys.INDENT, "yes"); serializer.setOutputProperty (OutputKeys.METHOD, "html"); serializer.transform (domSource, streamResult); outStream.close(); String content = new String (outStream.toByteArray(),"utf-8"); String uuid = UidUtil.generateUUID(); FileUtils.writeStringToFile(new File(wordPath, uuid+".html"), content, "utf-8"); presentationDto.setHtmlPath(tempContextUrl + "upload" + File.separator + uuid+".html"); } catch (Exception e) { e.printStackTrace(); return rv; } rv = 1; return rv; } 3、ppt转html

其实只是ppt转图片,有了图片后放到页面上去显示。

/** * ppt转html * @param tempContextUrl * @return */ private int pptToHtml(String tempContextUrl){ int rv = 0; String path = presentationDto.getWordPath(); //word路径 String wordPath = path.substring(0, path.indexOf("upload")+6); //文件夹名 String folderName = UidUtil.generateUUID(); List imgList = new ArrayList(); File file = new File(path); File folder = new File(wordPath + File.separator + folderName); try { folder.mkdirs(); FileInputStream is = new FileInputStream(file); SlideShow ppt = new SlideShow(is); is.close(); Dimension pgsize = ppt.getPageSize(); org.apache.poi.hslf.model.Slide[] slide = ppt.getSlides(); for (int i = 0; i < slide.length; i++) { TextRun[] truns = slide[i].getTextRuns(); for ( int k=0;k


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3