前端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>
````
相关推荐
EricWang13587 分钟前
[OS] 项目三-2-proc.c: exit(int status)
服务器·c语言·前端
September_ning7 分钟前
React.lazy() 懒加载
前端·react.js·前端框架
web行路人17 分钟前
React中类组件和函数组件的理解和区别
前端·javascript·react.js·前端框架
超雄代码狂39 分钟前
ajax关于axios库的运用小案例
前端·javascript·ajax
长弓三石1 小时前
鸿蒙网络编程系列44-仓颉版HttpRequest上传文件示例
前端·网络·华为·harmonyos·鸿蒙
小马哥编程1 小时前
【前端基础】CSS基础
前端·css
嚣张农民1 小时前
推荐3个实用的760°全景框架
前端·vue.js·程序员
周亚鑫1 小时前
vue3 pdf base64转成文件流打开
前端·javascript·pdf
Justinc.2 小时前
CSS3新增边框属性(五)
前端·css·css3
neter.asia2 小时前
vue中如何关闭eslint检测?
前端·javascript·vue.js