Skip to content

Commit 892f99a

Browse files
laura-bergoensxav-car
andcommittedMar 24, 2025
tech(api): move in common src initialize fonts in pdf lib document
Co-authored-by: Xavier Carron <xavier.carron@pix.fr>
1 parent adef580 commit 892f99a

File tree

7 files changed

+48
-23
lines changed

7 files changed

+48
-23
lines changed
 

Diff for: ‎api/src/prescription/target-profile/application/presenter/pdf/learning-content-pdf-presenter.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'dayjs/locale/fr.js';
22

3-
import pdfLibFontKit from '@pdf-lib/fontkit';
43
import { PDFDocument } from 'pdf-lib';
54

65
import * as coverPageBuilder from './builder/cover-page-builder.js';
@@ -9,7 +8,7 @@ import { FontManager } from './manager/font-manager.js';
98
import * as TemplatePageManager from './manager/template-page-manager.js';
109

1110
const present = async function (learningContent, title, language) {
12-
const pdfDocument = await _initializeNewPDFDocument(pdfLibFontKit);
11+
const pdfDocument = await _initializeNewPDFDocument();
1312
coverPageBuilder.build(pdfDocument, title, language);
1413
const pdfBytes = await learningContentBuilder.build(pdfDocument, learningContent, language).save();
1514
return Buffer.from(pdfBytes);
@@ -18,13 +17,11 @@ const present = async function (learningContent, title, language) {
1817
export { present };
1918

2019
/**
21-
* @param fontKit {Fontkit}
2220
* @returns {Promise<PDFDocument>}
2321
* @private
2422
*/
25-
async function _initializeNewPDFDocument(fontKit) {
23+
async function _initializeNewPDFDocument() {
2624
const pdfDocument = await PDFDocument.create();
27-
pdfDocument.registerFontkit(fontKit);
2825
await FontManager.initializeFonts(pdfDocument);
2926
await TemplatePageManager.initializeTemplatesPages(pdfDocument);
3027
return pdfDocument;

Diff for: ‎api/src/prescription/target-profile/application/presenter/pdf/manager/font-manager.js

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { readFile } from 'node:fs/promises';
2-
import * as url from 'node:url';
1+
import { FONTS, initializeFonts } from '../../../../../../shared/infrastructure/serializers/pdf/utils.js';
32

4-
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
5-
6-
const embeddedFonts = {};
3+
let embeddedFonts = {};
74
const COVER_PAGE_VERSION_TEXT_FONT_SIZE = 20;
85
const COVER_PAGE_LEGAL_MENTION_TEXT_FONT_SIZE = 10;
96
const COVER_PAGE_TITLE_TEXT_FONT_SIZE = 30;
@@ -14,13 +11,6 @@ const TUBE_TITLE_TEXT_FONT_SIZE = 7;
1411
const TUBE_DESCRIPTION_TEXT_FONT_SIZE = 7.5;
1512
const THEMATIC_TEXT_FONT_SIZE = 7;
1613

17-
const fonts = {
18-
robotoCondensedBold: 'RobotoCondensed-Bold.ttf',
19-
robotoCondensedLight: 'RobotoCondensed-Light.ttf',
20-
robotoCondensedRegular: 'RobotoCondensed-Regular.ttf',
21-
robotoRegular: 'Roboto-Regular.ttf',
22-
};
23-
2414
const FontManager = {
2515
key: {
2616
robotoCondensedBold: 'robotoCondensedBold',
@@ -178,15 +168,16 @@ const FontManager = {
178168

179169
/**
180170
* @param pdfDocument {PDFDocument}
181-
* @param dirname {string}
182171
* @returns {Promise<void>}
183172
*/
184173
async initializeFonts(pdfDocument) {
185-
for (const fontKey in fonts) {
186-
const fontFilename = fonts[fontKey];
187-
const fontFile = await readFile(`${__dirname}/fonts/${fontFilename}`);
188-
embeddedFonts[fontKey] = await pdfDocument.embedFont(fontFile, { subset: true, customName: fontFilename });
189-
}
174+
const newEmbeddedFonts = await initializeFonts(pdfDocument, [
175+
FONTS.robotoCondensedBold,
176+
FONTS.robotoCondensedLight,
177+
FONTS.robotoCondensedRegular,
178+
FONTS.robotoRegular,
179+
]);
180+
embeddedFonts = Object.assign(embeddedFonts, newEmbeddedFonts);
190181
},
191182
};
192183

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { readFile } from 'node:fs/promises';
2+
import * as url from 'node:url';
3+
4+
import pdfLibFontKit from '@pdf-lib/fontkit';
5+
6+
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
7+
8+
export const FONTS = {
9+
robotoCondensedBold: 'robotoCondensedBold',
10+
robotoCondensedLight: 'robotoCondensedLight',
11+
robotoCondensedRegular: 'robotoCondensedRegular',
12+
robotoRegular: 'robotoRegular',
13+
};
14+
15+
const FONT_FILES_BY_FONT_KEY = {
16+
[FONTS.robotoCondensedBold]: 'RobotoCondensed-Bold.ttf',
17+
[FONTS.robotoCondensedLight]: 'RobotoCondensed-Light.ttf',
18+
[FONTS.robotoCondensedRegular]: 'RobotoCondensed-Regular.ttf',
19+
[FONTS.robotoRegular]: 'Roboto-Regular.ttf',
20+
};
21+
22+
/**
23+
* @param pdfDocument {PDFDocument}
24+
* @param fontsKeysToEmbed {Array<string>}
25+
* @returns {Promise<Object<key: string, value: StandardFonts>>}
26+
*/
27+
export async function initializeFonts(pdfDocument, fontsKeysToEmbed) {
28+
pdfDocument.registerFontkit(pdfLibFontKit);
29+
const finalFontKeysToEmbed = fontsKeysToEmbed.length > 0 ? fontsKeysToEmbed : Object.values(FONTS);
30+
const embeddedFonts = {};
31+
for (const fontKey of finalFontKeysToEmbed) {
32+
const fontFilename = FONT_FILES_BY_FONT_KEY[fontKey];
33+
const fontFile = await readFile(`${__dirname}/fonts/${fontFilename}`);
34+
embeddedFonts[fontKey] = await pdfDocument.embedFont(fontFile, { subset: true, customName: fontFilename });
35+
}
36+
return embeddedFonts;
37+
}

0 commit comments

Comments
 (0)