JavaScript實(shí)現(xiàn)word轉(zhuǎn)png的示例代碼
由于 java 后端的 itext 庫(kù)是收費(fèi)版,公司安全部門發(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 依賴: 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í),在 id 為 htmlContent 的節(jié)點(diǎn)下,就可以看到轉(zhuǎn)換后的 html 內(nèi)容了
2.將 html 轉(zhuǎn)成 canvas
安裝 html2canvas 依賴: 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 Title、alt提示(Tips)實(shí)現(xiàn)源碼解讀
我們知道給某些HTML標(biāo)簽加上title屬性后,這個(gè)標(biāo)簽對(duì)象在瀏覽的時(shí)候,鼠標(biāo)移上去就會(huì)有一個(gè)小提示框出來(lái),并顯示title定義的內(nèi)容。2010-12-12
javascript模擬select,jselect的方法實(shí)現(xiàn)
由于主流瀏覽器對(duì)select元素渲染不同,所以在每種瀏覽器下顯示也不一樣,最主要的是默認(rèn)情況下UI太粗糙,即使通過(guò)css加以美化也不能達(dá)到很美觀的效果2012-11-11
JavaScript雙等號(hào)(==)與三等號(hào)(===)的區(qū)別舉例詳解
在JavaScript中判斷值相等有兩種操作符,即雙等號(hào)(==)和三等號(hào)(===),這篇文章主要給大家介紹了關(guān)于JavaScript雙等號(hào)(==)與三等號(hào)(===)區(qū)別的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-07-07
BootStrap數(shù)據(jù)表格實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家分享了BootStrap數(shù)據(jù)表格的相關(guān)知識(shí),感興趣的朋友一起看看吧2017-09-09
使用JS實(shí)現(xiàn)圖片展示瀑布流效果(簡(jiǎn)單實(shí)例)
下面小編就為大家?guī)?lái)一篇使用JS實(shí)現(xiàn)圖片展示瀑布流效果(簡(jiǎn)單實(shí)例)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09
javascript charAt() arr[i]數(shù)組實(shí)例代碼
實(shí)例區(qū)別一下charAt()和arr[i].toString()的使用方法2008-08-08
JavaScript實(shí)現(xiàn)垂直向上無(wú)縫滾動(dòng)特效代碼
下面小編就為大家?guī)?lái)一篇JavaScript實(shí)現(xiàn)垂直向上無(wú)縫滾動(dòng)特效代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11

