javascript PDFJS 未在 pdf.worker.js (Backbone+Marionette+pdf.js) 中定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24912734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:34:45  来源:igfitidea点击:

PDFJS is not defined in pdf.worker.js (Backbone+Marionette+pdf.js)

javascriptpdfbackbone.jsmarionettepdf.js

提问by emiel187

I want to render a PDF inside a Backbone.js/Marionette View with pdf.js, but PDFJS is not defined in pdf.worker.js. It is however defined in the Backbone View itself and also accessible via the console.

我想使用 pdf.js 在 Backbone.js/Marionette 视图中渲染 PDF,但 PDFJS 未在 pdf.worker.js 中定义。然而,它是在 Backbone 视图本身中定义的,也可以通过控制台访问。

This is the code for the View:

这是视图的代码:

module.exports = PDFView = Marionette.ItemView.extend({
    template: require('../../../templates/pdf_frame.hbs'),
    onRender: function(){
        var PDF_PATH = '../../pdf/websummitpitch.pdf';
        var PAGE_NUMBER = 1;
        var PAGE_SCALE = 1.5;
        var SVG_NS = 'http://www.w3.org/2000/svg';

        if (PDFJS) {
            console.log("PDFJS is loaded in Backbone View");
        } else {
            console.log("PDFJS is NOT loaded in Backbone View");
        }

        PDFJS.workerSrc = "/js/pdf.worker.js";
        console.log("workerSrc is set to ", PDFJS.workerSrc);

        // Loading document and page text content
        PDFJS.getDocument({url: PDF_PATH}).then(function (pdfDocument) {
            console.log("get document");

            pdfDocument.getPage(PAGE_NUMBER).then(function (page) {
                console.log("get page");

                var viewport = page.getViewport(PAGE_SCALE);
                page.getTextContent().then(function (textContent) {
                    // building SVG and adding that to the DOM
                    var svg = this.buildSVG(viewport, textContent);

                    // render svg in <canvas id="render"> in template
                    document.getElementById('render').appendChild(svg);
                });
            });
        });
    },
    buildSVG: function(viewport, textContent) {
        // Building SVG with size of the viewport (for simplicity)
        var svg = document.createElementNS(SVG_NS, 'svg:svg');
        svg.setAttribute('width', viewport.width + 'px');
        svg.setAttribute('height', viewport.height + 'px');
        // items are transformed to have 1px font size
        svg.setAttribute('font-size', 1);

        // processing all items
        textContent.items.forEach(function (textItem) {
            // we have to take in account viewport transform, which incudes scale,
            // rotation and Y-axis flip, and not forgetting to flip text.
            var tx = PDFJS.Util.transform(
                PDFJS.Util.transform(viewport.transform, textItem.transform),
                [1, 0, 0, -1, 0, 0]);
            var style = textContent.styles[textItem.fontName];
            // adding text element
            var text = document.createElementNS(SVG_NS, 'svg:text');
            text.setAttribute('transform', 'matrix(' + tx.join(' ') + ')');
            text.setAttribute('font-family', style.fontFamily);
            text.textContent = textItem.str;
            svg.appendChild(text);
        });
        return svg;
    }
});

This is the console output:console outputThe blue is console input, so PDFJS is accessible via the console.

这是控制台输出:控制台输出蓝色是控制台输入,因此可以通过控制台访问 PDFJS。

This is where the reference error occurs (in pdf.worker.js):

这是发生引用错误的地方(在 pdf.worker.js 中):

PDFJS.version = '1.0.233';

It is one of the very first lines, where the version number is set. However PDFJS seems to be undefined.

它是设置版本号的第一行之一。然而 PDFJS 似乎是未定义的。

This is my question:

这是我的问题:

Why is it not defined? How should I fix this?

为什么没有定义?我应该如何解决这个问题?

采纳答案by emiel187

I got the pdf viewer to work. Instead of using:

我让pdf查看器开始工作。而不是使用:

 PDFJS.workerSrc = "/js/pdf.worker.js";

I just disabled the worker like this:

我只是像这样禁用了工人:

 PDFJS.disableWorker = true;

Now it magically works. Not sure why though.

现在它神奇地起作用了。不知道为什么。