最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript實(shí)現(xiàn)word轉(zhuǎn)png的示例代碼

 更新時(shí)間:2024年02月06日 08:24:32   作者:前端_老Q  
這篇文章主要為大家詳細(xì)介紹了如何使用JavaScript實(shí)現(xiàn)word轉(zhuǎn)png的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

由于 java 后端的 itext 庫(kù)是收費(fèi)版,公司安全部門(mén)發(fā)出安全警告,要求整改。

所以采用前端實(shí)現(xiàn) word 文檔轉(zhuǎn)圖片功能。

一、需求

上傳 docx 文件,并生成 png 圖片作為縮略圖

二、分析

前端無(wú)法直接把 docx 文檔轉(zhuǎn)成圖片格式

三、解決方案

既然直接轉(zhuǎn)無(wú)法實(shí)現(xiàn),那就采用迂回戰(zhàn)術(shù)

  • 先轉(zhuǎn)成 html(用到庫(kù) docx-preview
  • 再將 html 轉(zhuǎn)成 canvas(用到庫(kù) html2canvas
  • 最后將 canvas 轉(zhuǎn)成 png

四、實(shí)現(xiàn)步驟

1.將 docx 文件轉(zhuǎn)成 html 格式,并插入目標(biāo)節(jié)點(diǎn)中

安裝 docx-preview 依賴(lài): pnpm add docx-preview --save

import { useEffect } from 'react';
import * as docx from 'docx-preview';

export default ({ file }) => {
  useEffect(() => {
    // file 為上傳好的 docx 格式文件
    docx2Html(file);
  }, [file]);

  /**
   * @description: docx 文件轉(zhuǎn) html
   * @param {*} file: docx 格式文件
   * @return {*}
   */
  const docx2Html = file => {
    if (!file) {
      return;
    }
    // 只處理 docx 文件
    const suffix = file.name?.substr(file.name.lastIndexOf('.') + 1).toLowerCase();
    if (suffix !== 'docx') {
      return;
    }
    const htmlContentDom = document.querySelector('#htmlContent'); // 生成 html 后掛載的 dom 節(jié)點(diǎn)
    const docxOptions = Object.assign(docx.defaultOptions, {
      debug: true,
      experimental: true,
    });
    docx.renderAsync(file, htmlContentDom, null, docxOptions).then(() => {
      console.log('docx 轉(zhuǎn) html 完成');
    });
  };

  return <div id='htmlContent' />;
};

此時(shí),在 idhtmlContent 的節(jié)點(diǎn)下,就可以看到轉(zhuǎn)換后的 html 內(nèi)容了

2.將 html 轉(zhuǎn)成 canvas

安裝 html2canvas 依賴(lài): pnpm add html2canvas --save

import html2canvas from 'html2canvas';

/**
 * @description: dom 元素轉(zhuǎn)為圖片
 * @return {*}
 */
const handleDom2Img = async () => {
  const htmlContentDom = document.querySelector('#htmlContent'); // 生成 html 后掛載的 dom 節(jié)點(diǎn)
  // 獲取剛剛生成的 dom 元素
  const htmlContent = htmlContentDom.querySelectorAll('.docx-wrapper>section')[0]; // 需要生成圖片的 html 內(nèi)容
  // 創(chuàng)建 canvas 元素
  const canvasDom = document.createElement('canvas');

  // 獲取 dom 寬高
  const w = parseInt(window.getComputedStyle(htmlContent).width, 10);
  // const h = parseInt(window.getComputedStyle(htmlContent).height, 10);

  // 設(shè)定 canvas 元素屬性寬高為 DOM 節(jié)點(diǎn)寬高 * 像素比
  const scale = window.devicePixelRatio; // 縮放比例
  canvasDom.width = w * scale; // 取文檔寬度
  canvasDom.height = w * scale; // 縮略圖是正方形,所以高度跟寬度保持一致

  // 按比例增加分辨率,將繪制內(nèi)容放大對(duì)應(yīng)比例
  const canvas = await html2canvas(htmlContent, {
    canvas: canvasDom,
    scale,
    useCORS: true,
  });
  return canvas;
};

3.將生成好的 canvas 轉(zhuǎn)成 png 文件,并下載

// 將 canvas 轉(zhuǎn)為 base64 圖片
const base64Str = canvas.toDataURL();

// 下載圖片
const imgName = `圖片_${new Date().valueOf()}`;
const aElement = document.createElement('a');
aElement.href = base64Str;
aElement.download = `${imgName}.png`;
document.body.appendChild(aElement);
aElement.click();
document.body.removeChild(aElement);
window.URL.revokeObjectURL(base64Str);

到此這篇關(guān)于JavaScript實(shí)現(xiàn)word轉(zhuǎn)png的示例代碼的文章就介紹到這了,更多相關(guān)JavaScript word轉(zhuǎn)png內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

伊川县| 石阡县| 阿克陶县| 油尖旺区| 舟山市| 商城县| 张家界市| 阳信县| 库尔勒市| 弥渡县| 巩留县| 遂宁市| 当阳市| 瑞昌市| 新沂市| 开鲁县| 鄂伦春自治旗| 鹤岗市| 娄烦县| 科技| 囊谦县| 礼泉县| 重庆市| 咸丰县| 遵化市| 华坪县| 社旗县| 平罗县| 淮南市| 财经| 明光市| 兴义市| 英超| 潞西市| 门头沟区| 安乡县| 大化| 邵阳县| 黄大仙区| 呼玛县| 苍梧县|