vue3: pdf.js 2.16.105 using typescript

npm create vite vuepdfpreview  //創建項目npm install vue-pdf-embed
npm install vue3-pdfjs
npm install pdfjs-dist@2.16.105 
<!--*                                |~~~~~~~|*                                |       |*                                |       |*                                |       |*                                |       |*                                |       |*     |~.\\\_\~~~~~~~~~~~~~~xx~~~         ~~~~~~~~~~~~~~~~~~~~~/_//;~|*     |  \  o \_         ,XXXXX),                         _..-~ o /  |*     |    ~~\  ~-.     XXXXX`)))),                 _.--~~   .-~~~   |*      ~~~~~~~`\   ~\~~~XXX' _/ ';))     |~~~~~~..-~     _.-~ ~~~~~~~*               `\   ~~--`_\~\, ;;;\)__.---.~~~      _.-~*                 ~-.       `:;;/;; \          _..-~~*                    ~-._      `''        /-~-~*                        `\              /  /*                          |         ,   | |*                           |  '        /  |*                            \/;          |*                             ;;          |*                             `;   .       |*                             |~~~-----.....|*                            | \             \*                           | /\~~--...__    |*                           (|  `\       __-\|*                           ||    \_   /~    |*                           |)     \~-'      |*                            |      | \      '*                            |      |  \    :*                             \     |  |    |*                              |    )  (    )*                               \  /;  /\  |*                               |    |/   |*                               |    |   |*                                \  .'  ||*                                |  |  | |*                                (  | |  |*                                |   \ \ |*                                || o `.)|*                                |`\\) |*                                |       |*                                |       |** @Author: geovindu* @Date: 2025-05-08 20:54:24* @LastEditors: geovindu* @LastEditTime: 2025-05-08 22:22:28* @FilePath: \vue\vuepdfpreview\src\App.vue* @Description: geovindu* @lib,packpage:* npm install vue-pdf-embed* npm install vue3-pdfjs*  npm install pdfjs-dist@2.16.105* @IDE: vscode* @jslib: node 20 vue.js 3.0* @OS: windows10* @database: mysql 8.0  sql server 2019 postgreSQL 16* Copyright (c) geovindu 2025 by geovindu@163.com, All Rights Reserved.--><template>
<div class="pdf-container"><PDFView :pdfUrl="pdfUrl" v-if="pdfUrl" /><div v-else >加載中...</div><div><a href="https://vite.dev" target="_blank"><img src="/vite.svg" class="logo" alt="Vite logo" /></a><a href="https://vuejs.org/" target="_blank"><img src="./assets/vue.svg" class="logo vue" alt="Vue logo" /></a></div><HelloWorld msg="Vite + Vue" /></div></template><script setup lang="ts">import HelloWorld from './components/HelloWorld.vue'//import PDFView from "./components/pdfPreview.vue" // 可以import PDFView from "./components/pdfjs.vue"   //可以//import jsPdf from "http://localhost:5173/pdfs/01.pdf"const pdfUrl = "./pdfs/09.pdf"</script><style scoped>
.logo {height: 6em;padding: 1.5em;will-change: filter;transition: filter 300ms;
}
.logo:hover {filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vue:hover {filter: drop-shadow(0 0 2em #42b883aa);
}
</style>
<!--*                        ::*                       :;J7, :,                        ::;7:*                       ,ivYi, ,                       ;LLLFS:*                       :iv7Yi                       :7ri;j5PL*                      ,:ivYLvr                    ,ivrrirrY2X,*                      :;r@Wwz.7r:                :ivu@kexianli.*                     :iL7::,:::iiirii:ii;::::,,irvF7rvvLujL7ur*                    ri::,:,::i:iiiiiii:i:irrv177JX7rYXqZEkvv17*                 ;i:, , ::::iirrririi:i:::iiir2XXvii;L8OGJr71i*               :,, ,,:   ,::ir@mingyi.irii:i:::j1jri7ZBOS7ivv,*                  ,::,    ::rv77iiiriii:iii:i::,rvLq@huhao.Li*              ,,      ,, ,:ir7ir::,:::i;ir:::i:i::rSGGYri712:*            :::  ,v7r:: ::rrv77:, ,, ,:i7rrii:::::, ir7ri7Lri*           ,     2OBBOi,iiir;r::        ,irriiii::,, ,iv7Luur:*         ,,     i78MBBi,:,:::,:,  :7FSL: ,iriii:::i::,,:rLqXv::*         :      iuMMP: :,:::,:ii;2GY7OBB0viiii:i:iii:i:::iJqL;::*        ,     ::::i   ,,,,, ::LuBBu BBBBBErii:i:i:i:i:i:i:r77ii*       ,       :       , ,,:::rruBZ1MBBqi, :,,,:::,::::::iiriri:*      ,               ,,,,::::i:  @arqiao.       ,:,, ,:::ii;i7:*     :,       rjujLYLi   ,,:::::,:::::::::,,   ,:i,:,,,,,::i:iii*     ::      BBBBBBBBB0,    ,,::: , ,:::::: ,      ,,,, ,,:::::::*     i,  ,  ,8BMMBBBBBBi     ,,:,,     ,,, , ,   , , , :,::ii::i::*     :      iZMOMOMBBM2::::::::::,,,,     ,,,,,,:,,,::::i:irr:i:::,*     i   ,,:;u0MBMOG1L:::i::::::  ,,,::,   ,,, ::::::i:i:iirii:i:i:*     :    ,iuUuuXUkFu7i:iii:i:::, :,:,: ::::::::i:i:::::iirr7iiri::*     :     :rk@Yizero.i:::::, ,:ii:::::::i:::::i::,::::iirrriiiri::,*      :      5BMBBBBBBSr:,::rv2kuii:::iii::,:i:,, , ,,:,:i@petermu.,*           , :r50EZ8MBBBBGOBBBZP7::::i::,:::::,: :,:,::i;rrririiii::*               :jujYY7LS0ujJL7r::,::i::,::::::::::::::iirirrrrrrr:ii:*            ,:  :@kevensun.:,:,,,::::i:i:::::,,::::::iir;ii;7v77;ii;i,*            ,,,     ,,:,::::::i:iiiii:i::::,, ::::iiiir@xingjief.r;7:i,*         , , ,,,:,,::::::::iiiiiiiiii:,:,:::::::::iiir;ri7vL77rrirri::*          :,, , ::::::::i:::i:::i:i::,,,,,:,::i:i:::iir;@Secbone.ii:::** @Author: geovindu* @Date: 2025-05-08 21:00:52* @LastEditors: geovindu* @LastEditTime: 2025-05-08 22:47:03* @FilePath: \vue\vuepdfpreview\src\components\pdfjs.vue* @Description: geovindu* @lib,packpage:** @IDE: vscode* @jslib: node 20 vue.js 3.0* @OS: windows10* @database: mysql 8.0  sql server 2019 postgreSQL 16* Copyright (c) geovindu 2025 by geovindu@163.com, All Rights Reserved.-->
<template><div class="pdf-container"><div v-if="loading" class="text-center py-8">加載中...</div><div v-else-if="error" class="text-center py-8 text-red-500">{{ error }}</div><div v-else><div class="flex justify-between items-center mb-4"><div class="flex space-x-2"><button @click="zoomIn" class="px-3 py-1 bg-blue-500 text-white rounded hover:bg-blue-600">放大</button><button @click="zoomOut" class="px-3 py-1 bg-blue-500 text-white rounded hover:bg-blue-600">縮小</button><button @click="downloadPDF" class="px-3 py-1 bg-green-500 text-white rounded hover:bg-green-600">下載文檔</button></div><div class="text-center">第 {{ currentPage }} / {{ totalPages }} 頁<button @click="prevPage" :disabled="currentPage <= 1" class="px-3 py-1 bg-gray-200 rounded hover:bg-gray-300">上一頁</button><button @click="nextPage" :disabled="currentPage >= totalPages" class="px-3 py-1 bg-gray-200 rounded hover:bg-gray-300">下一頁</button></div></div><div id="pdf-container" class="w-full h-[600px] border border-gray-300 overflow-auto"><canvas ref="pdfCanvas"></canvas></div></div></div></template><script setup>import { ref, onMounted, reactive } from 'vue';import * as pdfjsLib from 'pdfjs-dist';// 設置 worker 路徑pdfjsLib.GlobalWorkerOptions.workerSrc = '/pdfjs/pdf.worker.js';const props = defineProps({pdfUrl: { type: String, required: true }});const pdfCanvas = ref(null);const loading = ref(true);const error = ref('');const currentPage = ref(1);const totalPages = ref(0);const scale = ref(1.0);let pdfDoc = null;const renderPage = async (num) => {try {const page = await pdfDoc.getPage(num);const viewport = page.getViewport({ scale: scale.value });// 設置 canvas 尺寸pdfCanvas.value.width = viewport.width;pdfCanvas.value.height = viewport.height;// 渲染頁面const renderContext = {canvasContext: pdfCanvas.value.getContext('2d'),viewport: viewport};await page.render(renderContext).promise;currentPage.value = num;} catch (err) {console.error('渲染頁面失敗:', err);error.value = `渲染失敗: ${err.message}`;}};const prevPage = () => {if (currentPage.value > 1) {renderPage(currentPage.value - 1);}};const nextPage = () => {if (currentPage.value < totalPages.value) {renderPage(currentPage.value + 1);}};const zoomIn = () => {scale.value += 0.1;renderPage(currentPage.value);};const zoomOut = () => {if (scale.value > 0.2) {scale.value -= 0.1;renderPage(currentPage.value);}};const downloadPDF = () => {const link = document.createElement('a');link.href = props.pdfUrl;link.download = props.pdfUrl.split('/').pop();link.click();};onMounted(async () => {try {// 加載 PDFconst loadingTask = pdfjsLib.getDocument(props.pdfUrl);pdfDoc = await loadingTask.promise;totalPages.value = pdfDoc.numPages;// 渲染第一頁renderPage(1);loading.value = false;} catch (err) {console.error('加載 PDF 失敗:', err);error.value = `加載失敗: ${err.message}`;loading.value = false;}});</script><style scoped>.pdf-container {max-width: 1000px;margin: 0 auto;}#pdf-container canvas {max-width: 100%;display: block;margin: 0 auto;}</style>

onMounted(async () => {try {// 假設props.pdfUrl現在是Base64編碼的PDF字符串// 移除可能存在的Data URI前綴(如"data:application/pdf;base64,")const base64Data = props.pdfUrl.replace(/^data:application\/pdf;base64,/, '');// 將Base64字符串轉換為Uint8Arrayconst binaryData = atob(base64Data);const arrayBuffer = new ArrayBuffer(binaryData.length);const uint8Array = new Uint8Array(arrayBuffer);for (let i = 0; i < binaryData.length; i++) {uint8Array[i] = binaryData.charCodeAt(i);}// 使用Uint8Array加載PDFconst loadingTask = pdfjsLib.getDocument({ data: uint8Array });pdfDoc = await loadingTask.promise;totalPages.value = pdfDoc.numPages;// 渲染第一頁renderPage(1);loading.value = false;} catch (err) {console.error('加載PDF失敗:', err);error.value = `加載失敗: ${err.message}`;loading.value = false;}
});

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/82442.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/82442.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/82442.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java面試全棧解析:Spring Boot、Kafka與Redis實戰揭秘

《Java面試全棧解析&#xff1a;Spring Boot、Kafka與Redis實戰揭秘》 【面試現場】 面試官&#xff1a;&#xff08;推了推眼鏡&#xff09;小張&#xff0c;你簡歷里提到用Spring Boot開發過微服務系統&#xff0c;能說說自動配置的實現原理嗎&#xff1f; 程序員&#xff1…

常見的提示詞攻擊方法 和防御手段——提示詞注入(Prompt Injection)攻擊解析

提示詞注入&#xff08;Prompt Injection&#xff09;攻擊解析 提示詞注入是一種針對大型語言模型&#xff08;LLM&#xff09;的新型攻擊手段&#xff0c;攻擊者通過精心設計的輸入文本&#xff08;提示詞&#xff09;操控AI模型的輸出&#xff0c;使其執行非預期行為或泄露敏…

基于NI-PXI的HIL系統開發

基于NI-PXI平臺的汽車電控單元HIL系統開發全解析 引言&#xff1a;HIL系統如何成為汽車電控開發的“效率倍增器”&#xff1f; 某車企通過基于NI-PXI的HIL系統&#xff0c;將懸架控制器的測試周期從3個月壓縮至2周&#xff0c;故障檢出率提升65%。這背后是硬件在環技術對汽車電…

復合機器人案例啟示:富唯智能如何以模塊化創新引領工業自動化新標桿

在國產工業機器人加速突圍的浪潮中&#xff0c;富唯智能復合機器人案例憑借其高精度焊接與智能控制技術&#xff0c;成為行業標桿。然而&#xff0c;隨著制造業對柔性化、全場景協作需求的升級&#xff0c;復合機器人正從單一功能向多模態協同進化。作為這一領域的創新者&#…

如何使用極狐GitLab 軟件包倉庫功能托管 python?

極狐GitLab 是 GitLab 在中國的發行版&#xff0c;關于中文參考文檔和資料有&#xff1a; 極狐GitLab 中文文檔極狐GitLab 中文論壇極狐GitLab 官網 軟件包庫中的 PyPI 包 (BASIC ALL) 在項目的軟件包庫中發布 PyPI 包。然后在需要將它們用作依賴項時安裝它們。 軟件包庫適用…

K8s中的containerPort與port、targetPort、nodePort的關系:

pod中的containerPort與service中的port、targetPort、nodePort的關系&#xff1a; 1、containerPort為pod的配置&#xff0c;對應pod內部服務監聽的具體端口&#xff0c;例如nginx服務默認監聽80端口&#xff0c;那么nginx的pod的containerPort應該配置為80&#xff0c;例如m…

面試題:QTableView和QTableWidget的異同

目錄 1.QTableView簡介 2.QTableWidget簡介 3.QTableView和QTableWidget不同 4.總結 1.QTableView簡介 QTableView是一個基于模型-視圖架構的表格控件&#xff0c;用于展示表格形式的數據。同樣需要關聯一個QAbstractTableModel或其子類&#xff08;如QStandardItemModel&a…

smbd:快速拉取服務端SMB共享文件腳本工具

地址:https://github.com/MartinxMax/smbd smbd 是一款簡單的 Bash 腳本&#xff0c;用於自動化從共享目錄透過 SMB/CIFS 協議下載檔案的過程。此工具設計用來與遠端 SMB 分享進行互動&#xff0c;並將其內容下載到本地目錄中。 環境需求 $ apt install smbclient 使用方式 …

MiInsertVad函數分析之nt!MMVAD結構

MiInsertVad函數分析之nt!MMVAD結構 1: kd> dt nt!MMVAD 89520270 0x000 u1 : __unnamed 0x004 LeftChild : (null) 0x008 RightChild : (null) 0x00c StartingVpn : 0x2b0 0x010 EndingVpn : 0x2c5 0x014 u …

OSPF不規則區域劃分

1、建立一條虛鏈路 vlink 可以被視為是??區域的?段延伸。 這?條虛擬的鏈路&#xff0c;只能夠跨域?個???區域。 [r2-ospf-1-area-0.0.0.1]vlink-peer 3.3.3.3 [r3-ospf-1-area-0.0.0.1]vlink-peer 2.2.2.2 在沒有建立虛鏈路之前,r1是不能ping r4的。vlink建?的鄰居關…

Webug4.0靶場通關筆記-靶場搭建方法(3種方法)

目錄 一、虛擬機綠色版本 1. 開啟phpstudy 2. 訪問靶場 二、Docker版本 1.拉取鏡像 2.啟動鏡像 三、源碼安裝版本 1. 搭建環境 &#xff08;1&#xff09;安裝PHPStudy &#xff08;2&#xff09;WeBug4.0靶場源碼 &#xff08;3&#xff09;安裝Navicat &#xff…

Linux:進程間通信---消息隊列信號量

文章目錄 1.消息隊列1.1 消息隊列的原理1.2 消息隊列的系統接口 2. 信號量2.1 信號量的系統調用接口 3. 淺談進程間通信3.1 IPC在內核中數據結構設計3.2 共享內存的缺點3.3 理解信號量 序&#xff1a;在上一章中&#xff0c;我們引出了命名管道和共享內存的概念&#xff0c;了解…

電子電器架構 --- 車載網關的設計

我是穿拖鞋的漢子&#xff0c;魔都中堅持長期主義的汽車電子工程師。 老規矩&#xff0c;分享一段喜歡的文字&#xff0c;避免自己成為高知識低文化的工程師&#xff1a; 鈍感力的“鈍”&#xff0c;不是木訥、遲鈍&#xff0c;而是直面困境的韌勁和耐力&#xff0c;是面對外界…

華為云Git使用與GitCode操作指南

案例介紹 本文檔帶領開發者學習如何在云主機上基于GitCode來使用Git來管理自己的項目代碼,并使用一些常用的Git命令來進行Git環境的設置。 案例內容 1 概述 1.1 背景介紹 Git 是一個快速、可擴展的分布式版本控制系統,它擁有異常豐富的命令集,可以提供高級操作和對內部…

ESP32開發入門(七):HTTP開發實踐

一、HTTP協議基礎 1.1 什么是HTTP&#xff1f; HTTP&#xff08;HyperText Transfer Protocol&#xff0c;超文本傳輸協議&#xff09;是互聯網上應用最為廣泛的一種網絡協議&#xff0c;用于從服務器傳輸超文本到本地瀏覽器。它是一種無狀態的請求/響應協議&#xff0c;工作…

Python 對象引用、可變性和垃圾 回收(變量不是盒子)

變量不是盒子 1997 年夏天&#xff0c;我在 MIT 學了一門 Java 課程。Lynn Andrea Stein 教授 &#xff08;一位獲獎的計算機科學教育工作者&#xff0c;目前在歐林工程學院教書&#xff09;指 出&#xff0c;人們經常使用“變量是盒子”這樣的比喻&#xff0c;但是這有礙于理…

局域網常用的測速工具,Iperf3使用教程

目錄 下載方式 Windows Linux 使用方法&#xff1a;測試局域網帶寬 步驟一&#xff1a;服務端準備 步驟二&#xff1a;客戶端發起連接 步驟三&#xff1a;查看結果 參數說明 1. Iperf常用參數&#xff08;測試夠用&#xff09; 2. 通用參數&#xff08;Server端和Cli…

《深入理解分布式系統》之認識分布式系統

本文是閱讀深入理解分布式系統第一章認識分布式系統時的筆記。 分布式系統的特點 多進程不共享操作系統不共享時鐘 分布式系統 由多個可獨立運行的子系統組成。每個子系統可以獨立選擇運行平臺。不同的運行平臺存在差異&#xff0c;比如操作系統&#xff0c;硬件規格等。由…

UE5 PCG學習筆記

https://www.bilibili.com/video/BV1onUdY2Ei3/?spm_id_from333.337.search-card.all.click&vd_source707ec8983cc32e6e065d5496a7f79ee6 一、安裝PCG 插件里選擇以下進行安裝 移動目錄后&#xff0c;可以使用 Update Redirector References&#xff0c;更新下&#xff0…

工業現場ModbusTCP轉EtherNETIP網關引領生物現場領新浪潮

生物質發生器是一種能夠產生、培養生物的設備。客戶現場需要將生物發生器連接到羅克韋爾系統&#xff0c;但是二者協議無法直接通訊&#xff0c;需要通過ModbusTCP轉Ethernet/IP網關將兩者進行通訊連接&#xff0c;生物質發生器以其獨特的工作原理和優勢&#xff0c;使得生物的…