-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenerateRasterPDF.js
More file actions
957 lines (803 loc) · 32.8 KB
/
GenerateRasterPDF.js
File metadata and controls
957 lines (803 loc) · 32.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
/**
* ============================================================================
* CV PDF GENERATOR - Screenshot to PDF Approach
* ============================================================================
*
* Cette approche garantit un rendu IDENTIQUE au navigateur :
* 1. Capture un screenshot complet du CV en mode "screen"
* 2. Découpe intelligemment l'image en tranches de hauteur A4
* 3. Recompose un PDF multi-pages à partir des tranches
*
* ✅ Pas de @media print appliqué
* ✅ Rendu pixel-perfect identique à l'écran
* ✅ Pagination intelligente basée sur le contenu
* ✅ Fonts et ressources synchronisées
*
* USAGE:
* node generateCvPdf.js
* node generateCvPdf.js --locale fr --theme dark
* node generateCvPdf.js --url http://localhost:3000
*
* @requires @playwright/test pdf-lib sharp
*/
const { chromium } = require('@playwright/test');
const { PDFDocument } = require('pdf-lib');
const sharp = require('sharp');
const fs = require('fs').promises;
const fsSync = require('fs');
const path = require('path');
const yaml = require('yaml');
const { rehydratePdf } = require('./rehydrate-pdf');
// ============================================================================
// CONFIGURATION
// ============================================================================
const CONFIG = {
// Source HTML
templatePath: path.join(__dirname, 'index-template.html'),
localesPath: path.join(__dirname, 'locales'),
cssPath: path.join(__dirname, 'style.css'),
cssPdfPath: path.join(__dirname, 'style-pdf.css'), // CSS spécifique PDF
// Options disponibles
supportedLocales: ['fr', 'en'],
supportedThemes: ['dark', 'light'],
// Viewport optimal pour capturer le CV
// Largeur généreuse pour bon rendu, sera redimensionné à A4
viewport: {
width: 900, // Largeur confortable
height: 1273 // Ratio A4 (900 * 1.414)
},
// Dimensions A4 en pixels (96 DPI)
a4: {
widthPx: 794, // 210mm à 96 DPI
heightPx: 1123 // 297mm à 96 DPI
},
// Options de pagination
pagination: {
usePdfCss: true, // Utiliser style-pdf.css au lieu du zoom
targetPages: null, // Nombre de pages cibles (null = auto, utilisé seulement si usePdfCss = false)
smartBreak: true, // Découpage intelligent aux limites de sections
breakSelectors: ['.section', '.experience-item', '.project-item', 'h2'], // Éléments à ne pas couper
minSectionHeight: 100 // Hauteur minimale pour considérer une section
},
// Timeouts (ms)
timeout: {
navigation: 30000,
fonts: 10000,
render: 5000
},
// Output
outputDir: './exports',
tempDir: './exports/temp'
};
// ============================================================================
// UTILITIES - Chargement des données localisées
// ============================================================================
/**
* Charge les données YAML pour une locale donnée
*/
function loadLocale(localeName) {
try {
const yamlPath = path.join(CONFIG.localesPath, `${localeName}.yml`);
const yamlContent = fsSync.readFileSync(yamlPath, 'utf8');
return yaml.parse(yamlContent);
} catch (error) {
console.error(`❌ Erreur chargement locale ${localeName}:`, error.message);
return null;
}
}
/**
* Génère le HTML localisé avec thème
*/
function generateLocalizedHtml(templateHtml, localeData, localeName, themeName) {
const { JSDOM } = require('jsdom');
const dom = new JSDOM(templateHtml);
const { document } = dom.window;
document.documentElement.lang = localeName;
document.documentElement.setAttribute('data-theme', themeName);
if (localeData.title) document.title = localeData.title;
// Meta tags
const metaSelectors = {
'meta[name="description"]': localeData['profile-desc'],
'meta[name="keywords"]': localeData.keywords,
'meta[name="author"]': localeData.name,
'meta[property="og:title"]': localeData.title,
'meta[property="og:description"]': localeData['profile-desc'],
'meta[name="twitter:title"]': localeData.title,
'meta[name="twitter:description"]': localeData['profile-desc']
};
Object.entries(metaSelectors).forEach(([selector, content]) => {
const element = document.querySelector(selector);
if (element && content) {
element.setAttribute('content', content);
}
});
// Labels ARIA
const ariaElements = {
'lang-button': localeData['lang-label'],
'toggle': localeData['theme-label'],
'print-btn': localeData['download-label']
};
Object.entries(ariaElements).forEach(([id, label]) => {
const element = document.getElementById(id);
if (element && label) element.setAttribute('aria-label', label);
});
// Traductions i18n
const i18nElements = document.querySelectorAll('[data-i18n]');
i18nElements.forEach(element => {
const key = element.getAttribute('data-i18n');
const translation = localeData[key];
if (translation !== undefined) element.innerHTML = translation;
});
return dom.serialize();
}
// ============================================================================
// PLAYWRIGHT - Capture du rendu screen
// ============================================================================
/**
* Attend explicitement que toutes les webfonts soient chargées
*/
async function waitForFonts(page) {
try {
await page.waitForFunction(
() => document.fonts.ready,
{ timeout: CONFIG.timeout.fonts }
);
const allFontsLoaded = await page.evaluate(() => {
return Array.from(document.fonts).every(font => font.status === 'loaded');
});
if (!allFontsLoaded) {
console.warn('⚠️ Certaines fonts ne sont pas chargées, attente supplémentaire...');
await page.waitForTimeout(1000);
}
const fontCount = await page.evaluate(() => document.fonts.size);
console.log(`✓ ${fontCount} webfont(s) chargée(s)`);
return true;
} catch (error) {
console.warn('⚠️ Timeout attente fonts:', error.message);
return false;
}
}
/**
* Attend la stabilité complète du DOM
*/
async function waitForLayoutStability(page) {
await page.waitForLoadState('networkidle', {
timeout: CONFIG.timeout.navigation
});
await page.waitForTimeout(CONFIG.timeout.render);
const imagesLoaded = await page.evaluate(() => {
const images = Array.from(document.images);
return images.every(img => img.complete && img.naturalHeight !== 0);
});
if (!imagesLoaded) {
console.warn('⚠️ Certaines images ne sont pas chargées');
}
console.log('✓ Layout stabilisé');
}
/**
* Prépare la page pour la capture (désactive animations, masque boutons UI)
* Applique le mode PDF via CSS au lieu du zoom
*/
async function preparePage(page, usePdfMode = true) {
// Désactiver les animations
await page.addStyleTag({
content: `
*, *::before, *::after {
animation-duration: 0s !important;
animation-delay: 0s !important;
transition-duration: 0s !important;
transition-delay: 0s !important;
}
`
});
// Masquer les boutons d'interface
await page.evaluate(() => {
const buttonsToHide = document.querySelectorAll(
'#print-btn, #toggle, #lang-button, .lang-dropdown, .top-right-buttons'
);
buttonsToHide.forEach(btn => {
if (btn) btn.style.display = 'none';
});
});
// Activer le mode PDF si demandé
if (usePdfMode) {
await page.evaluate(() => {
// pdf-mode injection removed: PDF debug class should be added explicitly when needed
});
console.log('✓ Mode PDF activé (style-pdf.css appliqué)');
}
console.log('✓ Page préparée pour capture');
}
/**
* Analyse le DOM pour trouver les positions des sections
* Retourne un tableau de positions Y où il est "safe" de couper
* La coupure se fait au MILIEU de l'espace entre deux sections
*/
async function findSafeCutPoints(page) {
return await page.evaluate((config) => {
const cutPoints = [0]; // Début du document
// Trouver tous les éléments qui correspondent aux sélecteurs
const elements = [];
config.pagination.breakSelectors.forEach(selector => {
document.querySelectorAll(selector).forEach(el => elements.push({
element: el,
selector: selector
}));
});
// Trier les éléments par position Y
const sortedElements = elements
.map(({ element, selector }) => {
const rect = element.getBoundingClientRect();
const scrollY = window.pageYOffset || document.documentElement.scrollTop;
const absoluteY = rect.top + scrollY;
const style = window.getComputedStyle(element);
const marginTop = parseInt(style.marginTop) || 0;
return {
element,
selector,
y: absoluteY,
marginTop: marginTop,
height: rect.height
};
})
.filter(item => item.height >= config.pagination.minSectionHeight)
.sort((a, b) => a.y - b.y);
// Pour chaque élément (sauf le premier), calculer le milieu de l'espace avec l'élément précédent
for (let i = 1; i < sortedElements.length; i++) {
const prevElement = sortedElements[i - 1];
const currentElement = sortedElements[i];
// Position de fin de l'élément précédent
const prevEnd = prevElement.y + prevElement.height;
// Position de début de l'élément actuel (incluant son margin-top)
const currentStart = currentElement.y;
// Couper au milieu de l'espace entre les deux
const middlePoint = Math.round((prevEnd + currentStart) / 2);
cutPoints.push(middlePoint);
}
// Trier et dédupliquer
return [...new Set(cutPoints)].sort((a, b) => a - b);
}, CONFIG);
}
/**
* Capture un screenshot complet de la page en mode screen
*/
/**
* Capture des pages avec fenêtre virtuelle A4 scrollante + tiling haute résolution
* Concept:
* 1. Scale le contenu CSS (ex: ×2)
* 2. Capture des tiles en scrollant horizontalement puis verticalement
* 3. Stitch les tiles pour reconstituer chaque page A4
*/
async function captureWithVirtualA4Window(localizedHtml, outputDir, themeName, usePdfMode = true, scale = 1, tiles = '1x1') {
let browser;
try {
console.log('\n🚀 Lancement de Chromium...');
browser = await chromium.launch({
headless: true,
args: [
'--disable-web-security',
'--font-render-hinting=none',
'--disable-gpu',
'--no-sandbox'
]
});
const cssScale = isFinite(scale) && scale > 0 ? scale : 1;
const [cols, rows] = (typeof tiles === 'string' ? tiles.split('x').map(n => parseInt(n, 10)) : [1, 1]);
const context = await browser.newContext({
viewport: CONFIG.viewport,
deviceScaleFactor: 1, // On utilise CSS scale au lieu de deviceScaleFactor
hasTouch: false,
isMobile: false,
colorScheme: themeName === 'dark' ? 'dark' : 'light'
});
const page = await context.newPage();
// CRITIQUE: Forcer media='screen' pour éviter @media print
await page.emulateMedia({ media: 'screen' });
console.log('✓ Navigateur initialisé');
console.log(` Viewport: ${CONFIG.viewport.width}×${CONFIG.viewport.height}px`);
console.log(` Fenêtre virtuelle A4: ${CONFIG.a4.widthPx}×${CONFIG.a4.heightPx}px (ratio 1:1.414)`);
console.log(` Media: screen (${themeName}) - @media print désactivé`);
if (cssScale > 1) {
console.log(` Scale CSS: ×${cssScale} → Tiling ${cols}×${rows}`);
}
console.log('\n📄 Chargement du HTML...');
await page.setContent(localizedHtml, {
waitUntil: 'domcontentloaded'
});
console.log('\n⏳ Synchronisation du rendu...');
await waitForFonts(page);
await waitForLayoutStability(page);
await preparePage(page, usePdfMode);
// Obtenir la hauteur AVANT le zoom pour calculer les positions de scroll
const totalHeightPreScale = await page.evaluate(() => document.documentElement.scrollHeight);
const realContentHeightPreScale = await page.evaluate(() => {
try {
const children = Array.from(document.body.children).filter(el => getComputedStyle(el).display !== 'none');
if (!children.length) return document.documentElement.scrollHeight;
const last = children[children.length - 1];
const rect = last.getBoundingClientRect();
return Math.ceil(rect.bottom + window.scrollY);
} catch (e) {
return document.documentElement.scrollHeight;
}
});
console.log(` Hauteur réelle du contenu (pré-scale): ${realContentHeightPreScale}px`);
// Calculer la hauteur de la fenêtre virtuelle A4 (ratio fixe)
const windowWidth = CONFIG.viewport.width;
const windowHeight = Math.round(windowWidth * 1.414); // Ratio A4 exact
console.log(` Fenêtre virtuelle A4: ${windowWidth}×${windowHeight}px`);
// Analyser les sections pour ajuster les positions de scroll (AVANT zoom)
let scrollPositions = [0];
if (CONFIG.pagination.smartBreak) {
console.log('\n🔍 Analyse des sections pour ajustement du scroll...');
const safeCutPoints = await findSafeCutPoints(page);
console.log(`✓ ${safeCutPoints.length} point(s) de coupure détecté(s): ${safeCutPoints.join(', ')}`);
scrollPositions = findOptimalCutPositions(safeCutPoints, realContentHeightPreScale, windowHeight);
console.log(` Positions de scroll (pré-scale): ${scrollPositions.join(', ')}`);
} else {
const numPages = Math.ceil(realContentHeightPreScale / windowHeight);
scrollPositions = Array.from({ length: numPages }, (_, i) => i * windowHeight);
}
// Maintenant appliquer le zoom CSS APRÈS avoir calculé les positions
if (cssScale > 1) {
await page.evaluate((scaleValue) => {
const container = document.querySelector('.container');
const body = document.body;
const html = document.documentElement;
if (container) {
container.style.transformOrigin = 'top left';
container.style.transform = `scale(${scaleValue})`;
// Vérifier le background sur html puis body (copier TOUT le background)
let bgColor, bgImage, bgSize, bgPosition, bgRepeat;
const htmlBg = window.getComputedStyle(html).backgroundImage;
const bodyBg = window.getComputedStyle(body).backgroundImage;
if (htmlBg && htmlBg !== 'none') {
// Background sur html
const htmlStyle = window.getComputedStyle(html);
bgColor = htmlStyle.backgroundColor;
bgImage = htmlStyle.backgroundImage;
bgSize = htmlStyle.backgroundSize;
bgPosition = htmlStyle.backgroundPosition;
bgRepeat = htmlStyle.backgroundRepeat;
} else if (bodyBg && bodyBg !== 'none') {
// Background sur body
const bodyStyle = window.getComputedStyle(body);
bgColor = bodyStyle.backgroundColor;
bgImage = bodyStyle.backgroundImage;
bgSize = bodyStyle.backgroundSize;
bgPosition = bodyStyle.backgroundPosition;
bgRepeat = bodyStyle.backgroundRepeat;
} else {
// Pas d'image mais peut-être une couleur
const htmlStyle = window.getComputedStyle(html);
const bodyStyle = window.getComputedStyle(body);
bgColor = htmlStyle.backgroundColor !== 'rgba(0, 0, 0, 0)' ? htmlStyle.backgroundColor : bodyStyle.backgroundColor;
}
// Copier le background complet sur container
if (bgColor) container.style.backgroundColor = bgColor;
if (bgImage && bgImage !== 'none') {
container.style.backgroundImage = bgImage;
container.style.backgroundSize = bgSize;
container.style.backgroundPosition = bgPosition;
container.style.backgroundRepeat = bgRepeat;
container.style.backgroundAttachment = 'fixed'; // CRITIQUE: fixe le background pour éviter les décalages entre tiles
}
// Retirer le background du body ET html pour éviter la duplication
body.style.backgroundImage = 'none';
body.style.backgroundColor = 'transparent';
body.style.background = 'transparent';
html.style.backgroundImage = 'none';
html.style.backgroundColor = 'transparent';
html.style.background = 'transparent';
}
}, cssScale);
await page.waitForTimeout(200);
console.log(`✓ Container zoomé ×${cssScale} (background déplacé)`);
}
// Capturer chaque page avec tiling (scroll horizontal + vertical)
console.log(`\n📸 Capture avec tiling ${cols}×${rows} (${scrollPositions.length} pages)...`);
const screenshots = [];
for (let i = 0; i < scrollPositions.length; i++) {
const scrollYPage = scrollPositions[i];
console.log(` Page ${i + 1}: Capture à Y=${scrollYPage}px`);
const pageTiles = [];
// Pour chaque ligne de tiles
for (let r = 0; r < rows; r++) {
// Scroll vertical: position de base * scale + offset de ligne * windowHeight
const scrollY = scrollYPage * cssScale + r * windowHeight;
// Pour chaque colonne de tiles
for (let c = 0; c < cols; c++) {
// Scroll horizontal vers la colonne
const scrollX = c * windowWidth;
await page.evaluate(({ x, y }) => { window.scrollTo(x, y); }, { x: scrollX, y: scrollY });
await page.waitForTimeout(100);
const tilePath = path.join(outputDir, `page-${i + 1}-r${r}-c${c}.png`);
await page.screenshot({ path: tilePath, type: 'png' });
// Lire dimensions réelles
const meta = await sharp(tilePath).metadata();
pageTiles.push({
path: tilePath,
row: r,
col: c,
width: meta.width,
height: meta.height
});
console.log(` Tile [${r},${c}]: ${meta.width}×${meta.height}px`);
}
}
// Assembler les tiles en une seule image A4
const stitchedPath = path.join(outputDir, `page-${i + 1}.png`);
// Calculer dimensions finales (somme des tiles)
const tileW = pageTiles[0].width;
const tileH = pageTiles[0].height;
const finalWidth = tileW * cols;
const finalHeight = tileH * rows;
// Créer une image vide et composer les tiles
const composites = pageTiles.map(t => ({
input: t.path,
left: t.col * tileW,
top: t.row * tileH
}));
await sharp({
create: {
width: finalWidth,
height: finalHeight,
channels: 4,
background: { r: 0, g: 0, b: 0, alpha: 0 }
}
})
.composite(composites)
.png()
.toFile(stitchedPath);
console.log(` ✓ Assemblage: ${finalWidth}×${finalHeight}px → ${stitchedPath}`);
// Cleanup tiles
for (const tile of pageTiles) {
await fs.unlink(tile.path).catch(() => {});
}
screenshots.push({ path: stitchedPath, width: finalWidth, height: finalHeight });
} // Fermeture de la boucle for (scrollPositions)
// Supprimer la dernière page (marge de sécurité du padding-bottom: 300px)
if (screenshots.length > 2) {
const lastPage = screenshots.pop();
await fs.unlink(lastPage.path).catch(() => {});
console.log(`✓ Dernière page supprimée (marge de sécurité)`);
}
console.log(`✓ ${screenshots.length} zone(s) capturée(s)`);
await browser.close();
return screenshots;
} catch (error) {
console.error('\n❌ Erreur lors de la capture:', error.message);
if (browser) await browser.close();
throw error;
}
}
// ============================================================================
// IMAGE PROCESSING - Découpage en tranches A4
// ============================================================================
/**
* Convertit les screenshots en format A4
* Redimensionne à la largeur A4 en préservant le ratio
*/
async function convertToA4Format(screenshots) {
console.log('\n🔄 Conversion en format A4...');
const a4Pages = [];
for (let i = 0; i < screenshots.length; i++) {
const screenshot = screenshots[i];
console.log(` Page ${i + 1}: ${screenshot.width}×${screenshot.height}px`);
const image = sharp(screenshot.path);
// Vérifier le ratio
const actualRatio = screenshot.height / screenshot.width;
const a4Ratio = CONFIG.a4.heightPx / CONFIG.a4.widthPx;
const ratioDiff = Math.abs(actualRatio - a4Ratio) / a4Ratio;
let processedBuffer;
let finalWidth, finalHeight;
// Si l'image est déjà au bon ratio ET en haute résolution, la garder telle quelle
if (ratioDiff < 0.01 && screenshot.width >= CONFIG.a4.widthPx) {
console.log(` → Haute résolution conservée (ratio: ${actualRatio.toFixed(3)}, diff: ${(ratioDiff * 100).toFixed(2)}%)`);
processedBuffer = await image.png().toBuffer();
finalWidth = screenshot.width;
finalHeight = screenshot.height;
} else {
// Redimensionner à la largeur A4 seulement si nécessaire
const scaleRatio = CONFIG.a4.widthPx / screenshot.width;
const scaledHeight = Math.round(screenshot.height * scaleRatio);
console.log(` → Redimensionnement: ${CONFIG.a4.widthPx}×${scaledHeight}px (ratio: ${scaleRatio.toFixed(3)})`);
processedBuffer = await image
.resize(CONFIG.a4.widthPx, scaledHeight, {
fit: 'fill',
kernel: 'lanczos3'
})
.png()
.toBuffer();
finalWidth = CONFIG.a4.widthPx;
finalHeight = scaledHeight;
}
a4Pages.push({
buffer: processedBuffer,
width: finalWidth,
height: finalHeight
});
}
console.log(`✓ ${a4Pages.length} page(s) convertie(s)`);
return a4Pages;
}
/**
* Trouve les positions de coupure optimales basées sur les safe cut points
* pour maximiser l'utilisation des pages A4
* IMPORTANT: Garantit un espacement MINIMUM de pageHeight entre chaque position
*/
function findOptimalCutPositions(safeCutPoints, totalHeight, pageHeight) {
const positions = [0];
let currentPos = 0;
while (currentPos < totalHeight) {
const minNextPos = currentPos + pageHeight; // Position minimale (pas de chevauchement)
if (minNextPos >= totalHeight) {
break; // Dernière page
}
// Trouver le safe cut point le plus proche APRÈS minNextPos
let bestCutPoint = minNextPos;
let minDistance = Infinity;
for (const cutPoint of safeCutPoints) {
// SÉCURITÉ: le cutPoint doit être AU MOINS à minNextPos (pas de chevauchement)
if (cutPoint >= minNextPos && cutPoint <= minNextPos + 200) { // Tolérance de 200px
const distance = cutPoint - minNextPos;
if (distance < minDistance) {
minDistance = distance;
bestCutPoint = cutPoint;
}
}
}
positions.push(bestCutPoint);
currentPos = bestCutPoint;
}
return positions;
}
// ============================================================================
// PDF GENERATION - Composition du PDF final
// ============================================================================
/**
* Crée un PDF multi-pages à partir des images A4
*/
async function createPdfFromA4Images(imageBuffers, outputPath) {
console.log('\n📝 Création du PDF final...');
const pdfDoc = await PDFDocument.create();
// Dimensions A4 en points (1 point = 1/72 inch)
// A4 = 210mm × 297mm = 595.28pt × 841.89pt
const a4Width = 595.28;
const a4Height = 841.89;
for (let i = 0; i < imageBuffers.length; i++) {
console.log(` Ajout de la page ${i + 1}/${imageBuffers.length}...`);
const pageData = imageBuffers[i];
const buffer = pageData.buffer || pageData; // Support ancien format (buffer direct)
// Créer une nouvelle page A4
const page = pdfDoc.addPage([a4Width, a4Height]);
// Embed l'image PNG dans le PDF (haute résolution conservée)
const pngImage = await pdfDoc.embedPng(buffer);
// Obtenir les dimensions natives de l'image
const { width: imgWidth, height: imgHeight } = pngImage.scale(1);
// Calculer les ratios
const imgRatio = imgWidth / imgHeight;
const a4Ratio = a4Width / a4Height;
const ratioDiff = Math.abs(imgRatio - a4Ratio) / a4Ratio;
if (pageData.width) {
console.log(` Image: ${pageData.width}×${pageData.height}px → PDF: ${a4Width.toFixed(0)}×${a4Height.toFixed(0)}pt`);
}
// Toujours étirer sur toute la page (l'image haute-res sera affichée dans le cadre A4)
if (ratioDiff < 0.01) {
console.log(` ✓ Ratio OK (diff: ${(ratioDiff * 100).toFixed(2)}%)`);
page.drawImage(pngImage, {
x: 0,
y: 0,
width: a4Width,
height: a4Height
});
} else {
console.warn(` ⚠ Ratio différent (diff: ${(ratioDiff * 100).toFixed(2)}%) - préservation du ratio`);
let drawWidth = a4Width;
let drawHeight = drawWidth / imgRatio;
if (drawHeight > a4Height) {
drawHeight = a4Height;
drawWidth = drawHeight * imgRatio;
}
const x = (a4Width - drawWidth) / 2;
const y = (a4Height - drawHeight) / 2;
page.drawImage(pngImage, {
x,
y,
width: drawWidth,
height: drawHeight
});
}
}
console.log(' Écriture du fichier PDF...');
const pdfBytes = await pdfDoc.save();
await fs.writeFile(outputPath, pdfBytes);
console.log(`✓ PDF créé: ${imageBuffers.length} page(s)`);
}
// ============================================================================
// MAIN - Point d'entrée du script
// ============================================================================
/**
* Parse les arguments CLI
*/
function parseCliArgs() {
const args = process.argv.slice(2);
const options = {
locale: 'fr',
theme: 'dark',
output: null,
url: null,
pages: CONFIG.pagination.targetPages, // Nombre de pages cibles
scale: 1,
tiles: undefined // Auto-détecté basé sur scale
,
// Rehydration options (can be provided at top-level or with rehydrate- prefix)
preset: null, // optional preset for rehydration (tight|normal|loose)
debug: false, // show debug overlay during rehydration
rehydratePreset: null, // explicit rehydrate preset (overrides --preset)
rehydrateDebug: false // explicit rehydrate debug flag (overrides --debug)
};
for (let i = 0; i < args.length; i++) {
if (args[i] === '--locale' && args[i + 1]) {
options.locale = args[i + 1];
i++;
} else if (args[i] === '--theme' && args[i + 1]) {
options.theme = args[i + 1];
i++;
} else if (args[i] === '--output' && args[i + 1]) {
options.output = args[i + 1];
i++;
} else if (args[i] === '--url' && args[i + 1]) {
options.url = args[i + 1];
i++;
} else if (args[i] === '--pages' && args[i + 1]) {
options.pages = parseInt(args[i + 1]);
i++;
} else if (args[i] === '--scale' && args[i + 1]) {
options.scale = parseFloat(args[i + 1]);
i++;
} else if (args[i] === '--tiles' && args[i + 1]) {
options.tiles = args[i + 1];
i++;
} else if (args[i] === '--preset' && args[i + 1]) {
// Preset for rehydration (or general preset)
options.preset = args[i + 1];
i++;
} else if (args[i] === '--rehydrate-preset' && args[i + 1]) {
options.rehydratePreset = args[i + 1];
i++;
} else if (args[i] === '--debug') {
// Enable debug overlay for rehydration
options.debug = true;
} else if (args[i] === '--rehydrate-debug') {
options.rehydrateDebug = true;
}
}
return options;
}
/**
* Fonction principale
*/
async function main() {
console.log('============================================================');
console.log(' CV PDF GENERATOR - Screenshot to PDF');
console.log('============================================================\n');
try {
const options = parseCliArgs();
const selectedLocale = CONFIG.supportedLocales.includes(options.locale)
? options.locale
: CONFIG.supportedLocales[0];
const selectedTheme = CONFIG.supportedThemes.includes(options.theme)
? options.theme
: CONFIG.supportedThemes[0];
console.log(`📋 Configuration:`);
console.log(` Locale: ${selectedLocale}`);
console.log(` Theme: ${selectedTheme}`);
if (options.pages) {
console.log(` Pages cibles: ${options.pages}`);
}
// Création des répertoires
if (!fsSync.existsSync(CONFIG.outputDir)) {
fsSync.mkdirSync(CONFIG.outputDir, { recursive: true });
}
if (!fsSync.existsSync(CONFIG.tempDir)) {
fsSync.mkdirSync(CONFIG.tempDir, { recursive: true });
}
// Chemin de sortie
const outputFileName = `cv-${selectedLocale}-${selectedTheme}.pdf`;
const outputPath = options.output || path.join(CONFIG.outputDir, outputFileName);
console.log(` Output: ${outputPath}\n`);
// Chargement du template et de la locale
console.log('📚 Chargement des ressources...');
const templateHtml = fsSync.readFileSync(CONFIG.templatePath, 'utf8');
const localeData = loadLocale(selectedLocale);
if (!localeData) {
throw new Error(`Impossible de charger la locale: ${selectedLocale}`);
}
console.log('✓ Template et locale chargés');
// Génération du HTML localisé
console.log('\n🔧 Génération du HTML localisé...');
let localizedHtml = generateLocalizedHtml(
templateHtml,
localeData,
selectedLocale,
selectedTheme
);
// Injection du CSS inline
const cssContent = fsSync.readFileSync(CONFIG.cssPath, 'utf8');
// Ajouter le CSS PDF si activé
let finalCss = cssContent;
if (CONFIG.pagination.usePdfCss && fsSync.existsSync(CONFIG.cssPdfPath)) {
const cssPdfContent = fsSync.readFileSync(CONFIG.cssPdfPath, 'utf8');
finalCss += '\n\n' + cssPdfContent;
console.log('✓ CSS PDF ajouté');
}
localizedHtml = localizedHtml.replace(
/<link rel="stylesheet" href=".*?">/,
`<style>${finalCss}</style>`
);
console.log('✓ HTML prêt');
// Capture avec fenêtre virtuelle A4 scrollante
// Auto-déterminer tiles basé sur scale
const scale = options.scale || 1;
const tiles = options.tiles || (scale === 1 ? '1x1' : `${scale}x${scale}`);
const screenshots = await captureWithVirtualA4Window(
localizedHtml,
CONFIG.tempDir,
selectedTheme,
CONFIG.pagination.usePdfCss,
scale,
tiles
);
// Conversion en format A4
const a4Pages = await convertToA4Format(screenshots);
// Création du PDF final (Temporaire Raster)
const rasterOutputPath = outputPath.replace('.pdf', '-raster.pdf');
console.log(`💾 Sauvegarde du PDF raster temporaire : ${rasterOutputPath}`);
await createPdfFromA4Images(a4Pages, rasterOutputPath);
// PIPELINE DE RÉHYDRATATION
console.log('\n🔮 Réhydratation du PDF (Ajout du texte sélectionnable)...');
try {
// Determine preset/debug for rehydration: explicit rehydrate flags override general ones
const rehydratePreset = options.rehydratePreset || options.preset || 'normal';
const rehydrateDebug = options.rehydrateDebug || options.debug || false;
const rehydratedPath = await rehydratePdf({
input: rasterOutputPath,
locale: selectedLocale,
theme: selectedTheme,
preset: rehydratePreset,
debug: rehydrateDebug
});
// Rename the rehydrated PDF to the expected output path
await fs.rename(rehydratedPath, outputPath);
// Remove the temporary raster PDF
await fs.unlink(rasterOutputPath);
console.log(`✨ Réhydratation réussie (preset=${rehydratePreset}, debug=${rehydrateDebug}) !`);
} catch (e) {
console.error("⚠️ Erreur lors de la réhydratation, utilisation du PDF raster brut comme fallback.", e);
// Fallback: Si la réhydratation échoue, on utilise le raster
if (fsSync.existsSync(rasterOutputPath)) {
await fs.rename(rasterOutputPath, outputPath);
}
}
// Nettoyage
console.log('\n🧹 Nettoyage des fichiers temporaires...');
for (const screenshot of screenshots) {
await fs.unlink(screenshot.path);
}
console.log('✓ Nettoyage terminé');
console.log('\n============================================================');
console.log(`✅ PDF généré avec succès: ${outputPath}`);
console.log(` ${a4Pages.length} page(s) A4`);
console.log('============================================================\n');
} catch (error) {
console.error('\n❌ ERREUR FATALE:', error.message);
console.error(error.stack);
process.exit(1);
}
}
// Exécution
if (require.main === module) {
main();
}
module.exports = { captureWithVirtualA4Window, convertToA4Format, createPdfFromA4Images };