JavaScript實(shí)現(xiàn)word轉(zhuǎn)png的示例代碼
由于 java 后端的 itext 庫是收費(fèi)版,公司安全部門發(fā)出安全警告,要求整改。
所以采用前端實(shí)現(xiàn) word 文檔轉(zhuǎn)圖片功能。
一、需求
上傳 docx 文件,并生成 png 圖片作為縮略圖
二、分析
前端無法直接把 docx 文檔轉(zhuǎn)成圖片格式
三、解決方案
既然直接轉(zhuǎn)無法實(shí)現(xiàn),那就采用迂回戰(zhàn)術(shù)
- 先轉(zhuǎn)成
html(用到庫 docx-preview ) - 再將
html轉(zhuǎn)成canvas(用到庫 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)文章
用JS操作FRAME中的IFRAME及其內(nèi)容的實(shí)現(xiàn)代碼
一直都需要這樣的東西,發(fā)現(xiàn)了這個(gè)好東西,一定要研究下2008-07-07
Bootstrap幻燈片輪播圖支持觸屏左右手勢(shì)滑動(dòng)的實(shí)現(xiàn)方法
最近在研究用bootstrap搭建網(wǎng)站,Bootstrap能自適應(yīng)pc端和手機(jī)端,并且移動(dòng)設(shè)備優(yōu)先,適合現(xiàn)如今移動(dòng)營銷,大家用的設(shè)備基本是觸屏的了,能用滑動(dòng)交互在小屏幕上體驗(yàn)會(huì)更好,那么如何實(shí)現(xiàn)呢?下面小編給大家介紹下bootstrap 手勢(shì)滑動(dòng)輪播圖的實(shí)現(xiàn)方法2016-10-10
node.js使用nodemailer發(fā)送郵件實(shí)例
這篇文章主要介紹了node.js使用nodemailer發(fā)送郵件的方法,例子中使用的是QQ郵箱,你也可以修改成其它的郵箱如163、gmail等,需要的朋友可以參考下2014-03-03
JavaScript黑洞數(shù)字之運(yùn)算路線查找算法(遞歸算法)實(shí)例
關(guān)于Bootstrap彈出框無法調(diào)用問題的解決辦法
JS控制一個(gè)DIV層在指定時(shí)間內(nèi)消失的方法

