基于docxtemplater的word模板处理vue组件

一、安装依赖

npm install docxtemplater
npm install pizzip
npm install file-saver

二、组件源码
<template>
     <el-button  @click="renderDoc">文件下载</el-button>
</template>
<script>
import Docxtemplater from 'docxtemplater'
import PizZip from 'pizzip'
import PizZipUtils from 'pizzip/utils/index.js'
import { saveAs } from 'file-saver'

function loadFile (url, callback) {
  PizZipUtils.getBinaryContent(url, callback)
}
export default {
  props: {
    dataList: {
      type: Object
    }
  },
  methods: {
    replaceErrors (key, value) {
      if (value instanceof Error) {
        return Object.getOwnPropertyNames(value).reduce(function (error, key) {
          error[key] = value[key]
          return error
        }, {})
      }
      return value
    },
    errorHandler (error) {
      console.log(JSON.stringify({ error: error }, this.replaceErrors()))
      if (error.properties && error.properties.errors instanceof Array) {
        const errorMessages = error.properties.errors
          .map(function (error) {
            return error.properties.explanation
          })
          .join('\n')
        console.log('errorMessages', errorMessages)
        // errorMessages is a humanly readable message looking like this :
        // 'The tag beginning with "foobar" is unopened'
      }
      throw error
    },
    renderDoc () {
      // 解决异步参数传递问题
      const _this = this
      loadFile('https://docxtemplater.com/tag-example.docx', function (
        error,
        content
      ) {
        if (error) {
          throw error
        }
        var zip = new PizZip(content)
        var doc = new Docxtemplater().loadZip(zip)
        doc.setData(_this.dataList)
        try {
          // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
          doc.render()
        } catch (error) {
          // The error thrown here contains additional information when logged with JSON.stringify (it contains a properties object containing all suberrors).
          this.errorHandler(error)
        }
        var out = doc.getZip().generate({
          type: 'blob',
          mimeType:
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
        }) // Output the document using Data-URI
        saveAs(out, 'output.docx')
      })
    }
  }
}
</script>
三、组件调用
<template>
  <div >
    <Docxtemplater :dataList="this.data"/>
  </div>
</template>
<script>
// @ is an alias to /src
import Docxtemplater from '@/components/docxtemplater.vue'
export default {
  name: 'docxtemplater',
  components: {
    Docxtemplater
  },
  data () {
    return {
      data: {
        first_name: 'John',
        last_name: 'Doe',
        phone: '0652455478',
        description: 'New Website'
      }
    }
  }
}
</script>
四、效果展示

【界面展示】
在这里插入图片描述
【word模板】
在这里插入图片描述
【数据生成】
在这里插入图片描述

五、示例下载

https://download.csdn.net/download/qq_40981137/15364180

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 Word 文档转换为 PDF,你可以使用一些 JavaScript 库和工具来实现。下面是使用 `docxtemplater` 和 `pdfmake` 库的一个示例: 首先,确保你的项目中已安装了 `docxtemplater` 和 `pdfmake` 依赖。你可以使用以下命令进行安装: ```bash npm install docxtemplater pdfmake ``` 接下来,创建一个 JavaScript 函数来执行转换: ```javascript import Docxtemplater from 'docxtemplater'; import PizZip from 'pizzip'; import { saveAs } from 'file-saver'; import pdfMake from 'pdfmake/build/pdfmake'; import pdfFonts from 'pdfmake/build/vfs_fonts'; // 注册字体 pdfMake.vfs = pdfFonts.pdfMake.vfs; function convertToPDF(wordFile) { // 读取 Word 文档内容 const reader = new FileReader(); reader.onload = function(event) { const content = event.target.result; // 将 Word 文档转换为可编辑模板 const zip = new PizZip(content); const doc = new Docxtemplater().loadZip(zip); // 生成 PDF 内容 const pdfContent = doc.getZip() .generate({ type: 'blob', mimeType: 'application/pdf' }); // 下载 PDF 文件 saveAs(pdfContent, 'converted-document.pdf'); }; reader.readAsBinaryString(wordFile); } ``` 在上述代码中,我们将 Word 文档加载到 `Docxtemplater` 中,然后将其转换为可编辑模板。接下来,使用 `generate` 方法将模板生成为 PDF 文件。最后,通过 `saveAs` 方法将生成的 PDF 文件保存到本地。 你可以在 Vue 或其他 JavaScript 框架中使用此函数来实现 Word 到 PDF 的转换。只需将 Word 文档作为参数传递给 `convertToPDF` 函数即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值