【问题标题】:Warning: Error during font loading: Maximum call stack size exceeded error with PDFJS on rails警告:字体加载期间出错:在 Rails 上使用 PDFJS 时超出最大调用堆栈大小错误
【发布时间】:2016-08-01 03:14:54
【问题描述】:

我正在尝试在我的 Rails 应用程序上使用 PDFJS 加载 pdf。 仅加载图像,而不会渲染任何文本。

控制台说:

Warning: Error during font loading: Maximum call stack size exceeded

奇怪的是,它可以在 localhost 和 Firefox 上完美运行。 它适用于本地主机上的任何浏览器。该错误仅出现在生产服务器上。

这是我的配置。

应用程序.js

//= require compatibility
//= require pdf
//= require pdf_viewer
//= require simpleviewer

simpleviewer.js.erb

$(function(){

  'use strict';

  if (!PDFJS.PDFViewer || !PDFJS.getDocument) {
    alert('Please build the library and components using\n' +
          '  `node make generic components`');
  }

  PDFJS.workerSrc = "<%= asset_path('pdf.worker.js') %>";

  $('[data-type=pdf]').each(function(){
    var pdfUrl =$(this).attr('data-url');
    var container = $(this).find('#viewerContainer')[0];
    renderPdf(pdfUrl,container)
  })

  function renderPdf(pdfUrl, container){
    var pdfLinkService = new PDFJS.PDFLinkService();
    var pdfViewer = new PDFJS.PDFViewer({
      container: container,
      linkService: pdfLinkService,
    });
    pdfLinkService.setViewer(pdfViewer);

    container.addEventListener('pagesinit', function () {
      pdfViewer.currentScaleValue = 'page-width';
    });

    PDFJS.getDocument(pdfUrl).then(function (pdfDocument) {
      pdfViewer.setDocument(pdfDocument);
      pdfLinkService.setDocument(pdfDocument, null);
    });
  }
})

pdf_show.erb

<div class="pdf_show" data-type="pdf" data-url="#{pdf_url}">
  <div id="viewerContainer">
    <div id="viewer" class="pdfViewer">
    </div>
  </div>
</div>

非常感谢。

【问题讨论】:

    标签: javascript ruby-on-rails google-chrome pdf pdfjs


    【解决方案1】:

    正如https://github.com/mozilla/pdf.js/issues/7044 中提到的,这是 Chrome 运行 Uglified 代码的问题。您可以使用一种解决方法:应用 {compress: {sequences: false}} 选项(请参阅 source)或禁用 JS 压缩。 (您也可以将问题报告给 Chrome 错误跟踪系统)。

    【讨论】:

    【解决方案2】:

    大家都提到这个错误是由于chrome无法正确读取缩小后的代码。我尝试有选择地禁用 Rails 资产压缩器,但失败了。

    所以对于解决方法,我使用这个 cdn 。 我不知道这是不是官方的CDN。

    https://npmcdn.com/pdfjs-dist@1.4.79/build/pdf.combined.js
    

    更新:我正在使用的 CDN 使用的是假工人,没有缩小,我找不到任何其他用于 pdfjs 的 CDN。所以我在 AWS S3 上托管了缩小的 pdf.js 和 pdf.worker.js 并将其用作我自己的 cdn。

    【讨论】:

    • 通过执行上述操作,您将: 1) 禁用 web worker(这意味着浏览器可能会抛出“长时间运行的脚本”对话框); 2) 仍然没有压缩代码。
    • 是的,你是对的@async5。所以现在我在 S3 上托管缩小的 pdf.js 和 pdf.worker.js 并将其用作 cdn。它现在工作。谢谢!
    猜你喜欢
    • 2023-02-04
    • 2011-08-31
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 2016-09-26
    • 2013-08-19
    相关资源
    最近更新 更多