-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrehydrate-pdf.js
More file actions
1248 lines (1060 loc) · 45.2 KB
/
rehydrate-pdf.js
File metadata and controls
1248 lines (1060 loc) · 45.2 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
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* ============================================================================
* PDF REHYDRATION - Add Invisible Selectable Text to Raster PDF
* ============================================================================
*
* This script takes a raster PDF (screenshot-based) and "rehydrates" it
* by adding invisible selectable text on top, making the PDF searchable
* and copyable while maintaining the pixel-perfect visual appearance.
*
* APPROACH:
* 1. Use Playwright to extract text content and precise coordinates from HTML
* 2. Use pdf-lib to overlay invisible text on the raster PDF
* 3. Handle coordinate conversion (HTML top-left vs PDF bottom-left)
* 4. Calculate scaling factor between HTML viewport and PDF dimensions
*
* USAGE:
* node rehydrate-pdf.js
* node rehydrate-pdf.js --locale fr --theme dark
* node rehydrate-pdf.js --input exports/cv-fr-d.pdf
*
* @requires @playwright/test pdf-lib
*/
const { chromium } = require('@playwright/test');
const { PDFDocument, rgb, StandardFonts } = require('pdf-lib');
const fontkit = require('@pdf-lib/fontkit');
const fs = require('fs').promises;
const fsSync = require('fs');
const path = require('path');
const yaml = require('yaml');
// ============================================================================
// CONFIGURATION
// ============================================================================
const CONFIG = {
// Source HTML (same as generateCvPdf.js)
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'),
// Viewport (must match generateCvPdf.js)
viewport: {
width: 900,
height: 1273
},
// A4 dimensions in pixels (96 DPI)
a4: {
widthPx: 794,
heightPx: 1123
},
// Timeouts
timeout: {
navigation: 30000,
fonts: 10000,
render: 5000
},
// Output
outputDir: './exports',
supportedLocales: ['fr', 'en'],
supportedThemes: ['dark', 'light'],
// Fine-tuning options
positioning: {
// Mode: 'auto' uses bounding box dimensions directly, 'manual' uses calculated baseline
mode: 'auto', // 'auto' or 'manual'
// Font size adjustment strategy
adjustFontSizeToWidth: true, // Dynamically adjust fontSize to match exact text width
// Baseline positioning strategy (only used in manual mode):
// 'top': Use rect.top + fontSize (current behavior)
// 'baseline': Use estimated baseline position
// 'bottom': Use rect.bottom
strategy: 'baseline',
// Manual offset adjustments (in pixels before scaling)
offsetY: 0, // Global offset: Positive = move text down, Negative = move text up
offsetX: 0, // Global offset: Positive = move text right, Negative = move text left
// Specific offsets by element type (added to global offset)
offsetsByType: {
h1: 0,
h2: 0,
h3: 0,
h4: 0,
h5: 0,
h6: 0,
body: 0
},
// Font size adjustment factors (multiplicative)
fontSizeAdjust: 1.0, // Global adjustment
// Specific adjustments by element type
fontSizeAdjustments: {
h1: 1.20, // Large main title
h2: 1.15, // Section titles
h3: 1.10, // Sub-section titles
h4: 1.05, // Small titles
h5: 1.02, // Very small titles
h6: 1.00, // Smallest titles
body: 0.95 // Regular body text
},
// Baseline offset as percentage of fontSize (0.0 to 1.0)
// For Inter font: 0.203 (20.3% descent)
baselineOffset: 0.203
},
// Font configuration
fonts: {
// Try to use embedded custom fonts for better accuracy
useCustomFonts: true, // Set to true if you have Inter.ttf in fonts/ folder
// Local font paths (if available)
fontPaths: {
'Inter-Regular': './fonts/Inter-Regular.ttf',
'Inter-Bold': './fonts/Inter-Bold.ttf'
},
// Standard PDF fonts to use (closest to Inter)
// Helvetica is the closest sans-serif font to Inter in PDF standard fonts
standardFonts: {
regular: StandardFonts.Helvetica,
bold: StandardFonts.HelveticaBold
}
}
};
// ============================================================================
// UTILITIES - Locale Loading
// ============================================================================
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(`❌ Error loading locale ${localeName}:`, error.message);
return null;
}
}
function generateHtml(locale, theme) {
try {
const { JSDOM } = require('jsdom');
const templateContent = fsSync.readFileSync(CONFIG.templatePath, 'utf8');
const cssContent = fsSync.readFileSync(CONFIG.cssPath, 'utf8');
const cssPdfContent = fsSync.readFileSync(CONFIG.cssPdfPath, 'utf8');
const localeData = loadLocale(locale);
if (!localeData) {
throw new Error(`Failed to load locale data for ${locale}`);
}
// Parse HTML with JSDOM
const dom = new JSDOM(templateContent);
const { document } = dom.window;
// Set document attributes
document.documentElement.lang = locale;
document.documentElement.setAttribute('data-theme', theme);
// Set title
if (localeData.title) {
document.title = localeData.title;
}
// Update 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);
}
});
// Update ARIA labels
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);
}
});
// Replace all data-i18n elements with localized content
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;
}
});
// Serialize back to HTML string
let html = dom.serialize();
// Note: CSS is already linked in the template, but for inline generation:
// We could inject styles, but the template already has <link rel="stylesheet">
// For PDF generation context, we might want to inline styles:
html = html.replace('</head>', `
<style>${cssContent}</style>
<style>${cssPdfContent}</style>
</head>`);
return html;
} catch (error) {
console.error('❌ Error generating HTML:', error.message);
throw error;
}
}
// ============================================================================
// FONT HANDLING - Download and embed custom fonts
// ============================================================================
/**
* Download a font from URL (with caching)
*/
async function loadFontFromFile(fontPath) {
try {
const fontBytes = await fs.readFile(fontPath);
return fontBytes;
} catch (error) {
console.warn(` ⚠️ Could not load font from ${fontPath}: ${error.message}`);
return null;
}
}
/**
* Load and embed fonts into PDF document
* Returns an object with font references: { regular, bold }
*/
async function loadFonts(pdfDoc) {
const fonts = { regular: null, bold: null };
if (!CONFIG.fonts.useCustomFonts) {
// Use standard fonts
fonts.regular = await pdfDoc.embedFont(CONFIG.fonts.standardFonts.regular);
fonts.bold = await pdfDoc.embedFont(CONFIG.fonts.standardFonts.bold);
console.log('✅ Using standard PDF fonts (Helvetica - closest to Inter)');
return fonts;
}
try {
console.log('🔍 Loading custom fonts from files...');
// Try to load Inter Regular
if (CONFIG.fonts.fontPaths['Inter-Regular']) {
const regularBuffer = await loadFontFromFile(CONFIG.fonts.fontPaths['Inter-Regular']);
if (regularBuffer) {
fonts.regular = await pdfDoc.embedFont(regularBuffer);
console.log(' ✅ Inter Regular loaded from file');
}
}
// Try to load Inter Bold
if (CONFIG.fonts.fontPaths['Inter-Bold']) {
const boldBuffer = await loadFontFromFile(CONFIG.fonts.fontPaths['Inter-Bold']);
if (boldBuffer) {
fonts.bold = await pdfDoc.embedFont(boldBuffer);
console.log(' ✅ Inter Bold loaded from file');
}
}
// Fallback if any font is missing
if (!fonts.regular) {
fonts.regular = await pdfDoc.embedFont(CONFIG.fonts.standardFonts.regular);
console.log(' ⚠️ Using Helvetica as fallback for regular font');
}
if (!fonts.bold) {
fonts.bold = await pdfDoc.embedFont(CONFIG.fonts.standardFonts.bold);
console.log(' ⚠️ Using Helvetica-Bold as fallback for bold font');
}
} catch (error) {
console.warn(`⚠️ Error loading custom fonts: ${error.message}`);
console.log(' Using fallback fonts...');
fonts.regular = await pdfDoc.embedFont(CONFIG.fonts.standardFonts.regular);
fonts.bold = await pdfDoc.embedFont(CONFIG.fonts.standardFonts.bold);
}
return fonts;
}
// ============================================================================
// TEXT EXTRACTION - Extract text with precise coordinates from HTML
// ============================================================================
/**
* Extract all text nodes with their precise bounding boxes from the HTML page
* Uses getClientRects() to handle multiline text correctly
*/
async function extractTextCoordinates(page) {
console.log('📝 Extracting text coordinates from HTML...');
const textData = await page.evaluate(() => {
const results = [];
// Helper function to traverse DOM and find text nodes
function walkTextNodes(node, depth = 0) {
if (node.nodeType === Node.TEXT_NODE) {
const text = node.textContent.trim();
if (text.length > 0) {
// Create range for this text node
const range = document.createRange();
range.selectNodeContents(node);
// Get all client rects (handles multiline text)
const rects = range.getClientRects();
// Get computed style for font size and metrics
const parent = node.parentElement;
const style = parent ? window.getComputedStyle(parent) : null;
const fontSize = style ? parseFloat(style.fontSize) : 16;
const fontFamily = style ? style.fontFamily : 'Arial';
const fontWeight = style ? style.fontWeight : 'normal';
const lineHeight = style ? style.lineHeight : 'normal';
const textTransform = style ? style.textTransform : 'none';
const letterSpacing = style ? parseFloat(style.letterSpacing) || 0 : 0;
// Detect element type (for font size adjustments)
let elementType = 'body';
let tagName = parent ? parent.tagName.toLowerCase() : '';
// Walk up the DOM to find if we're in a heading
let current = parent;
while (current && elementType === 'body') {
const tag = current.tagName ? current.tagName.toLowerCase() : '';
if (tag === 'h1' || tag === 'h2' || tag === 'h3' || tag === 'h4' || tag === 'h5' || tag === 'h6') {
elementType = 'heading';
tagName = tag;
break;
}
current = current.parentElement;
}
// Calculate baseline offset
// For Inter font: descent is ~20% of fontSize, ascent is ~75%
const estimatedDescent = fontSize * 0.203; // Inter specific
const estimatedAscent = fontSize * 0.75; // Inter specific
// If we have multiple rects, the text wraps across lines
if (rects.length > 1) {
// Extract text character by character to find exact line breaks
let charIndex = 0;
for (let i = 0; i < rects.length; i++) {
const rect = rects[i];
if (rect.width === 0 || rect.height === 0) continue;
// Find where this line starts and ends by checking Y position of each character
let lineStart = charIndex;
let lineEnd = charIndex;
const testRange = document.createRange();
testRange.selectNodeContents(node);
// Iterate character by character to find where line breaks
for (let j = charIndex; j < text.length; j++) {
testRange.setStart(node, j);
testRange.setEnd(node, j + 1);
const charRect = testRange.getBoundingClientRect();
// Check if this character is on the same line (Y position within tolerance)
const onSameLine = Math.abs(charRect.top - rect.top) < rect.height * 0.3;
if (onSameLine) {
lineEnd = j + 1;
} else if (j > charIndex) {
// We've moved to a different line, stop here
break;
}
}
const lineText = text.substring(lineStart, lineEnd).trim();
charIndex = lineEnd;
if (lineText.length > 0) {
results.push({
text: lineText,
x: rect.left,
y: rect.top,
bottom: rect.bottom,
width: rect.width,
height: rect.height,
fontSize: fontSize,
fontFamily: fontFamily,
fontWeight: fontWeight,
lineHeight: lineHeight,
textTransform: textTransform,
letterSpacing: letterSpacing,
estimatedBaseline: rect.top + estimatedAscent,
estimatedDescent: estimatedDescent,
elementType: elementType,
tagName: tagName
});
}
}
} else if (rects.length === 1) {
// Single line text
const rect = rects[0];
if (rect.width > 0 && rect.height > 0) {
results.push({
text: text,
x: rect.left,
y: rect.top,
bottom: rect.bottom,
width: rect.width,
height: rect.height,
fontSize: fontSize,
fontFamily: fontFamily,
fontWeight: fontWeight,
lineHeight: lineHeight,
textTransform: textTransform,
letterSpacing: letterSpacing,
estimatedBaseline: rect.top + estimatedAscent,
estimatedDescent: estimatedDescent,
elementType: elementType,
tagName: tagName
});
}
}
}
} else if (node.nodeType === Node.ELEMENT_NODE) {
const element = node;
// Skip script, style, noscript tags
const tagName = element.tagName.toLowerCase();
if (tagName === 'script' || tagName === 'style' || tagName === 'noscript') {
return;
}
// Skip hidden elements
const style = window.getComputedStyle(element);
if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') {
return;
}
// Recurse into child nodes
for (let child of element.childNodes) {
walkTextNodes(child, depth + 1);
}
}
}
// Start from body
const body = document.body;
if (body) {
walkTextNodes(body);
}
// Also get viewport dimensions
return {
textItems: results,
viewport: {
width: document.documentElement.scrollWidth,
height: document.documentElement.scrollHeight
}
};
});
console.log(`✅ Extracted ${textData.textItems.length} text segments`);
console.log(`📐 HTML viewport: ${textData.viewport.width}x${textData.viewport.height}px`);
return textData;
}
// ============================================================================
// PDF OVERLAY - Add invisible text to raster PDF
// ============================================================================
/**
* Load the raster PDF and overlay invisible text on it
*/
async function overlayTextOnPdf(pdfPath, textData, outputPath, debugMode = false) {
console.log(`📄 Loading raster PDF: ${pdfPath}`);
// Load the existing raster PDF
const existingPdfBytes = await fs.readFile(pdfPath);
const pdfDoc = await PDFDocument.load(existingPdfBytes);
// Register fontkit to support custom fonts
pdfDoc.registerFontkit(fontkit);
// Load fonts (Inter or fallback to Helvetica)
const fonts = await loadFonts(pdfDoc);
// Get pages
const pages = pdfDoc.getPages();
console.log(`📑 PDF has ${pages.length} pages`);
// Calculate scaling factors
const firstPage = pages[0];
const pdfWidth = firstPage.getWidth();
const pdfHeight = firstPage.getHeight();
const htmlWidth = textData.viewport.width;
const htmlHeight = textData.viewport.height;
// Calculate total HTML height for all pages
const totalHtmlHeight = htmlHeight;
const numPages = pages.length;
// Use same window height calculation as generateCvPdf.js
// windowHeight = windowWidth * 1.414 (A4 ratio)
const windowHeight = Math.round(CONFIG.viewport.width * 1.414);
console.log(`📐 Window height (A4 ratio): ${windowHeight}px`);
// Scale factors: PDF points to HTML pixels
const scaleX = pdfWidth / CONFIG.viewport.width;
const scaleY = pdfHeight / windowHeight;
console.log(`📐 PDF dimensions: ${pdfWidth}x${pdfHeight}pt (${numPages} pages)`);
console.log(`📐 HTML dimensions: ${htmlWidth}x${htmlHeight}px`);
console.log(`📐 Scale factors: X=${scaleX.toFixed(3)}, Y=${scaleY.toFixed(3)}`);
// Sorting Strategy: TRUST THE HTML DOM ORDER
// The user confirms the HTML structure is the "source of truth".
// Since extractTextCoordinates() iterates via TreeWalker (DOM order),
// the textItems array is already in the correct logical reading order.
// Any spatial sorting (Column/Grid) risks scrambling this natural order.
if (debugMode) {
console.log('🔄 Ordering: Using natural DOM order (HTML source of truth)');
}
const sortedItems = textData.textItems;
if (debugMode) {
console.log('\n=== READING ORDER (First 30 items) ===');
sortedItems.slice(0, 30).forEach((i, idx) => {
console.log(`${(idx+1).toString().padStart(2)}. Y=${i.y.toFixed(0).padStart(4)} X=${i.x.toFixed(0).padStart(3)} "${i.text.substring(0, 35)}"`);
});
}
// Detect large vertical gaps to inject invisible structure breakers
// This helps PDF viewers understand that "Row 1" and "Row 2" are separate blocks
const sortedByY = [...textData.textItems].sort((a, b) => a.y - b.y);
const gaps = [];
if (sortedByY.length > 0) {
let maxYInBlock = sortedByY[0].bottom;
// Debug gap detection
if (debugMode) console.log(`🔍 Analyzing vertical gaps (Start Y=${sortedByY[0].y.toFixed(0)})...`);
for (const item of sortedByY) {
// Ignore items that are too small or likely noise (less than 5px height)
if (item.height < 5) continue;
const gapSize = item.y - maxYInBlock;
// Use a smaller threshold (e.g. 20px) to catch tighter sections
if (gapSize > 25) {
if (debugMode) {
console.log(` Found gap of ${gapSize.toFixed(0)}px between Y=${maxYInBlock.toFixed(0)} and Y=${item.y.toFixed(0)}`);
}
gaps.push((maxYInBlock + item.y) / 2);
}
// Only extend block bottom if it pushes further down
if (item.bottom > maxYInBlock) {
maxYInBlock = item.bottom;
}
}
}
if (debugMode) {
console.log(`🧱 Detected ${gaps.length} vertical gaps for structural separation: ${gaps.map(g => g.toFixed(0)).join(', ')}`);
}
// Store gaps for drawing phase
textData.gaps = gaps;
// Map icon text to readable labels for ATS compatibility
const iconLabels = {
'::': '', // Header icon - remove
'##': '', // Section icon - remove (H2 title already present)
'[]': '', // Section icon - remove
'>>': '•', // Bullet point
'||': '', // Section icon - remove
'<>': '', // Section icon - remove
'//': '', // Section icon - remove
'++': '', // Section icon - remove
'--': '-'
};
// Apply icon mapping to all items BEFORE deduplication
const mappedItems = sortedItems.map(item => {
const trimmed = item.text.trim();
if (debugMode && (trimmed === '##' || trimmed.includes('CONTACT'))) {
console.log(`📝 Mapping: "${trimmed}" → "${iconLabels[trimmed] || trimmed}"`);
}
if (iconLabels.hasOwnProperty(trimmed)) {
return { ...item, text: iconLabels[trimmed] };
}
return item;
});
// Remove duplicates: if two items have same Y position and same text, keep the one with larger width
const deduplicatedItems = [];
for (let i = 0; i < mappedItems.length; i++) {
const item = mappedItems[i];
// Skip empty text
if (item.text.length === 0) {
continue;
}
// Check if this item is a duplicate of any existing item (within 30px in Y)
const duplicateIndex = deduplicatedItems.findIndex(existing => {
return Math.abs(item.y - existing.y) < 30 &&
item.text.trim() === existing.text.trim();
});
if (duplicateIndex >= 0) {
// Found duplicate - keep the one with larger width (likely the real heading, not icon)
if (debugMode && (item.text.includes('CONTACT') || item.text.includes('COMPÉTENCES'))) {
console.log(`🔍 Duplicate #${i}: "${item.text}" at Y=${item.y.toFixed(1)} (w=${item.width.toFixed(1)}) vs existing at Y=${deduplicatedItems[duplicateIndex].y.toFixed(1)} (w=${deduplicatedItems[duplicateIndex].width.toFixed(1)})`);
}
// Always keep the larger one
if (item.width > deduplicatedItems[duplicateIndex].width) {
if (debugMode && (item.text.includes('CONTACT') || item.text.includes('COMPÉTENCES'))) {
console.log(` → Replacing with larger item`);
}
deduplicatedItems[duplicateIndex] = item; // Replace with larger one
} else {
if (debugMode && (item.text.includes('CONTACT') || item.text.includes('COMPÉTENCES'))) {
console.log(` → Keeping existing (larger)`);
}
}
// Don't add - we already have this text
continue;
}
// Not a duplicate, add it
if (debugMode && (item.text.includes('CONTACT') || item.text.includes('COMPÉTENCES'))) {
console.log(`✅ Adding #${i}: "${item.text}" at Y=${item.y.toFixed(1)} (width=${item.width.toFixed(1)})`);
}
deduplicatedItems.push(item);
}
if (debugMode) {
console.log(`🔍 Deduplication: ${mappedItems.length} items → ${deduplicatedItems.length} items (${mappedItems.length - deduplicatedItems.length} duplicates removed)`);
}
// ==========================================================================
// Draw invisible structural separators (lines in vertical gaps)
// This helps PDF viewers distinguish blocks (Row 1 vs Row 2)
// ==========================================================================
if (textData.gaps && textData.gaps.length > 0) {
console.log(`🧱 Drawing ${textData.gaps.length} invisible structural separators...`);
for (const gapY of textData.gaps) {
const pageIndex = Math.floor(gapY / windowHeight);
if (pageIndex < pages.length) {
const page = pages[pageIndex];
const pageHeight = page.getHeight();
// Convert global HTML Y to local PDF Y
// Note: Logic mirrored from item processing below
const relativeY = gapY - (pageIndex * windowHeight);
const pdfY = pageHeight - (relativeY * scaleY);
if (debugMode) console.log(` Drawing separator at HTML Y=${gapY.toFixed(0)} -> PDF Y=${pdfY.toFixed(0)} on Page ${pageIndex + 1}`);
// 1. Draw ALMOST invisible line (opacity > 0 ensures it's rendered in DOM structure)
page.drawLine({
start: { x: 0, y: pdfY },
end: { x: page.getWidth(), y: pdfY },
thickness: 2,
opacity: 0.01,
color: rgb(0.9, 0.9, 0.9) // Light gray, nearly invisible
});
// 2. Add invisible text barrier (dots across the page)
// Text is the strongest signal for content ordering
try {
const barrierText = '.'.repeat(200); // Dense line of dots
page.drawText(barrierText, {
x: 0,
y: pdfY,
size: 4,
font: fonts.regular,
color: rgb(1, 1, 1),
opacity: 0 // Text can be fully invisible and still break reading flow
});
} catch (e) {
console.warn('Failed to draw text barrier:', e);
}
}
}
}
// Process each text item
let itemsProcessed = 0;
let itemsSkipped = 0;
let itemsAdjusted = 0; // Track how many had width adjustments
for (const item of deduplicatedItems) {
// Determine which page this text belongs to based on windowHeight
const pageIndex = Math.floor(item.y / windowHeight);
if (pageIndex >= pages.length) {
// Text is beyond available pages, skip
itemsSkipped++;
continue;
}
const page = pages[pageIndex];
const pageHeight = page.getHeight();
// Calculate Y position relative to current page
const relativeY = item.y - (pageIndex * windowHeight);
// Convert coordinates from HTML (top-left origin) to PDF (bottom-left origin)
// PDF Y starts from bottom, HTML Y starts from top
// Calculate element-specific offset
let elementOffsetY = CONFIG.positioning.offsetY; // Start with global offset
if (item.elementType === 'heading' && item.tagName) {
const specificOffset = CONFIG.positioning.offsetsByType[item.tagName];
if (specificOffset !== undefined) {
elementOffsetY += specificOffset;
}
} else if (item.elementType === 'body') {
elementOffsetY += CONFIG.positioning.offsetsByType.body;
}
// Apply manual offsets BEFORE scaling
const adjustedX = item.x + CONFIG.positioning.offsetX;
// Apply font size adjustments based on element type
let fontSizeMultiplier = CONFIG.positioning.fontSizeAdjust;
// Get specific adjustment for this element type
if (item.elementType === 'heading' && item.tagName) {
const tagAdjustment = CONFIG.positioning.fontSizeAdjustments[item.tagName];
if (tagAdjustment) {
fontSizeMultiplier *= tagAdjustment;
}
} else if (item.elementType === 'body') {
fontSizeMultiplier *= CONFIG.positioning.fontSizeAdjustments.body;
}
const adjustedFontSize = item.fontSize * fontSizeMultiplier;
// Calculate Y position based on mode
let adjustedY;
if (CONFIG.positioning.mode === 'auto') {
// AUTO MODE: Use the bounding box dimensions directly
// The rect.height already represents the visual height of the text
// Position at the bottom of the bounding box (where the baseline roughly is)
// This works because PDF drawText positions at the baseline by default
// Use the bottom of the rect as the baseline position
// Subtract a small percentage of height for descenders
const descentRatio = 0.15; // ~15% for descenders in Inter
adjustedY = item.bottom - (item.height * descentRatio) + elementOffsetY;
} else {
// MANUAL MODE: Use strategy-based calculation
switch (CONFIG.positioning.strategy) {
case 'baseline':
// Use estimated baseline position
adjustedY = item.estimatedBaseline + elementOffsetY;
break;
case 'bottom':
// Use bottom of bounding box minus descent
adjustedY = (item.bottom - item.estimatedDescent) + elementOffsetY;
break;
case 'top':
default:
// Use top + fontSize (original behavior)
adjustedY = item.y + item.fontSize + elementOffsetY;
break;
}
}
const pdfX = adjustedX * scaleX;
const pdfY = pageHeight - ((adjustedY - (pageIndex * windowHeight)) * scaleY);
let pdfFontSize = adjustedFontSize * scaleY;
// Draw invisible text (opacity = 0)
try {
// Select appropriate font based on weight
const fontWeight = parseInt(item.fontWeight) || 400;
const isBold = fontWeight >= 600 || item.fontWeight === 'bold';
const selectedFont = isBold ? fonts.bold : fonts.regular;
// Apply text-transform (uppercase, lowercase, capitalize)
let transformedText = item.text;
if (item.textTransform) {
switch (item.textTransform) {
case 'uppercase':
transformedText = transformedText.toUpperCase();
break;
case 'lowercase':
transformedText = transformedText.toLowerCase();
break;
case 'capitalize':
transformedText = transformedText.replace(/\b\w/g, l => l.toUpperCase());
break;
}
}
// Clean text to avoid encoding issues with WinAnsi
// Replace problematic Unicode characters
let cleanText = transformedText
.replace(/→/g, '->') // Arrow
.replace(/‑/g, '-') // Non-breaking hyphen
.replace(/–/g, '-') // En dash
.replace(/—/g, '--') // Em dash
.replace(/'/g, "'") // Smart single quote
.replace(/'/g, "'") // Smart single quote
.replace(/"/g, '"') // Smart double quote
.replace(/"/g, '"') // Smart double quote
.replace(/…/g, '...') // Ellipsis
.replace(/[\u0080-\u00FF]/g, (c) => { // Try to preserve Latin-1 chars
return c;
})
.replace(/[^\x00-\xFF]/g, '?'); // Replace other non-Latin1 with ?
// Note: Icon mapping already done during deduplication, no need to re-apply here
// Dynamic fontSize adjustment to match exact width
if (CONFIG.positioning.adjustFontSizeToWidth && item.width > 0 && cleanText.length > 0) {
// Calculate the expected width in PDF coordinates
const expectedPdfWidth = item.width * scaleX;
// Use actual font metrics to measure text width
try {
const actualWidthBefore = selectedFont.widthOfTextAtSize(cleanText, pdfFontSize);
if (actualWidthBefore > 0) {
// Calculate adjustment ratio
const widthRatio = expectedPdfWidth / actualWidthBefore;
if (debugMode) {
console.log(`\n📏 "${cleanText.substring(0, 50)}${cleanText.length > 50 ? '...' : ''}"`);
console.log(` Chars: ${cleanText.length} | HTML width: ${item.width.toFixed(1)}px → PDF: ${expectedPdfWidth.toFixed(1)}pt`);
console.log(` Font: ${pdfFontSize.toFixed(2)}pt | Calculated width: ${actualWidthBefore.toFixed(1)}pt`);
console.log(` Ratio: ${widthRatio.toFixed(3)} ${widthRatio < 0.7 ? '❌ TOO SMALL' : widthRatio > 1.5 ? '❌ TOO LARGE' : widthRatio < 0.95 || widthRatio > 1.05 ? '⚠️ ADJUSTED' : '✅ OK'}`);
}
// Apply adjustment (with safety bounds to avoid extreme values)
if (widthRatio > 0.7 && widthRatio < 1.5) {
const oldSize = pdfFontSize;
pdfFontSize *= widthRatio;
itemsAdjusted++;
if (debugMode && Math.abs(widthRatio - 1.0) > 0.05) {
console.log(` → Adjusted: ${oldSize.toFixed(2)}pt → ${pdfFontSize.toFixed(2)}pt`);
}
}
}
} catch (error) {
// Silently fail if font measurement doesn't work
if (debugMode) {
console.log(` ⚠️ Width measurement failed: ${error.message}`);
}
}
}
// Note: Icon mapping already done during deduplication, no need to re-apply here
// Skip empty text (like :: icon)
if (cleanText.length === 0) {
continue;
}
page.drawText(cleanText, {
x: pdfX,
y: pdfY,
size: pdfFontSize,
font: selectedFont,
color: debugMode ? rgb(1, 0, 0) : rgb(0, 0, 0), // Red in debug mode
opacity: debugMode ? 0.5 : 0 // Semi-transparent in debug, invisible otherwise
});
itemsProcessed++;
} catch (error) {
// Skip items that cause errors (e.g., text too long, invalid coordinates)
console.warn(`⚠️ Skipped text item: "${item.text.substring(0, 20)}..." (${error.message})`);
}
}
console.log(`✅ Overlaid ${itemsProcessed}/${textData.textItems.length} text items (${itemsSkipped} skipped - out of bounds)`);
if (CONFIG.positioning.adjustFontSizeToWidth) {
console.log(`📊 Width adjustments applied: ${itemsAdjusted}/${itemsProcessed} items`);
}
// Save the rehydrated PDF
const pdfBytes = await pdfDoc.save();
await fs.writeFile(outputPath, pdfBytes);
console.log(`💾 Saved rehydrated PDF: ${outputPath}`);
return outputPath;
}
// ============================================================================
// MAIN WORKFLOW
// ============================================================================
async function rehydratePdf(options = {}) {
const locale = options.locale || 'fr';
const theme = options.theme || 'dark';
const inputPdf = options.input || null;
const debugMode = options.debug || false;
// Apply fine-tuning options to CONFIG
if (options.mode) {
CONFIG.positioning.mode = options.mode;
}
if (options.adjustWidth !== undefined) {
CONFIG.positioning.adjustFontSizeToWidth = options.adjustWidth;
}
if (options.offsetY !== undefined) {
CONFIG.positioning.offsetY = options.offsetY;
}
if (options.offsetX !== undefined) {
CONFIG.positioning.offsetX = options.offsetX;
}
if (options.strategy) {
CONFIG.positioning.strategy = options.strategy;
}
// Apply preset adjustments
if (options.preset) {
const presets = {
tight: {
h1: 1.25, h2: 1.20, h3: 1.15, h4: 1.10, h5: 1.05, h6: 1.02, body: 0.92
},
normal: {
h1: 1.20, h2: 1.15, h3: 1.10, h4: 1.05, h5: 1.02, h6: 1.00, body: 0.95
},
loose: {
h1: 1.15, h2: 1.12, h3: 1.08, h4: 1.04, h5: 1.01, h6: 0.98, body: 0.98
}
};
const selectedPreset = presets[options.preset];
if (selectedPreset) {
CONFIG.positioning.fontSizeAdjustments = { ...selectedPreset };
console.log(`🎯 Applied preset: ${options.preset}`);
} else {
console.warn(`⚠️ Unknown preset: ${options.preset}`);
}
}
// Apply specific offsets by type
if (options.offsetH1 !== undefined) {
CONFIG.positioning.offsetsByType.h1 = options.offsetH1;
}
if (options.offsetH2 !== undefined) {
CONFIG.positioning.offsetsByType.h2 = options.offsetH2;
}
if (options.offsetH3 !== undefined) {
CONFIG.positioning.offsetsByType.h3 = options.offsetH3;
}
if (options.offsetBody !== undefined) {
CONFIG.positioning.offsetsByType.body = options.offsetBody;
}
console.log('\n' + '='.repeat(80));
console.log('🔄 PDF REHYDRATION - Adding Invisible Selectable Text');
console.log('='.repeat(80));
console.log(`📍 Locale: ${locale} | Theme: ${theme}`);
console.log(`🤖 Mode: ${CONFIG.positioning.mode.toUpperCase()} ${CONFIG.positioning.mode === 'auto' ? '(smart bounding box)' : '(manual baseline)'}`);
console.log(`📏 Width adjustment: ${CONFIG.positioning.adjustFontSizeToWidth ? 'ENABLED (pixel-perfect wrapping)' : 'disabled'}`);
console.log(`🎯 Positioning: strategy=${CONFIG.positioning.strategy}, offsetY=${CONFIG.positioning.offsetY}, offsetX=${CONFIG.positioning.offsetX}`);
const adj = CONFIG.positioning.fontSizeAdjustments;
console.log(`🔤 Font sizes: h1×${adj.h1}, h2×${adj.h2}, h3×${adj.h3}, body×${adj.body}`);