前端html-docx实现html转word,并导出文件,文字+图片

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

前端web页面 有文字,有图片,保存web的css效果

使用工具:html-docx

官方网址:http://docs.asprain.cn/html-docx/readme.html

步骤:

1 npm install html-docx-js

npm install file-saver

import FileSaver from "file-saver";

import htmlDocx from "html-docx-js/dist/html-docx";

2 写html,写行内样式,word可显示效果;

设置图片宽度 width='XX'(不设置的话,图片显示宽高效果不理想)

3 导出的方法 var converted = htmlDocx.asBlob(content);

saveAs(converted, 'test.docx');

复制代码
<template>
  <div class="container">
    <breadCrumb></breadCrumb>
    <section>
      <wrapBox class="ar-box">
        <div id="pcContract">
          <div class="ar-title" style="text-align: center;font-size: 24px; font-weight: bold; line-height: 60px;">
            <p>关于{{alarm.monitorPoint.name}}限高架发生碰撞报警的报告</p>
          </div>
          <div class="sr-con">
            <div class="ar-conbox">
              <div class="ar-contit" style="font-size: 20px; font-weight: bold; line-height: 60px;">一、告警通知</div>
              <div class="ar-context ml20" style="text-indent: 2em;margin: 10px 0;"> {{alarm.remark}} </div>
            </div>
            <div class="ar-conbox">
              <div class="ar-contit" style="font-size: 20px; font-weight: bold; line-height: 60px;">二、监测数据</div>
              <div class="mb10" style="font-size: 18px; font-weight: bold; line-height: 60px;margin-bottom: 20px ;">2.1
                视频监测图片:</div>
              <div class="ar-context">
                <div class="ar-imgs" style="text-align: center;margin-bottom: 20px;">
                  <img class="ar-imgCss" :src="imgList[0]" width="600" />
                  <p>碰撞前</p>
                </div>
                <div class="ar-imgs" style="text-align: center;margin-bottom: 20px;">
                  <img class="ar-imgCss" :src="imgList[2]" width="600" />
                  <p>碰撞中</p>
                </div>
                <div class="ar-imgs" style="text-align: center;margin-bottom: 20px;">
                  <img class="ar-imgCss" :src="imgList[4]" width="600" />
                  <p>碰撞后</p>
                </div>
              </div>
            </div>
          </div>
          <footer>
            <p class="dateP" style="text-align: right;">{{alarm.addTime | ymd3}}</p>
          </footer>
        </div>
        <el-button type="primary" @click="download()">下载</el-button>
      </wrapBox>
    </section>
  </div>
</template>
<script>
  import alarmApi from '@/api/alarm/record'
  import deviceApi from '@/api/device/device'
  import ChartItem from "../Equipment/chartItem";

  import FileSaver from "file-saver";
  import htmlDocx from "html-docx-js/dist/html-docx";

  export default {
    name: "",
    components: {
      ChartItem,
    },
    data() {
      return {
        id: '',
        alarm: { monitorPoint: { name: '' } },
        imgList: [],
      };
    },
    computed: {   },
    mounted() {
      this.init();
      this.$nextTick(() => {
      });
    },
    methods: {
      init() {
        this.id = this.$route.params.id ? this.$route.params.id : '';
        console.log(',', this.id)
        alarmApi.getDetail(this.id).then((res) => {
          if (res.flag) {
            this.alarm = res.object;
            this.getImg()
          } else {
          }
        }).catch();
      },
      getImg() {
        let data = {
          showProof: true,
          ids: [this.alarm.id]
        }
        alarmApi.getData(data).then((res) => {
          if (res.flag == 0) {
            let strImg = res.data[0].alarmLogProof[0].imageUrl;
            let splitImg = strImg ? strImg.split(",") : '';
            this.imgList = splitImg
          }
        })
      },
      download() {
        let contentHtml = document.getElementById('pcContract').innerHTML;
        let content = `
          <!DOCTYPE html><html>
          <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
          </head>
          <body>
            <div>
            ${contentHtml}
            </div>
          </body>
        </html>
        `;
        let converted = htmlDocx.asBlob(content);
        FileSaver.saveAs(converted, `关于${this.alarm.monitorPoint.name}限高架发生碰撞报警的报告.docx`);
      },
      //over
    },
  };
</script>
<style lang="scss" scoped>
  .container {
    >section {
      padding: 20px;

      .detail {

        header {
          margin-bottom: 20px;

          h2 {
            font-size: 18px;
            color: $cyan2;
          }
        }

        section {
          display: flex;
          flex-direction: column;
          color: #859fce;


          .buttons {
            padding-left: 20px;

            .el-button {
              width: 100px;
            }
          }
        }
      }
    }
  }

  .ar-box {
    color: #fff;

  }

  .ar-conbox {
    margin-bottom: 20px;
  }

  .ar-title {
    text-align: center;
    font-size: 20px;

  }

  .ar-title p {
    margin-bottom: 10px;
  }

  .ar-contit {
    font-size: 18px;
    margin-bottom: 10px;
  }

  .ar-imgs {
    text-align: center;
    margin-bottom: 20px;
  }

  .ar-imgCss {
    width: 60%;
  }

  .ml20 {
    margin-left: 20px;
  }

  .mb10 {
    margin-bottom: 10px;
  }

  .dateP {
    text-align: right;
  }
</style>
````
相关推荐
耶啵奶膘2 小时前
uniapp+firstUI——上传视频组件fui-upload-video
前端·javascript·uni-app
视频砖家3 小时前
移动端Html5播放器按钮变小的问题解决方法
前端·javascript·viewport功能
lyj1689973 小时前
vue-i18n+vscode+vue 多语言使用
前端·vue.js·vscode
小白变怪兽5 小时前
一、react18+项目初始化(vite)
前端·react.js
ai小鬼头5 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
墨菲安全6 小时前
NPM组件 betsson 等窃取主机敏感信息
前端·npm·node.js·软件供应链安全·主机信息窃取·npm组件投毒
GISer_Jing6 小时前
Monorepo+Pnpm+Turborepo
前端·javascript·ecmascript
天涯学馆6 小时前
前端开发也能用 WebAssembly?这些场景超实用!
前端·javascript·面试
我在北京coding7 小时前
TypeError: Cannot read properties of undefined (reading ‘queryComponents‘)
前端·javascript·vue.js
Eiceblue7 小时前
使用 C# 发送电子邮件(支持普通文本、HTML 和附件)
开发语言·c#·html·visual studio