前端生成docx文档、excel表格、图片、pdf文件

一、前端将页面某区域内容下载为word文档:html-to-docx、file-saver插件组合使用
javascript 复制代码
import HTMLtoDOCX from 'html-to-docx';
import { saveAs } from 'file-saver';

const exportTest = async () => {
    const fileBuffer = await HTMLtoDOCX(
      `<h2>文件标题</h2><br>文件内容222`,
      null,
      {
        table: { row: { cantSplit: true } },
        footer: true,
        pageNumber: true,
        font: '-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji',
      },
    );
    saveAs(fileBuffer, `测试下载文件.docx`);
  }
二、前端将数据导出成excel表格:XLSX插件
javascript 复制代码
import * as XLSX from 'xlsx';

const downloadExcel = () => {
    const tableRows = [{'表头1':'111', 'name': 'zhangsan '}]
    const sheet = XLSX.utils.json_to_sheet(tableRows); //此处为表格的数据
    const wb = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, sheet);
    XLSX.writeFile(wb, `${date.getTime()}.xlsx`);
  };
三、页面区域导出为图片:html2canvas插件
javascript 复制代码
import html2canvas from 'html2canvas';
import { useEffect, useRef } from 'react';


export const TestPage = () => {
const testRef = useRef(null);

const html2image = (url: any, fileName: any) => {
  // 创建一个虚拟的a标签
  let link = document.createElement('a');
  link.href = url;
  link.download = fileName;
  document.body.appendChild(link);
  // 触发点击事件进行下载
  link.click();
  // 下载完成后删除a标签
  setTimeout(function () {
    document.body.removeChild(link);
  }, 100);
};

const downloadImg = () => {
    const dom = testRef.current;
    html2canvas(dom, {
      useCORS: true,
      allowTaint: true,
    }).then((canvas: any) => {
      const url = canvas.toDataURL('image/png');
      html2image(url, new Date().getTime().toString());
    });
  };

return (
    <>
        <div onClick={() =>{downloadImg ()}}>导出</div>
        <div ref={testRef}>需要导出的页面区域</div>
    </>
)
}
四、前端导出为pdf文件:jsPDF
javascript 复制代码
import jsPDF from 'jspdf';
const htmlStringToPdf = async (dom: HTMLElement, name: string) => {
  html2canvas(dom, {
    scale: 1,
    y: -10,
  }).then((canvas: any) => {
    const imgWidth = 200;
    const pageHeight = 300;
    const imgHeight = (canvas.height * imgWidth) / canvas.width;
    let heightLeft = imgHeight;
    let position = 0;
    heightLeft -= pageHeight;
    const doc = new jsPDF('p', 'mm', 'a4');
    doc.addImage(canvas, 'PNG', 0, position, imgWidth, imgHeight, '', 'FAST');
    while (heightLeft >= 0) {
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(canvas, 'PNG', 0, position, imgWidth, imgHeight, '', 'FAST');
      heightLeft -= pageHeight;
    }
    doc.save(`${name}.pdf`);
  });
};
相关推荐
蓝婷儿12 分钟前
第二章:CSS秘典 · 色彩与布局的力量
前端·css
flying robot14 分钟前
js在浏览器执行原理
开发语言·javascript·ecmascript
Wyc724091 小时前
HTML:入门
前端·html
Sunny_lxm1 小时前
自定义列甘特图,原生开发dhtmlxgantt根特图,根据数据生成只读根特图,页面展示html demo
前端·html·甘特图·dhtmlxgantt
熊猫钓鱼>_>2 小时前
建筑IT数字化突围:建筑设计企业的生存法则重塑
前端·javascript·easyui
GISer_Jing4 小时前
前端性能指标及优化策略——从加载、渲染和交互阶段分别解读详解并以Webpack+Vue项目为例进行解读
前端·javascript·vue
不知几秋4 小时前
数字取证-内存取证(volatility)
java·linux·前端
水银嘻嘻6 小时前
08 web 自动化之 PO 设计模式详解
前端·自动化
Zero1017137 小时前
【详解pnpm、npm、yarn区别】
前端·react.js·前端框架
&白帝&8 小时前
vue右键显示菜单
前端·javascript·vue.js