首页 > 其他分享 >vue中html导出成word

vue中html导出成word

时间:2024-06-05 17:23:33浏览次数:8  
标签:index canvas vue word cloneApp html regularImages const

vue 中将 html 中内容转换为 word 文档导出,无需模板,可以导出 echarts 图表。

使用 html-docx-js、file-saver 。

先将html中内容获取,之后将页面上的元素转成图片,然后把图片放到word文件中进行导出。

参考链接:https://blog.csdn.net/weixin_47494533/article/details/137018678

                  https://www.jb51.net/javascript/300311dk9.htm

1、引用

npm install html-docx-js --save
npm install file-saver --save

2、创建 exportToWord.js,添加导出方法

// exportToWord.js

import htmlDocx from 'html-docx-js/dist/html-docx';
import saveAs from 'file-saver';

/**
 * 导出html为word
 * @param fileName 导出文件名
 * @param elementClass 导出块的class
 */
export function exportToWord(fileName, elementClass) {
  //.export-box 需要转word的最大dom盒子的类名
  const app = document.querySelector(elementClass);
  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(() => {
    convertImagesToBase64(cloneApp);
     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, fileName);
  });

}

/**
 * 将image转换成base64
 * @param cloneApp
 */
 function convertImagesToBase64(cloneApp){
  let regularImages = cloneApp.getElementsByTagName('img');
  let canvas = document.createElement('canvas');
  let ctx = canvas.getContext('2d');

  for (let index = 0; index < regularImages.length; index++) {
    canvas.width = regularImages[index].width;
    canvas.height = regularImages[index].height;
    ctx.drawImage(
      regularImages[index],
      0,
      0,
      regularImages[index].width * 0.8,
      regularImages[index].height * 0.8,
    );
    let ext = regularImages[index].src
      .substring(regularImages[index].src.lastIndexOf('.') + 1)
      .toLowerCase();
    let dataURL = canvas.toDataURL('image/' + ext);
    regularImages[index].setAttribute('src', dataURL);
  }
  canvas.remove();
}

 3、调用方法

<template>
  <div>
    <div>
      <el-row>
        <button type="button" @click="exportWord1">导出word</button>
      </el-row>

      <div id="contentDom" style="text-align: center" class="export-box">
         <div>内容</div>

      </div>

    </div>
  </div>
</template>

<script>
  import { exportToWord } from '@/utils/feature/exportToWord';

    export default {
      name: "exportToWord",
      data() {
        return {

        }
      },
      methods: {
        // html转为word
        exportWord1() {
          exportToWord('test.docx', '.export-box');
        },

      }
    }
</script>

<style scoped>

</style>

  

标签:index,canvas,vue,word,cloneApp,html,regularImages,const
From: https://www.cnblogs.com/webttt/p/18233412

相关文章

  • html标签的快捷
     html标签的快捷录入方式一:<ul><li><ahref=""></a></li></ul>如果要写上面的标签,直接写##ul>li>a然后按Tap键盘,就可以快速构建二:<ul><li><ahref=""></a></li><li><......
  • Ant Design Vue 动态表头并填充数据
    AntDesignVue动态表头并填充数据AntDesignVue是基于AntDesign的Vue版本,它为Vue.js提供了一套高质量的UI组件库。在本文中,我们将介绍如何使用AntDesignVue创建一个动态表头并填充数据。首先,确保你已经安装了AntDesignVue。如果还没有安装,可以通过以下命......
  • html生成wrod文档,并智能生成目录
    注意:首页网页标签不要有h1等标签,不然会生成目录使用的是Aspose.WordsDocumentdoc=newDocument();DocumentBuilderbuilder=newDocumentBuilder(doc);builder.InsertHtml(temHtml);if(isCatalogue){......
  • 【vuex小试牛刀】
    了解vuex核心概念请移步https://vuex.vuejs.org/zh/#一、初始vuex#1.1vuex是什么就是把需要共享的变量全部存储在一个对象里面,然后将这个对象放在顶层组件中供其他组件使用父子组件通信时,我们通常会采用props+emit这种方式。但当通信......
  • 【慢慢理解Vue的设计思想】
    #理解Vue的设计思想MVVM框架的三要素:数据响应式、模板引擎及其渲染数据响应式:监听数据变化并在视图中更新Object.defineProperty()Proxy模版引擎:提供描述视图的模版语法插值:{{}}指令:v-bind,v-on,v-model,v-for,v-if渲染:如何将模板转换为html模板=>vdom=>dom#......
  • pdf文件可以转成html网页吗?
    目前我们工作或学习中使用最多的可能就是PDF格式的文档了,它虽然有很多好处,但是有时如果文档比较大,传送就比较麻烦,这时我们将其转换成HTML再发送就很方便了。那么pdf格式怎么转html格式呢?方法一、使用在线pdf转html如果不想下载软件的话,一些在线工具例如smallpdf中文版、speedpdf......
  • vue中将html导出成pdf
    vue中将页面html内容导出成pdf文件格式,使用 html2canvas、jspdf。首先使用 html2canvas将内容转换为图片,之后写入pdf。1、引用第一个.将页面html转换成图片npminstall--savehtml2canvas第二个.将图片生成pdfnpminstalljspdf--save2、创建  exportTo......
  • VUE网易云【附:资料➕文档】
    前言:我是源码分享交流Coding,专注Java+Vue领域,专业提供程序设计开发、源码分享、技术指导讲解、各类项目免费分享,定制和毕业设计服务!免费获取方式--->>文章末尾处!项目介绍007:项目名:仿网易云技术栈:VUE(页面数据对接网易云官方数据,需联网运行)访问网页:http://localh......
  • 【vuejs】keep-alive组件的原理讲解和使用讲解
    1.keep-alive简介Vue.js框架中的<keep-alive>组件是一个用于缓存组件实例的内置组件,它使得组件在不活动时保持其状态,从而提高应用的性能和用户体验。当使用动态组件<component>切换视图时,不在显示的组件实例会被销毁并重新创建,这会导致状态丢失。而<keep-alive>可......
  • word怎么改成图片?四个专业的方法,批量把word转为图片
    传统的Word文档在分享时可能受到格式、字体、排版等多种因素的限制,导致接收方无法完全还原原文档的样式。而通过将Word文档转换为图片格式,可以确保文档内容的完整性和一致性,使接收方能够准确理解文档内容。此外,图片格式的文件更容易在社交媒体、论坛、博客等平台上进行分享和传......