|
|
@@ -21,10 +21,42 @@
|
|
|
import { ref, onMounted } from "vue"
|
|
|
import * as PDF from "pdfjs-dist"
|
|
|
|
|
|
-PDF.GlobalWorkerOptions.workerSrc = new URL(
|
|
|
- 'pdfjs-dist/build/pdf.worker.mjs',
|
|
|
- import.meta.url
|
|
|
-).toString()
|
|
|
+let workerBlobUrl = null
|
|
|
+
|
|
|
+const initWorker = async () => {
|
|
|
+ if (workerBlobUrl) return
|
|
|
+
|
|
|
+ // 尝试多个可能的 Worker 路径
|
|
|
+ const urls = [
|
|
|
+ // 1. 生产环境:相对于库文件的路径(已在 build:lib 时拷贝并重命名为 .js)
|
|
|
+ new URL("./pdf.worker.js", import.meta.url).toString(),
|
|
|
+ // 2. 开发环境:node_modules 路径
|
|
|
+ new URL("pdfjs-dist/build/pdf.worker.mjs", import.meta.url).toString(),
|
|
|
+ ]
|
|
|
+
|
|
|
+ let lastError = null
|
|
|
+ for (const url of urls) {
|
|
|
+ try {
|
|
|
+ // 方案:通过 Fetch 获取 worker 内容并转换为 Blob URL
|
|
|
+ // 这样可以绕过服务器对 .mjs 文件的 MIME 类型校验(application/octet-stream)
|
|
|
+ const response = await fetch(url)
|
|
|
+ if (!response.ok) continue
|
|
|
+
|
|
|
+ const arrayBuffer = await response.arrayBuffer()
|
|
|
+ const blob = new Blob([arrayBuffer], { type: "application/javascript" })
|
|
|
+ workerBlobUrl = URL.createObjectURL(blob)
|
|
|
+ PDF.GlobalWorkerOptions.workerSrc = workerBlobUrl
|
|
|
+ return // 成功加载则退出
|
|
|
+ } catch (e) {
|
|
|
+ lastError = e
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ console.warn("All PDF worker paths failed, using fallback", lastError)
|
|
|
+ // 最后的保底方案
|
|
|
+ PDF.GlobalWorkerOptions.workerSrc = urls[urls.length - 1]
|
|
|
+}
|
|
|
|
|
|
const props = defineProps({
|
|
|
data: Array,
|
|
|
@@ -60,6 +92,7 @@ const scaleX = () => {
|
|
|
}
|
|
|
|
|
|
const loadFile = async () => {
|
|
|
+ await initWorker()
|
|
|
pdfDoc = await PDF.getDocument(props.data).promise
|
|
|
pdf_pages.value = pdfDoc.numPages
|
|
|
renderPage()
|