警告:loadFont - 翻译字体失败:“TypeError:e.peekBytes 不是函数”

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

我在 Angular18 项目中使用 PDF.js (pdfjs-dist v4.7.76 (最新)), pdf 查看器对于我上传的所有 PDF 都运行良好,除了一个:

几乎所有 PDF 文本均未显示: pdf rendered

(我应该看到什么) what I should see

我在控制台中收到此警告:

警告:loadFont - 翻译字体失败:“TypeError:e.peekBytes 不是函数”。

于是我调试了pdf.worker.min.mjs文件,发现错误来自于getFontFileType函数。

功能代码如下:

function getFontFileType(e, {type: t, subtype: i, composite: a}) {
    let s, r;
    if (function isTrueTypeFile(e) {
        const t = e.peekBytes(4);
        return 65536 === readUint32(t, 0) || "true" === bytesToString(t)
    }(e) || isTrueTypeCollectionFile(e))
        s = a ? "CIDFontType2" : "TrueType";
    else if (function isOpenTypeFile(e) {
        return "OTTO" === bytesToString(e.peekBytes(4))
    }(e))
        s = a ? "CIDFontType2" : "OpenType";
    else if (function isType1File(e) {
        const t = e.peekBytes(2);
        return 37 === t[0] && 33 === t[1] || 128 === t[0] && 1 === t[1]
    }(e))
        s = a ? "CIDFontType0" : "MMType1" === t ? "MMType1" : "Type1";
    else if (function isCFFFile(e) {
        const t = e.peekBytes(4);
        return t[0] >= 1 && t[3] >= 1 && t[3] <= 4
    }(e))
        if (a) {
            s = "CIDFontType0";
            r = "CIDFontType0C"
        } else {
            s = "MMType1" === t ? "MMType1" : "Type1";
            r = "Type1C"
        }
    else {
        warn("getFontFileType: Unable to detect correct font file Type/Subtype.");
        s = t;
        r = i
    }
    return [s, r]
}

这就是错误的来源(函数的第 3 行): e variable content

错误是由于“e”变量的类型引起的,它应该是 Stream,但实际上是“Dict”。 这可能是由于奇怪的字体文件造成的,所以我尝试使用以下选项打开 pdf 文件:

 pdfjs.getDocument({
   url: 'the file url',
   disableFontFace: true, // this option
   disableStream: true, // or this option
   cMapUrl: 'https://cdn.jsdelivr.net/npm/[email protected]/cmaps/', // or these two options
   cMapPacked: true,
 });

或者我在渲染页面时添加了此选项:

page.render({
  canvasContext: context,
  viewport: viewport,
  renderingMode: 'svg',  // this line
})

但似乎没有人改变任何事情。所以请问,有人已经遇到这个问题了吗?你能想出办法解决吗?

编辑: 这是 pdf 文件: PDF文件

javascript typescript pdf.js pdfjs-dist
1个回答
0
投票

根据这个github问题,不考虑这种字体类型。

所以我通过在translateFont函数中的pdf.worker.min.mjs文件中添加一些行来解决这个问题:

if (E?.xref?.stream) {
  E = E.xref.stream;
}
if(u?.xref?.stream) {
  u = u.xref.stream;
}

这样就可以完美工作了。

我在 github 问题中的回答:Github 问题

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