前端html-docx实现html转word,预览并导出文件

使用工具:html-docx

优势:图片、图表能直接预览并转为base64导出,省去后端难以实现图表的生成后插入的麻烦
劣势:适合一些简单的word模板导出(比如只有标题正文简单的表格图表图片的文档),复杂的可以直接忽略。比如:纸张大小、纸张方向、css大部分样式等等(经本人试验导出无法生成,如有大佬可以配置实现欢迎指正)

下载引入

npm install html-docx-js
npm install file-saver
// 引入
import htmlDocx from 'html-docx-js/dist/html-docx'
import saveAs from 'file-saver'

在创建的盒子内完成html代码

<div class="export-box">
	<!-- html -->
</div>
<button @click="gohtml">生成word</button>

生成word导出参考https://blog.csdn.net/Liuer_Qin/article/details/124799876

gohtml() {
  const app = document.querySelector('.export-box')
  const cloneApp = app.cloneNode(true)
  const canvases = app.getElementsByTagName('canvas')
  const cloneCanvases = cloneApp.getElementsByTagName('canvas')
  const promises = Array.from(canvases).map((ca, index) => {
    return new Promise((res) => {
      const url = ca.toDataURL('image/png', 1)
      const img = new Image()
      img.onload = () => {
        URL.revokeObjectURL(url)
        res()
      }
      img.src = url
      // 生成img插入clone的dom的canvas之前
      cloneCanvases[index].parentNode.insertBefore(img, cloneCanvases[index])
    })
  })
  // 移除原来的canvas
  const cloneCanvas = cloneApp.getElementsByTagName('canvas')
  Array.from(cloneCanvas).forEach((ca) => ca.parentNode.removeChild(ca))

  Promise.all(promises).then(() => {
    this.convertImagesToBase64(cloneApp)
    // console.log(document.head.outerHTML)
    const converted = htmlDocx.asBlob(`
      <html xmlns:o=\'urn:schemas-microsoft-com:office:office\' xmlns:w=\'urn:schemas-microsoft-com:office:word\' xmlns=\'http://www.w3.org/TR/REC-html40\'><head><style>
      ${document.head.outerHTML}
      </head>
      <body>
      ${cloneApp.outerHTML}
      </body>
      </html>`)
    saveAs(converted, 'test.docx')
  })
},
convertImagesToBase64(cloneApp) {
      var regularImages = cloneApp.getElementsByTagName('img')
      var canvas = document.createElement('canvas')
      var ctx = canvas.getContext('2d')
      regularImages.forEach((item) => {
        canvas.width = item.width
        canvas.height = item.height
        ctx.drawImage(item, 0, 0, item.width, item.height)
        var ext = item.src.substring(item.src.lastIndexOf('.') + 1).toLowerCase()
        var dataURL = canvas.toDataURL('image/' + ext)
        item.setAttribute('src', dataURL)
      })
      canvas.remove()
}

页面及导出效果
在这里插入图片描述
在这里插入图片描述
最后补充一下,项目本准备开始采用这种方案来实现的,但是功能还是有些欠缺了,尤其纸张大小和横版显示,不得不放弃了这种方式。因为图表的问题,最后是前端先请求数据再页面隐藏位置生成图表,按base64或者文件传送的方式传给后端,仍然由后端生成word返回文件流进行导出。
综上,如何模板内容格式简单,要求低,纯前端来实现这种预览导出还是可行的,要求高的话还是后端去做吧。

  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
要使用html-docx-js实现word下载并在分页中加表头,可以按照以下步骤进行操作: 1. 安装html-docx-js 可以通过npm安装html-docx-js,命令如下: ``` npm install html-docx-js ``` 也可以在浏览器中使用CDN引入html-docx-js。 2. 创建表格 可以使用HTML创建一个带有表头的表格,例如: ```html <table> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr> <td>张三</td> <td>18</td> <td>男</td> </tr> <tr> <td>李四</td> <td>20</td> <td>女</td> </tr> <tr> <td>王五</td> <td>22</td> <td>男</td> </tr> </tbody> </table> ``` 3. 使用html-docx-js将表格换为docx格式 可以使用html-docx-js将HTML中的内容换为docx格式,例如: ```javascript const htmlDocx = require('html-docx-js'); const tableHtml = ` <table> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr> <td>张三</td> <td>18</td> <td>男</td> </tr> <tr> <td>李四</td> <td>20</td> <td>女</td> </tr> <tr> <td>王五</td> <td>22</td> <td>男</td> </tr> </tbody> </table> `; const docx = htmlDocx.asBlob(tableHtml); // 下载docx文件 const downloadLink = document.createElement('a'); downloadLink.href = window.URL.createObjectURL(docx); downloadLink.download = 'table.docx'; document.body.appendChild(downloadLink); downloadLink.click(); ``` 4. 在分页中加表头 要在分页中加表头,可以将表头和表格分开,在表格上面放置一个固定位置的表头,例如: ```html <div style="position: fixed; top: 0;"> <table> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> </table> </div> <div style="margin-top: 60px;"> <table> <tbody> <tr> <td>张三</td> <td>18</td> <td>男</td> </tr> <tr> <td>李四</td> <td>20</td> <td>女</td> </tr> <tr> <td>王五</td> <td>22</td> <td>男</td> </tr> <!-- 省略部分内容 --> </tbody> </table> </div> ``` 然后使用html-docx-js将两个部分分别换为docx格式,最后合并成一个docx文件即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值