Javascript 如何使pdf.js库在Internet explorer中工作

Javascript 如何使pdf.js库在Internet explorer中工作,javascript,pdf,canvas,modal-dialog,internet-explorer-11,Javascript,Pdf,Canvas,Modal Dialog,Internet Explorer 11,我使用了pdf.js和pdf.worker.js来显示弹出模式。除了IE,它在每个浏览器中都能正常工作。我看到了这个问题的不同答案,但没有一个对我有效。 我尝试了compatible.js使pdf.js正常工作,但没有效果。你们有谁知道这件事吗?? 求你了,我真的需要帮助 我用于以弹出模式显示pdf文档的代码如下: // shows te pdf in pop up function showPDF(pdf_url) { $("#pdf-loader"

我使用了
pdf.js
pdf.worker.js
来显示弹出模式。除了IE,它在每个浏览器中都能正常工作。我看到了这个问题的不同答案,但没有一个对我有效。 我尝试了
compatible.js
使
pdf.js
正常工作,但没有效果。你们有谁知道这件事吗?? 求你了,我真的需要帮助

我用于以弹出模式显示pdf文档的代码如下:

 // shows te pdf in pop up

        function showPDF(pdf_url) {
            $("#pdf-loader").show();

            PDFJS.getDocument({ url: pdf_url }).then(function (pdf_doc) {

                __PDF_DOC = pdf_doc;
                __TOTAL_PAGES = __PDF_DOC.numPages;

                // Hide the pdf loader and show pdf container in HTML
                $("#pdf-loader").hide();
                $("#pdf-contents").show();
                $("#pdf-total-pages").text(__TOTAL_PAGES);

                // Show the first page
                __PDF_DOC.getPage(1).then(handlePages);
            }).catch(function (error) {
                // If error re-show the upload button
                $("#pdf-loader").hide();
                $("#upload-button").show();
            });
        }

        // takes the pages of pdf
        function handlePages(page) {
            //This gives us the page's dimensions at full scale
            var viewport = page.getViewport(1);

            //We'll create a canvas for each page to draw it on
            var canvas = document.createElement("canvas");
            //canvas.style.display = "block";

            canvas.setAttribute("id", "pdf" + __CURRENT_PAGE);

            canvas.style.cssText = "border-bottom:1px solid #000000; cursor:crosshair; text-align:center; ";

            var context = canvas.getContext('2d');
            canvas.height = viewport.height;
            canvas.width = viewport.width;
            var a = canvas.width + 70;
            document.getElementById("con").style.width = (a + "px");
            if (a > 690) {
                console.log(a);
                $("#header").css({ marginLeft: "0px" });
                $("#header1").css({ marginLeft: "0px" });
            }
            else {
                $("#header").css({ marginLeft: "0px" });
                $("#header1").css({ marginLeft: "0px" });
            }
            //Draw it on the canvas
            page.render({ canvasContext: context, viewport: viewport });
            //Add it to the web page
            document.getElementById("div").appendChild(canvas);
            //document.body.appendChild(canvas);

            //Move to next page
            __CURRENT_PAGE++;
            if (__CURRENT_PAGE <= __TOTAL_PAGES) {
                __PDF_DOC.getPage(__CURRENT_PAGE).then(handlePages);
            }
        }
//在弹出窗口中显示te pdf
函数showPDF(pdf\U url){
$(“#pdf加载器”).show();
getDocument({url:pdf\u url})。然后(函数(pdf\u doc){
__PDF_DOC=PDF_DOC;
__总页数=\uuuuupdf\udoc.numPages;
//隐藏pdf加载程序并在HTML中显示pdf容器
$(“#pdf加载器”).hide();
$(“#pdf内容”).show();
$(“#pdf总页数”)。文本(#总页数);
//显示第一页
__PDF_DOC.getPage(1)。然后(手册页);
}).catch(函数(错误){
//如果出现错误,请重新显示上载按钮
$(“#pdf加载器”).hide();
$(“#上载按钮”).show();
});
}
//获取pdf的页面
功能手册页(第页){
//这为我们提供了全比例的页面尺寸
var viewport=page.getViewport(1);
//我们将为每个页面创建一个画布来绘制它
var canvas=document.createElement(“canvas”);
//canvas.style.display=“块”;
setAttribute(“id”、“pdf”+_当前页面);
canvas.style.cssText=“边框底部:1px实心#000000;光标:十字线;文本对齐:中心;”;
var context=canvas.getContext('2d');
canvas.height=viewport.height;
canvas.width=viewport.width;
var a=画布宽度+70;
document.getElementById(“con”).style.width=(a+“px”);
如果(a>690){
控制台日志(a);
$(“#header”).css({marginLeft:“0px”});
$(“#header1”).css({marginLeft:“0px”});
}
否则{
$(“#header”).css({marginLeft:“0px”});
$(“#header1”).css({marginLeft:“0px”});
}
//把它画在画布上
render({canvasContext:context,viewport:viewport});
//将其添加到网页
document.getElementById(“div”).appendChild(画布);
//document.body.appendChild(画布);
//移到下一页
__当前页面++;

如果(u\u当前页面可能您使用的是最新版本的pdf.js和pdf.worker.js版本。由于IE浏览器不支持ES6格式,在使用最新版本的pdf.js时,可能会有一些功能不支持IE 11浏览器

您可以尝试使用ES5版本的pdf.js和pdf.worker.js版本(这里有一个关于使用pdf.js的示例,它在我使用IE11时运行良好,您可以查看):

  • pdf.js ES5版本:
    https://mozilla.github.io/pdf.js/es5/build/pdf.js
  • pdf.worker.js ES5版本:
    https://mozilla.github.io/pdf.js/es5/build/pdf.worker.js

IE11不直接支持ES6功能,因此PDF.js库的某些功能在IE11中不起作用

您需要使用ES5版本的pdf.jspdf.worker.js。

其中一个:

从节点模块使用(“pdfjs-dist”库):

  • 导入“pdfjs dist/build/pdf.js”导入“pdfjs dist/es5/build/pdf.js”
  • 导入“pdfjs dist/build/pdf.worker.js”到=>导入“pdfjs-dist/es5/build/pdf.worker.js”
  • 使用ES5版本:

  • pdf.js
  • pdf.worker.js

  • 其他答案中提到的ES5版本实际上包括ES6代码(例如箭头函数) 返回此版本以确保ES5安全和IE兼容:


    有些字母在IE中没有显示,但在Firefox、Chrome和SafariLinks中看起来很好。这些是404链接从源代码编译而来的
    git switch--detach v1.4.20