You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello I ahve a NodeJs environment, Typescript and using pdf-lib with '@pdf-lib/fontkit'.
I have a massive font to be able to support asian characters and I want to subset the font, so that only the used characters are included.
private async createFontSubset(fontBytes: Buffer, annotationMap: Map<number, Annotation[]>): Promise<Uint8Array> {
const font = fontkit.create(Uint8Array.from(fontBytes));
const textArray = [];
for (const annotations of annotationMap.values()) {
for (const annotation of annotations) {
textArray.push(annotation.text);
}
}
const finalString = textArray.join('');
const run = font.layout(finalString);
const subset = font.createSubset();
for (const glyph of run.glyphs) {
subset.includeGlyph(glyph.id);
}
const subsetStream = await subset.encodeStream();
const subsetFontData = await this.streamToUInt8Atrray(subsetStream);
console.log('Subset font data: ', subsetFontData);
return subsetFontData;
}
private async streamToUInt8Atrray(subsetStream: SubsetStream): Promise<Uint8Array> {
return new Promise((resolve, reject) => {
const chunks: Uint8Array[] = [];
subsetStream.on('data', (chunk: Uint8Array) => {
chunks.push(chunk);
});
subsetStream.on('end', () => {
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
const combined = new Uint8Array(totalLength);
let offset = 0;
for (const chunk of chunks) {
combined.set(chunk, offset);
offset += chunk.length;
}
resolve(combined);
});
});
}
Error processing the request: Error: Unknown font format
at Object.create (/Users/nnemes/Workspace/pdf-writer/node_modules/@pdf-lib/fontkit/dist/fontkit.umd.js:17842:12)
at Function.<anonymous> (/Users/nnemes/Workspace/pdf-writer/node_modules/pdf-lib/cjs/core/embedders/CustomFontEmbedder.js:39:58)
at step (/Users/nnemes/Workspace/pdf-writer/node_modules/tslib/tslib.js:141:27)
at Object.next (/Users/nnemes/Workspace/pdf-writer/node_modules/tslib/tslib.js:122:57)
at /Users/nnemes/Workspace/pdf-writer/node_modules/tslib/tslib.js:115:75
at new Promise (<anonymous>)
at Object.__awaiter (/Users/nnemes/Workspace/pdf-writer/node_modules/tslib/tslib.js:111:16)
at CustomFontEmbedder.for (/Users/nnemes/Workspace/pdf-writer/node_modules/pdf-lib/cjs/core/embedders/CustomFontEmbedder.js:35:24)
at PDFDocument.<anonymous> (/Users/nnemes/Workspace/pdf-writer/node_modules/pdf-lib/cjs/api/PDFDocument.js:883:79)
at step (/Users/nnemes/Workspace/pdf-writer/node_modules/tslib/tslib.js:141:27)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello I ahve a NodeJs environment, Typescript and using pdf-lib with '@pdf-lib/fontkit'.
I have a massive font to be able to support asian characters and I want to subset the font, so that only the used characters are included.
However, when I do this
I get an error that
Beta Was this translation helpful? Give feedback.
All reactions