此博客转自:/fyhx/article/details/69569310
场景:
后台上传的文档要再前端app上在线预览
解决办法:
将文档转成html用于前端显示
maven引入
[plain]view plaincopy<dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.document</artifactId><version>1.0.5</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId><version>1.0.5</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>3.9</version></dependency>
具体代码:
1、docx转html
[java]view plaincopy/***docx文件转html*@paramtempContextUrl项目访问名*@return*/publicintWordToHtml(StringtempContextUrl){intrv=0;try{Stringpath=presentationDto.getWordPath();//word路径StringwordPath=path.substring(0,path.indexOf("upload")+6);//word文件名StringwordName=path.substring(path.lastIndexOf(File.separator)+1,path.lastIndexOf("."));//后缀Stringsuffix=path.substring(path.lastIndexOf("."));//生成html路径StringhtmlPath=wordPath+File.separator+System.currentTimeMillis()+"_show"+File.separator;//生成html文件名StringhtmlName=System.currentTimeMillis()+".html";//图片路径StringimagePath=htmlPath+"image"+File.separator;//判断html文件是否存在FilehtmlFile=newFile(htmlPath+htmlName);//word文件FilewordFile=newFile(wordPath+File.separator+wordName+suffix);//1)加载word文档生成XWPFDocument对象InputStreamin=newFileInputStream(wordFile);XWPFDocumentdocument=newXWPFDocument(in);//2)解析XHTML配置(这里设置IURIResolver来设置图片存放的目录)FileimgFolder=newFile(imagePath);XHTMLOptionsoptions=XHTMLOptions.create();options.setExtractor(newFileImageExtractor(imgFolder));//html中图片的路径相对路径options.URIResolver(newBasicURIResolver("image"));options.setIgnoreStylesIfUnused(false);options.setFragment(true);//3)将XWPFDocument转换成XHTML//生成html文件上级文件夹Filefolder=newFile(htmlPath);if(!folder.exists()){folder.mkdirs();}OutputStreamout=newFileOutputStream(htmlFile);XHTMLConverter.getInstance().convert(document,out,options);//4)转换为项目访问路径StringabsolutePath=htmlFile.getAbsolutePath();htmlPath=tempContextUrl+absolutePath.substring(absolutePath.indexOf("upload"));presentationDto.setHtmlPath(htmlPath);}catch(FileNotFoundExceptione){e.printStackTrace();returnrv;}catch(XWPFConverterExceptione){e.printStackTrace();returnrv;}catch(IOExceptione){e.printStackTrace();returnrv;}rv=1;returnrv;}
2、xls转html[java]view plaincopyprivateintxlsToHtml(StringtempContextUrl){intrv=0;Stringpath=presentationDto.getWordPath();//word路径StringwordPath=path.substring(0,path.indexOf("upload")+6)+File.separator;//word文件名StringwordName=path.substring(path.lastIndexOf(File.separator)+1);try{InputStreaminput=newFileInputStream(wordPath+wordName);HSSFWorkbookexcelBook=newHSSFWorkbook(input);ExcelToHtmlConverterexcelToHtmlConverter=newExcelToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());excelToHtmlConverter.processWorkbook(excelBook);Listpics=excelBook.getAllPictures();if(pics!=null){for(inti=0;i<pics.size();i++){Picturepic=(Picture)pics.get(i);try{pic.writeImageContent(newFileOutputStream(wordPath+pic.suggestFullFileName()));}catch(FileNotFoundExceptione){e.printStackTrace();}}}DocumenthtmlDocument=excelToHtmlConverter.getDocument();ByteArrayOutputStreamoutStream=newByteArrayOutputStream();DOMSourcedomSource=newDOMSource(htmlDocument);StreamResultstreamResult=newStreamResult(outStream);TransformerFactorytf=TransformerFactory.newInstance();Transformerserializer=tf.newTransformer();serializer.setOutputProperty(OutputKeys.ENCODING,"utf-8");serializer.setOutputProperty(OutputKeys.INDENT,"yes");serializer.setOutputProperty(OutputKeys.METHOD,"html");serializer.transform(domSource,streamResult);outStream.close();Stringcontent=newString(outStream.toByteArray(),"utf-8");Stringuuid=UidUtil.generateUUID();FileUtils.writeStringToFile(newFile(wordPath,uuid+".html"),content,"utf-8");presentationDto.setHtmlPath(tempContextUrl+"upload"+File.separator+uuid+".html");}catch(Exceptione){e.printStackTrace();returnrv;}rv=1;returnrv;}
3、ppt转html
其实只是ppt转图片,有了图片后放到页面上去显示。
[java]view plaincopy/***ppt转html*@paramtempContextUrl*@return*/privateintpptToHtml(StringtempContextUrl){intrv=0;Stringpath=presentationDto.getWordPath();//word路径StringwordPath=path.substring(0,path.indexOf("upload")+6);//文件夹名StringfolderName=UidUtil.generateUUID();List<String>imgList=newArrayList<String>();Filefile=newFile(path);Filefolder=newFile(wordPath+File.separator+folderName);try{folder.mkdirs();FileInputStreamis=newFileInputStream(file);SlideShowppt=newSlideShow(is);is.close();Dimensionpgsize=ppt.getPageSize();org.apache.poi.hslf.model.Slide[]slide=ppt.getSlides();for(inti=0;i<slide.length;i++){TextRun[]truns=slide[i].getTextRuns();for(intk=0;k<truns.length;k++){RichTextRun[]rtruns=truns[k].getRichTextRuns();for(intl=0;l<rtruns.length;l++){rtruns[l].setFontIndex(1);rtruns[l].setFontName("宋体");}}BufferedImageimg=newBufferedImage(pgsize.width,pgsize.height,BufferedImage.TYPE_INT_RGB);Graphics2Dgraphics=img.createGraphics();graphics.setPaint(Color.BLUE);graphics.fill(newRectangle2D.Float(0,0,pgsize.width,pgsize.height));slide[i].draw(graphics);//这里设置图片的存放路径和图片的格式(jpeg,png,bmp等等),注意生成文件路径StringimgName=File.separator+folderName+File.separator+"pict_"+(i+1)+".jpeg";FileOutputStreamout=newFileOutputStream(wordPath+imgName);javax.imageio.ImageIO.write(img,"jpeg",out);out.close();imgList.add(File.separator+"upload"+imgName);}}catch(FileNotFoundExceptione){e.printStackTrace();returnrv;}catch(IOExceptione){e.printStackTrace();returnrv;}rv=createHtml(wordPath,imgList,tempContextUrl);returnrv;}/***ppt转html时生成html*@paramwordPathupload根目录*@paramimgList所有幻灯片路径*@paramtempContextUrl项目访问路径*@return*/privateintcreateHtml(StringwordPath,List<String>imgList,StringtempContextUrl){intrv=0;StringBuildersb=newStringBuilder("<!doctypehtml><html><head><metacharset='utf-8'><title>无标题文档</title></head><body>");if(imgList!=null&&!imgList.isEmpty()){for(Stringimg:imgList){sb.append("<imgsrc='"+img+"'/><br>");}}sb.append("</body></html>");Stringuuid=UidUtil.generateUUID();try{Filefile=newFile(wordPath+File.separator+uuid+".html");BufferedWriterbufferedWriter=newBufferedWriter(newOutputStreamWriter(newFileOutputStream(file),"UTF-8"));bufferedWriter.write(sb.toString());bufferedWriter.close();}catch(IOExceptione){e.printStackTrace();returnrv;}presentationDto.setHtmlPath(tempContextUrl+"upload"+File.separator+uuid+".html");return1;}
4、pdf转html
原理同ppt转html一样
[java]view plaincopy/***pdf转html*@paramtempContextUrl*@return*/privateintpdfToHtml(StringtempContextUrl){intrv=0;Stringpath=presentationDto.getWordPath();//word路径StringwordPath=path.substring(0,path.indexOf("upload")+6);//文件夹名StringfolderName=UidUtil.generateUUID();List<String>imgList=newArrayList<String>();Filefile=newFile(path);try{PDDocumentdoc=PDDocument.load(path);intpageCount=doc.getPageCount();System.out.println(pageCount);Listpages=doc.getDocumentCatalog().getAllPages();for(inti=0;i<pages.size();i++){PDPagepage=(PDPage)pages.get(i);BufferedImageimage=page.convertToImage();Iteratoriter=ImageIO.getImageWritersBySuffix("jpg");ImageWriterwriter=(ImageWriter)iter.next();StringimgName=File.separator+folderName+File.separator+i+".jpg";Filefolder=newFile(wordPath+File.separator+folderName);//先创建文件夹folder.mkdirs();FileoutFile=newFile(wordPath+imgName);//再创建文件imgList.add(File.separator+"upload"+imgName);outFile.createNewFile();FileOutputStreamout=newFileOutputStream(outFile);ImageOutputStreamoutImage=ImageIO.createImageOutputStream(out);writer.setOutput(outImage);writer.write(newIIOImage(image,null,null));}doc.close();}catch(FileNotFoundExceptione){e.printStackTrace();returnrv;}catch(IOExceptione){e.printStackTrace();returnrv;}rv=createHtml(wordPath,imgList,tempContextUrl);return1;}