亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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)文章

  • JavaScript黑洞數(shù)字之運(yùn)算路線查找算法(遞歸算法)實(shí)例

    JavaScript黑洞數(shù)字之運(yùn)算路線查找算法(遞歸算法)實(shí)例

    這篇文章主要介紹了JavaScript黑洞數(shù)字之運(yùn)算路線查找算法,涉及JavaScript遞歸操作算法相關(guān)技巧,需要的朋友可以參考下
    2016-01-01
  • 關(guān)于Bootstrap彈出框無(wú)法調(diào)用問(wèn)題的解決辦法

    關(guān)于Bootstrap彈出框無(wú)法調(diào)用問(wèn)題的解決辦法

    這篇文章主要介紹了關(guān)于Bootstrap彈出框無(wú)法調(diào)用問(wèn)題的解決辦法的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • Javascript 兼容firefox的一些問(wèn)題

    Javascript 兼容firefox的一些問(wèn)題

    上午在做一些測(cè)試.把頁(yè)面拿到火狐上去就出現(xiàn)一堆問(wèn)題.頁(yè)面布局先不說(shuō).Javascript代碼的問(wèn)題就夠頭疼
    2009-05-05
  • JS控制一個(gè)DIV層在指定時(shí)間內(nèi)消失的方法

    JS控制一個(gè)DIV層在指定時(shí)間內(nèi)消失的方法

    這篇文章主要介紹了JS控制一個(gè)DIV層在指定時(shí)間內(nèi)消失的方法,需要的朋友可以參考下
    2014-02-02
  • 最新評(píng)論