日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pdf、markdown、docx文件预览

發(fā)布時間:2023/12/18 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pdf、markdown、docx文件预览 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

記錄一下實現(xiàn) .md \ .pdf \ .docx文件的預覽。

markdown 文件預覽

安裝 markdown-it

$> npm install --save markdown-it

解析.md文件轉(zhuǎn)換為 html,然后添加到 dom 節(jié)點中。

import MarkdownIt from "markdown-it";function parseFile(fileUrl) {// 創(chuàng)建實例const md = new MarkdownIt({html: true, // Enable HTML tags in sourcexhtmlOut: false, // Use '/' to close single tags (<br />).// This is only for full CommonMark compatibility.breaks: false, // Convert '\n' in paragraphs into <br>langPrefix: "language-", // CSS language prefix for fenced blocks. Can be// useful for external highlighters.linkify: false, // Autoconvert URL-like text to links// Enable some language-neutral replacement + quotes beautification// For the full list of replacements, see https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/replacements.jstypographer: false,// Double + single quotes replacement pairs, when typographer enabled,// and smartquotes on. Could be either a String or an Array.//// For example, you can use '???“' for Russian, '?“?‘' for German,// and ['?\xA0', '\xA0?', '?\xA0', '\xA0?'] for French (including nbsp).quotes: "“”‘’",});// 解析文件const html = md.render(fileUrl);return html; }

拿到轉(zhuǎn)換后的 html,插入到 dom 節(jié)點中,這里給一個 vue 的示例

<template><div class="md-preview"><div class="content"><div className="markdown-content" v-html="content" /></div></div> </template> <script> import { onMounted, ref, computed } from "vue"; // md import ReadME from "@/README.md?raw";// 解析后的內(nèi)容變量 const content = ref("");onMounted(() => {//content = parseFile(ReadME); }); </script>

highlight.js 代碼高亮

解析出來的內(nèi)容沒有任何樣式,通常我們的代碼使用高亮,方便閱讀。

安裝

$> npm install highlight.js

通過屬性highlight定義需要處理的代碼片段

const md = new MarkdownIt({// ...highlight: (str, lang) => {if (lang && hljs.getLanguage(lang)) {try {return ('<pre class="hljs"><code>' +hljs.highlight(str, {language: lang,ignoreIllegals: true,}).value +"</code></pre>");} catch (__) {}}return ('<pre class="hljs"><code>' + md.utils.escapeHtml(str) + "</code></pre>");}, });

此時只加了基本的樣式,可以通過導入不同主題的.css文件,來應用樣式。主題文件目錄highlight.js/styles/**

在頂部導入樣式文件,

import "highlight.js/styles/atom-one-dark.css";

markdown-it-anchor 增加導航標識

通過標題導航到指定的文字,通常# 會被轉(zhuǎn)換為h . 通過配置增加永久性導航設置

安裝

@sindresorhus/slugify 用來處理獲取到的文本內(nèi)容,有時候我們的標題中會含有一些特殊字符。能很好的處理

$> npm install markdown-it-anchor @sindresorhus/slugify --save

MarkdownIt 通過 use 方法安裝插件,

import MarkdownItAnchor from "markdown-it-anchor"; import slugify from "@sindresorhus/slugify";const md = new MarkdownIt({// ... }); md.use(MarkdownItAnchor, {level: 1,slugify: (s) => {return slugify(s);},getTokensText(tokens) {return tokens.filter((t) => ["text", "code_inline"].includes(t.type)).map((t) => t.content).join("");}, });

可以看到轉(zhuǎn)換后的 html 內(nèi)容中,所有的標題都包含有 id 屬性,通過permalink 生成永久性導航鏈接,

md.use(MarkdownItAnchor, {// ...permalink: MarkdownItAnchor.permalink.headerLink(), });

可以在生成的 html 中看到h標題中有<a class="header-anchor" href="#title"></a>

node-html-parser 生成簡單 dom 結(jié)構(gòu),支持 query 節(jié)點

在轉(zhuǎn)換后的 html 中已經(jīng)包含了可導航的標題信息;再從中提取出標題數(shù)據(jù),即可自定義渲染導航菜單。

$> npm install node-html-parser --save

以 vue 代碼示例

import { parse } from "node-html-parser";// 通過計算屬性,等待md文件解析完后生成對應的導航數(shù)據(jù) const navData = computed(() => {const root = parse(content.value);const match = [];for (const h of root.querySelectorAll("h1, h2, h3, h4, h5, h6")) {const slug = h.getAttribute("id") || slugify(h.textContent);h.setAttribute("id", slug);// h.innerHTML = `<a href="#${slug}>${h.innerHTML}</a>`match.push(`<a href="#${slug}" class="${h.rawTagName}">${h.innerHTML}</a>`);}return match.join(""); });

pdf 文件預覽

加載 pdf 文件,進行預覽。

安裝

$> npm install pdfjs-dist --save

vue3 中沒有 require,所以安裝其他依賴

# 支持es module $> npm install @bundled-es-modules/pdfjs-dist

通過讀取.pdf文件內(nèi)容,繪制到 canvas 中,在家動態(tài)生成的 canvas 節(jié)點添加到指定區(qū)域。

import pdfjs from "@bundled-es-modules/pdfjs-dist/build/pdf"; // import viewer from '@bundled-es-modules/pdfjs-dist/web/pdf_viewer'; import worker from "@bundled-es-modules/pdfjs-dist/build/pdf.worker.js?url";pdfjs.GlobalWorkerOptions.workerSrc = worker;function parsePdf(fileUrl) {const self = this;// 創(chuàng)建實例,加載文件const loadingTask = pdfjs.getDocument(fileUrl);// container box ref 引用const container = this.$refs.pdf;// 通過promise讀取loadingTask.promise.then(function (doc) {const numPages = doc.numPages;// meta 數(shù)據(jù)信息doc.getMetadata().then(function (data) {// 元數(shù)據(jù)信息});const loadPage = function (pageNum) {doc.getPage(pageNum).then(function (page) {const scale = 1;const viewport = page.getViewport({ scale });const outputScale = window.devicePixelRatio || 1;const canvas = document.createElement("canvas");const context = canvas.getContext("2d");// contianer 為預覽所在的dom區(qū)域container.appendChild(canvas);canvas.width = Math.floor(viewport.width * outputScale);canvas.height = Math.floor(viewport.height * outputScale);canvas.style.width = Math.floor(viewport.width) + "px";canvas.style.height = Math.floor(viewport.height) + "px";const transform =outputScale !== 1 ? [outputScale, 0, 0, outputScale, 0, 0] : null;const renderContext = {canvasContext: context,transform,viewport,};page.render(renderContext);});};for (let i = 1; i <= numPages; i++) {// 加載讀取每一頁數(shù)據(jù)loadPage(i);}}); }

那對應的 vue 示例,

<template><div class="pdf-preview"><div ref="pdf" /></div> </template>

docx文件預覽

加載 docx 文件數(shù)據(jù)流轉(zhuǎn)換為 html,然后插入 dom 節(jié)點中

安裝

$> npm install mammoth --save

瀏覽器使用引用的是mammoth/mammoth.browser,需要的數(shù)據(jù)格式必須是ArrayBuffer

示例是加載本地項目總的資源,通過網(wǎng)絡請求加在資源為數(shù)據(jù)流blob,然后通過FileReader轉(zhuǎn)換為 ArrayBuffer

此處以 vue 示例

<template><div class="docx-preview"><div ref="docx" v-html="content" /></div> </template> <script setup> import mammoth from 'mammoth/mammoth.browser' import { ref } from 'vue'const conent = ref('')function parseDocs(fileUrl){let $this = this// raadfileconst blob = await this.$http.request({type: 'get',url: fileUrl,responseType: 'blob',})// blob 轉(zhuǎn) arrayBufferconst fileReader = new FileReader()fileReader.onload = function (res) {// 解析轉(zhuǎn)換mammoth.convertToHtml({ arrayBuffer: fileReader.result }).then(function (result) {const html = result.value // The generated HTML$this.content = html}).done()}fileReader.readAsArrayBuffer(blob) } <script>

.docx只解析為 html,沒有任何樣式,需要自己去設置樣式。所以說如果就只是預覽,可以讓后端轉(zhuǎn)為 pdf 后前端在展示預覽。

具體實例代碼可參考示例項目:

vite+vue3 模板代碼倉庫(vite-vue3)[https://gitee.com/ngd_b/vue3-vite]

總結(jié)

以上是生活随笔為你收集整理的pdf、markdown、docx文件预览的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。