如何使用cdn导入pdf.js



我想在我的一个项目中使用pdf.js,但我遇到了导入它的问题,基本上,CDN不起的作用

这是我的代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/pdfjs-dist@2.7.570/build/pdf.min.js"></script>
</head>
<body>
<canvas id="my-canvas"></canvas>
<script>
pdfjsLib.getDocument('./ahmed.pdf').then(doc => {
console.log("this file has" + doc._pdfInfo.numPages);
});
</script>
</body>
</html>

这就是我的控制台显示的错误

不推荐使用API:否;GlobalWorkerOptions.workerSrc";指定。

未捕获类型错误:pdfjsLib.getDocument(…(.then不是函数

那么我该怎么办才能解决这个问题,非常感谢

您必须将GlobalWorkerOptions.workerSrc设置为相同版本的/build/pdf.worker(.min).js

pdfjsLib.GlobalWorkerOptions.workerSrc =
"https://cdn.jsdelivr.net/npm/pdfjs-dist@2.7.570/build/pdf.worker.min.js"; 👈 
pdfjsLib.getDocument('./ahmed.pdf').promise.then(doc => {
console.log(`This document has ${doc._pdfInfo.numPages} pages.");
});

,正如@Pasi所提到的,你必须通过链接.promise来承诺.getDocument()。没有它,就没有.then()

请参阅;Hello World with document load error handling";此页面上的示例开始:https://mozilla.github.io/pdf.js/examples/

(您的代码段在getDocument()之后缺少.promise,并且设置了workerSrc属性(

最新更新