word转html在前端页面显示

近日碰到该需求,遇坑记录于此,望能帮助到其他伙伴。
word转HTML时图片的处理我分为两种解决方案:1、图片保存在本地文件夹。2、转为base64。
以下分doc与docx,不同格式处理方法略有不同。
前端我用的是vue,直接通过“v-html”显示html文本

一、图片保存本地

坑1

大多数浏览器都不能以形如(file:///C://xxx)的形式直接访问本地文件,故需要在后台配置文件中配置虚拟路径映射到本地盘符。

@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{
   
    @Autowired
    private RepeatSubmitInterceptor repeatSubmitInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
   
        /** 本地文件上传路径 */
        //访问时:http://localhost:8080/Constants.RESOURCE_PREFIX/xxx
        //RuoYiConfig.getProfile()保存的本地文件夹路径
        registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + RuoYiConfig.getProfile() + "/");
}

1、doc

// 实例化WordToHtmlConverter,为图片等资源文件做准备
				WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
						DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
				wordToHtmlConverter.setPicturesManager(new PicturesManager() {
   
					public String savePicture(byte[] content, PictureType pictureType, String suggestedName,
							float widthInches, float heightInches) {
   
						return suggestedName;
					}
				});
				// doc
				HWPFDocument wordDocument = new HWPFDocument(entity.getContent());
				wordToHtmlConverter.processDocument(wordDocument);
				// 处理图片,会在同目录下生成并保存图片
				List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();
				if (pics != null) {
   
					for (int i = 0; i < pics.size(); i++) {
   
						Picture pic = (Picture) pics.get(i);
						try {
   
							String a = RuoYiConfig.getProfile()+ "/" + pic.suggestFullFileName();
							pic.wr
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
你可以使用一些库或工具来将Word文档换为Markdown格式。下面是一种可能的方法: 1. 使用`mammoth.js`库:`mammoth.js`是一个JavaScript库,可以将.docx文件换为HTML或Markdown格式。你可以使用它来将Word文档换为Markdown格式。首先,将`mammoth.js`库添加到你的项目中。 ```html <script src="https://unpkg.com/mammoth"></script> ``` 2. 创建一个HTML文件,用于选择和上传Word文档,并将其换为Markdown格式。 ```html <!DOCTYPE html> <html> <body> <input type="file" id="wordFile" /> <button onclick="convertToMarkdown()">Convert to Markdown</button> <div id="result"></div> <script> function convertToMarkdown() { var fileInput = document.getElementById("wordFile"); var file = fileInput.files[0]; var reader = new FileReader(); reader.onload = function (e) { var arrayBuffer = e.target.result; var options = { arrayBuffer: arrayBuffer, }; mammoth.extractRawText(options).then(function (result) { var markdown = result.value; var resultDiv = document.getElementById("result"); resultDiv.textContent = markdown; }); }; reader.readAsArrayBuffer(file); } </script> </body> </html> ``` 上述代码将创建一个包含文件选择按钮和“Convert to Markdown”按钮的页面。当用户选择一个Word文档并点击按钮时,将调用`convertToMarkdown`函数。该函数将使用`mammoth.js`库将Word文档换为Markdown,并将结果显示页面上。 请注意,此代码仅在浏览器环境中运行。如果你在Node.js环境中运行,请使用相应的文件读取方法来读取Word文档。 希望这个方法能帮助到你!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小苗爸爸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值