前端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 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
(⊙o⊙)~哦4 小时前
JavaScript substring() 方法
前端
无心使然云中漫步5 小时前
GIS OGC之WMTS地图服务,通过Capabilities XML描述文档,获取matrixIds,origin,计算resolutions
前端·javascript
Bug缔造者5 小时前
Element-ui el-table 全局表格排序
前端·javascript·vue.js
xnian_5 小时前
解决ruoyi-vue-pro-master框架引入报错,启动报错问题
前端·javascript·vue.js
麒麟而非淇淋6 小时前
AJAX 入门 day1
前端·javascript·ajax
2401_858120536 小时前
深入理解MATLAB中的事件处理机制
前端·javascript·matlab
阿树梢6 小时前
【Vue】VueRouter路由
前端·javascript·vue.js
随笔写7 小时前
vue使用关于speak-tss插件的详细介绍
前端·javascript·vue.js
史努比.8 小时前
redis群集三种模式:主从复制、哨兵、集群
前端·bootstrap·html