I am using PDF.js (pdfjs-dist v4.7.76 (latest)) in a Angular18 project,
The pdf viewer is working well for all PDF I uploaded, except with one:
Almost all the text of the PDF is not showing :
And I have this warning in the console :
Warning: loadFont – translateFont failed: “TypeError: e.peekBytes is not a function”.
So I debugged the pdf.worker.min.mjs file, and I found that the error come from the getFontFileType function.
Here’s the function code:
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]
}
And here’s where the error come from (line 3 of the function):
The error is due to the type of the “e” variable, that should be a Stream, but is a “Dict” instead.
This is probably due to a weird font file, so I tried to open the pdf file with these options:
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,
});
or I added this option when I render the page :
page.render({
canvasContext: context,
viewport: viewport,
renderingMode: 'svg', // this line
})
But no one seemed to change anything. So please, did anyone already have this issue? Can you think of a way to fix it?