我在 Angular18 项目中使用 PDF.js (pdfjs-dist v4.7.76 (最新)), pdf 查看器对于我上传的所有 PDF 都运行良好,除了一个:
我在控制台中收到此警告:
警告: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]
}
错误是由于“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文件