详细记录Word文档(包含doc文件和docx文件的上传图片会回显)转Html实现前端预览

实现了两种格式Word文档转Html的需求

优点:可以实现多图的doc文档和docx文档转HTML代码,图片也会完美展示,图片不需要保存到本地服务器,直接上传到文件服务器即可,文档格式也会保留
缺点:文档的页边距格式也会保留,由于html代码比较复杂,如果需要去除页边距格式建议是前端进行处理

controller层

    @PostMapping("/uploadAndConvert")
    public ApiResult<DocVO> uploadAndConvert(@RequestParam("file") MultipartFile request){
        String fileName = request.getOriginalFilename();

        if (fileName ==null){
            return ApiResult.fail("文件名为空");
        }
        try {
            InputStream inputStream = request.getInputStream();
            return newsService.uploadAndConvert(inputStream,fileName);
        }catch (IOException e){
            return ApiResult.fail("上传转换异常");
        }
    }

实现层

    @Override
    public ApiResult<DocVO> uploadAndConvert(InputStream inputStream,String fileName) throws IOException {
        byte[] bytes = IOUtils.toByteArray(inputStream);
        //需要调用自己的文件上传下载服务
        String uploadUrl = FileSystemClient.upload(bytes, 30);
        String docSource = uploadUrl+"&filename="+fileName;
        DocVO docVO = new DocVO();
        if (uploadUrl != null){
            //需要调用自己的文件上传下载服务
            byte[] download = FileSystemClient.download(uploadUrl, 30);
            if (fileName.endsWith(".doc")){
                return docToHtml(bytes, docSource, docVO);
            }else if(fileName.endsWith(".docx")){
                return docxToHtml(docSource, docVO, download);
            }

        }
        return ApiResult.fail("文件上传失败");
    }

doc文档转Html

            HWPFDocument hwpfDocument = new HWPFDocument(new ByteArrayInputStream(bytes));
            org.w3c.dom.Document newDocument = XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument();
            WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(newDocument);
            wordToHtmlConverter.setPicturesManager( new PicturesManager()
            {
            	//重写savePicture方法去上传图片到文件服务器并返回图片访问地址,这样就会自动把图片链接放到html的对应位置
                @Override
                public String savePicture(byte[] content,
                                          PictureType pictureType, String suggestedName,
                                          float widthInches, float heightInches )
                {
                    //这里需要调用你自己的文件上传服务,content就是图片的byte数组,直接就是savePicture方法的参数,不需要额外传参(我也没太懂,但是可以直接用)
                    return FileSystemClient.upload(content,30);
                }
            } );
            wordToHtmlConverter.processDocument(hwpfDocument);
            List<Picture> allPictures = hwpfDocument.getPicturesTable().getAllPictures();
            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(allPictures)){
                for (Picture p : allPictures) {
                    Picture picture = p;
                    FileSystemClient.upload(picture.getContent(),30);
                }
            }
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT,"no");
            transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            transformer.setOutputProperty(OutputKeys.METHOD, "html");
            StringWriter stringWriter = new StringWriter();
            transformer.transform(new DOMSource(wordToHtmlConverter.getDocument()), new StreamResult(stringWriter));
            //最终的html,直接返给前端进行渲染就可以了
            String html = stringWriter.toString();

docx转Html

            //与doc转html基本类似,差异在于工具类的不同
            XWPFDocument document = new XWPFDocument(new ByteArrayInputStream(download));
            StringWriter stringWriter = new StringWriter();
            XHTMLConverter xhtmlConverter = (XHTMLConverter) XHTMLConverter.getInstance();
            XHTMLOptions options = XHTMLOptions.create();
            //docx转html时同样需要重写指定工具类的方法
            options.setImageManager( new ImageManager(new File(""), "") {
                String upload = null;
                @Override
                public void extract(String imagePath, byte[] imageData) throws IOException {
                    //自己的文件上传接口,imageData同样是上面的byte数组,不需要额外传参
                    upload = FileSystemClient.upload(imageData, 30);
                }
                @Override
                public String resolve(String uri) {
                    //这里返回的是文件上传服务返回的图片访问地址
                    return upload;
                }
            });
            xhtmlConverter.convert(document, stringWriter, options);
            //最终的html,直接返给前端进行渲染就可以了
            String html = new String(stringWriter.toString().getBytes("utf-8"), "utf-8");

最后特别感谢两位大佬的文章,让我受益匪浅

https://www.jianshu.com/p/272b0ac2f06e
https://www.cnblogs.com/jameslif/p/3356588.html

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前端实现在线常用文件的方法有两种: 1. 使用第三方插件或工具,如 Google Docs、Office Online、PDF.js 等,这些工具可以将文件化为网页格式进行在线。 2. 使用 HTML5 提供的文件功能,支持常用的图片、音频、视频和 PDF 等文件。 下面分别给出实现方法和示例代码: 1. 使用第三方插件或工具 Google Docs 示例: ```html <!-- 在 HTML 中嵌入 Google Docs 链接 --> <iframe src="https://docs.google.com/gview?url=http://example.com/sample.docx&embedded=true"></iframe> ``` Office Online 示例: ```html <!-- 在 HTML 中嵌入 Office Online 链接 --> <iframe src="https://view.officeapps.live.com/op/embed.aspx?src=http://example.com/sample.docx"></iframe> ``` PDF.js 示例: ```html <!-- 在 HTML 中嵌入 PDF.js 链接 --> <iframe src="https://mozilla.github.io/pdf.js/web/viewer.html?file=http://example.com/sample.pdf"></iframe> ``` 2. 使用 HTML5 提供的文件功能 图片示例: ```html <!-- 在 HTML 中嵌入图片 --> <img src="http://example.com/sample.jpg" alt="Sample Image" /> ``` 音频示例: ```html <!-- 在 HTML 中嵌入音频 --> <audio src="http://example.com/sample.mp3" controls></audio> ``` 视频示例: ```html <!-- 在 HTML 中嵌入视频 --> <video src="http://example.com/sample.mp4" controls></video> ``` PDF 文件示例: ```html <!-- 在 HTML 中嵌入 PDF 文件 --> <embed src="http://example.com/sample.pdf" type="application/pdf" /> ``` 以上是常见文件在线实现方法和示例代码,具体实现还需根据具体需求和技术栈来选择最适合的方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值