PDFJS:无效的 PDF 结构

问题描述 投票:0回答:1

我正在尝试使用

pdf.js
从 pdf 文档中提取纯文本,但由于某种原因无法克服
Invalid PDF structure
错误。

我的代码如下:

const pdfjslib = require('pdfjs-dist');

const pdfPath = 'https://www.corenet.gov.sg/media/2268607/dc19-07.pdf'

var loadingTask = pdfjslib.getDocument(pdfPath);
loadingTask.promise.then(async (doc) => {
    console.log(doc);
    return null
})
.catch((err)=>{
    console.log(err)
});

我尝试过来自同一域的其他 pdf 文档,但都抛出相同的错误:

...
Warning: Ignoring invalid character "34" in hex string
Warning: Ignoring invalid character "104" in hex string
Warning: Indexing all PDF objects
{ Error
    at InvalidPDFExceptionClosure (.../pdf_test/node_modules/pdfjs-dist/build/pdf.js:658:35)
    at Object.<anonymous> (...pdf_test/node_modules/pdfjs-dist/build/pdf.js:661:2)
    at __w_pdfjs_require__ (.../pdf_test/node_modules/pdfjs-dist/build/pdf.js:52:30)
    at Object.defineProperty.value (...pdf_test/node_modules/pdfjs-dist/build/pdf.js:129:23)
    at __w_pdfjs_require__ (.../pdf_test/node_modules/pdfjs-dist/build/pdf.js:52:30)
    at pdfjsVersion (...pdf_test/node_modules/pdfjs-dist/build/pdf.js:116:18)
    at .../pdf_test/node_modules/pdfjs-dist/build/pdf.js:119:10
    at webpackUniversalModuleDefinition (.../pdf_test/node_modules/pdfjs-dist/build/pdf.js:25:20)
    at Object.<anonymous> (.../pdf_test/node_modules/pdfjs-dist/build/pdf.js:32:3)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
  name: 'InvalidPDFException',
  message: 'Invalid PDF structure' }

其他领域的其他 pdf 似乎也可以。请注意,从上述域下载 pdf 效果很好,并且可以在 Chrome 浏览器上查看。我怀疑pdf文档已损坏。我没有实现任何前端代码,因为上述代码的目的是将其托管在云上。

javascript pdf pdf.js
1个回答
0
投票

浏览器控制台日志错误并没有帮助我修复它。

我运行 PHP 应用程序 (Moodle),然后查看 PHP 错误日志,看到一些变量预计会在要生成的证书的 html 源正文中被替换。

检查您的后端应用程序是否有错误日志以及提供给 PDF.js 的 html 源正文是否缺少和未定义的变量。

尝试从头开始提供给 PDF.js 的 html 正文将有助于调试异常的来源。

© www.soinside.com 2019 - 2024. All rights reserved.