image.png
docx文件實(shí)現(xiàn)前端預(yù)覽
代碼實(shí)現(xiàn)
首先npm i docx-preview
引入renderAsync方法
將blob數(shù)據(jù)流傳入方法中,渲染word文檔
import { defaultOptions, renderAsync } from "docx-preview";
renderAsync(buffer, document.getElementById("container"), null,
options: {
className: string = "docx", // 默認(rèn)和文檔樣式類的類名/前綴
inWrapper: boolean = true, // 啟用圍繞文檔內(nèi)容渲染包裝器
ignoreWidth: boolean = false, // 禁止頁(yè)面渲染寬度
ignoreHeight: boolean = false, // 禁止頁(yè)面渲染高度
ignoreFonts: boolean = false, // 禁止字體渲染
breakPages: boolean = true, // 在分頁(yè)符上啟用分頁(yè)
ignoreLastRenderedPageBreak: boolean = true,//禁用lastRenderedPageBreak元素的分頁(yè)
experimental: boolean = false, //啟用實(shí)驗(yàn)性功能(制表符停止計(jì)算)
trimXmlDeclaration: boolean = true, //如果為真后雷,xml聲明將在解析之前從xml文檔中刪除
debug: boolean = false, // 啟用額外的日志記錄
}
);
pdf實(shí)現(xiàn)前端預(yù)覽
代碼實(shí)現(xiàn)
-首先npm i pdfjs-dist
-設(shè)置PDFJS.GlobalWorkerOptions.workerSrc的地址
-通過(guò)PDFJS.getDocument處理pdf數(shù)據(jù)季惯,返回一個(gè)對(duì)象pdfDoc
-通過(guò)pdfDoc.getPage單獨(dú)獲取第1頁(yè)的數(shù)據(jù)
-創(chuàng)建一個(gè)dom元素,設(shè)置元素的畫布屬性
-通過(guò)page.render方法臀突,將數(shù)據(jù)渲染到畫布上
import * as PDFJS from "pdfjs-dist/legacy/build/pdf";
// 設(shè)置pdf.worker.js文件的引入地址
PDFJS.GlobalWorkerOptions.workerSrc = require("pdfjs-dist/legacy/build/pdf.worker.entry.js");
// data是一個(gè)ArrayBuffer格式勉抓,也是一個(gè)buffer流的數(shù)據(jù)
PDFJS.getDocument(data).promise.then(pdfDoc=>{
const numPages = pdfDoc.numPages; // pdf的總頁(yè)數(shù)
// 獲取第1頁(yè)的數(shù)據(jù)
pdfDoc.getPage(1).then(page =>{
// 設(shè)置canvas相關(guān)的屬性
const canvas = document.getElementById("the_canvas");
const ctx = canvas.getContext("2d");
const dpr = window.devicePixelRatio || 1;
const bsr =
ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio ||
ctx.msBackingStorePixelRatio ||
ctx.oBackingStorePixelRatio ||
ctx.backingStorePixelRatio ||
1;
const ratio = dpr / bsr;
const viewport = page.getViewport({ scale: 1 });
canvas.width = viewport.width * ratio;
canvas.height = viewport.height * ratio;
canvas.style.width = viewport.width + "px";
canvas.style.height = viewport.height + "px";
ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
const renderContext = {
canvasContext: ctx,
viewport: viewport,
};
// 數(shù)據(jù)渲染到canvas畫布上
page.render(renderContext);
})
})
excel實(shí)現(xiàn)前端預(yù)覽
代碼實(shí)現(xiàn)
下載exceljs、handsontable的庫(kù)
通過(guò)exceljs讀取到文件的數(shù)據(jù)
通過(guò)workbook.getWorksheet方法獲取到每一個(gè)工作表的數(shù)據(jù)候学,將數(shù)據(jù)處理成一個(gè)二維數(shù)組的數(shù)據(jù)
引入@handsontable/vue的組件HotTable
通過(guò)settings屬性藕筋,將一些配置參數(shù)和二維數(shù)組數(shù)據(jù)傳入組件,渲染成excel樣式梳码,實(shí)現(xiàn)預(yù)覽
// 加載excel的數(shù)據(jù)
(new ExcelJS.Workbook().xlsx.load(buffer)).then(workbook=>{
// 獲取excel的第一頁(yè)的數(shù)據(jù)
const ws = workbook.getWorksheet(1);
// 獲取每一行的數(shù)據(jù)
const data = ws.getRows(1, ws.actualRowCount);
})
// 渲染頁(yè)面
import { HotTable } from "@handsontable/vue";
<hot-table :settings="hotSettings"></hot-table>
hotSettings = {
language: "zh-CN",
readOnly: true,
data: this.data,
cell: this.cell,
mergeCells: this.merge,
colHeaders: true,
rowHeaders: true,
height: "calc(100vh - 107px)",
// contextMenu: true,
// manualRowMove: true,
// 關(guān)閉外部點(diǎn)擊取消選中時(shí)間的行為
outsideClickDeselects: false,
// fillHandle: {
// direction: 'vertical',
// autoInsertRow: true
// },
// afterSelectionEnd: this.afterSelectionEnd,
// bindRowsWithHeaders: 'strict',
licenseKey: "non-commercial-and-evaluation"
}