-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-pdf.js
More file actions
267 lines (226 loc) · 7.67 KB
/
generate-pdf.js
File metadata and controls
267 lines (226 loc) · 7.67 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
/**
* ============================================================================
* CV PDF GENERATOR - SIMPLE & CLEAN
* ============================================================================
*
* Génération PDF simplifiée via Playwright :
* - Utilise directement les fichiers HTML générés (index-fr.html, index-en.html)
* - Applique le style PDF via style-pdf.css
* - Export PDF natif sans traitement complexe
*
* USAGE:
* node generate-pdf.js
* node generate-pdf.js --locale fr --theme dark
* node generate-pdf.js --all
*
* OPTIONS:
* --locale [fr|en] Langue du CV (défaut: fr)
* --theme [dark|light] Thème (défaut: dark)
* --all Génère toutes les combinaisons
*
* @requires @playwright/test
*/
const { chromium } = require('@playwright/test');
const { PDFDocument } = require('pdf-lib');
const fs = require('fs').promises;
const fsSync = require('fs');
const path = require('path');
// ============================================================================
// CONFIGURATION
// ============================================================================
const CONFIG = {
htmlDir: process.env.HTML_DIR || './dist/web',
outputDir: process.env.OUTPUT_DIR || './dist/pdf',
hideOnlineCvLink: process.env.HIDE_ONLINE_CV_LINK === 'true',
supportedLocales: ['fr', 'en'],
supportedThemes: ['dark', 'light'],
pdfOptions: {
format: 'A4',
printBackground: true,
preferCSSPageSize: false,
margin: {
top: '0mm',
right: '0mm',
bottom: '0mm',
left: '0mm'
}
}
};
// ============================================================================
// PDF GENERATION
// ============================================================================
/**
* Génère un PDF depuis un fichier HTML
*/
async function generatePdf(locale, theme, options = {}) {
const htmlFile = `index-${locale}.html`;
const htmlPath = path.resolve(CONFIG.htmlDir, htmlFile);
const outputPath = path.join(CONFIG.outputDir, `cv-${locale}-${theme}.pdf`);
// Vérifier que le fichier HTML existe
if (!fsSync.existsSync(htmlPath)) {
console.error(`❌ HTML file not found: ${htmlPath}`);
console.error(` HTML_DIR: ${CONFIG.htmlDir}`);
console.error(` Looking for: ${htmlFile}`);
throw new Error(`HTML file not found: ${htmlPath}`);
}
console.log(`📄 Generating PDF: ${locale} / ${theme}`);
console.log(` Input: ${htmlFile}`);
console.log(` Output: ${outputPath}`);
// Créer le dossier de sortie si nécessaire
await fs.mkdir(CONFIG.outputDir, { recursive: true });
// Lancer le navigateur
const browser = await chromium.launch({
headless: true
});
const page = await browser.newPage({
viewport: {
width: 1200,
height: 3000
},
deviceScaleFactor: options.raster ? 2 : 1
});
// Charger le HTML
const htmlContent = await fs.readFile(htmlPath, 'utf-8');
// Lire les CSS nécessaires
const cssBasePath = path.join(__dirname, 'style.css');
const cssPdfPath = path.join(__dirname, 'style-pdf.css');
const cssBaseContent = await fs.readFile(cssBasePath, 'utf-8');
const cssPdfContent = await fs.readFile(cssPdfPath, 'utf-8');
// Naviguer vers la page
await page.goto(`file://${htmlPath}`, {
waitUntil: 'networkidle'
});
// Forcer le rendu en mode "screen"
await page.emulateMedia({ media: 'screen' });
// Supprimer tous les liens CSS existants et injecter notre CSS directement
await page.evaluate(() => {
const links = Array.from(document.querySelectorAll('link[rel="stylesheet"]'));
links.forEach(link => link.remove());
});
// Injecter le CSS de base
await page.addStyleTag({ content: cssBaseContent });
// Injecter le CSS PDF
await page.addStyleTag({ content: cssPdfContent });
// Appliquer le thème et activer le mode PDF
await page.evaluate((selectedTheme) => {
document.documentElement.setAttribute('data-theme', selectedTheme);
// pdf-mode injection removed: PDF debug class should be added explicitly when needed
}, theme);
if (CONFIG.hideOnlineCvLink) {
await page.evaluate(() => {
const onlineLink = document.getElementById('online-cv-link');
if (!onlineLink) return;
const listItem = onlineLink.closest('li');
if (listItem) {
listItem.remove();
} else {
onlineLink.remove();
}
});
}
// Attendre que tout soit rendu
await page.waitForTimeout(1500);
// Vérifier la hauteur du contenu
const contentHeight = await page.evaluate(() => {
return document.querySelector('.container')?.scrollHeight || document.body.scrollHeight;
});
console.log(` Content height: ${contentHeight}px`);
if (options.raster) {
// Génération raster (capture fullPage + PDF) pour éviter tout découpage
const pngBuffer = await page.screenshot({
fullPage: true,
type: 'png'
});
const pdfDoc = await PDFDocument.create();
const pngImage = await pdfDoc.embedPng(pngBuffer);
const pngDims = pngImage.scale(1);
const pdfPage = pdfDoc.addPage([pngDims.width, pngDims.height]);
pdfPage.drawImage(pngImage, {
x: 0,
y: 0,
width: pngDims.width,
height: pngDims.height
});
const pdfBytes = await pdfDoc.save();
await fs.writeFile(outputPath, pdfBytes);
} else {
// Génération vectorielle standard (multi-pages A4)
await page.pdf({
path: outputPath,
...CONFIG.pdfOptions
});
}
await browser.close();
console.log(`✅ PDF generated: ${outputPath}\n`);
}
/**
* Génère toutes les combinaisons de PDFs
*/
async function generateAll(options = {}) {
console.log('🚀 Generating all PDF combinations...\n');
for (const locale of CONFIG.supportedLocales) {
for (const theme of CONFIG.supportedThemes) {
try {
await generatePdf(locale, theme, options);
} catch (error) {
console.error(`❌ Error generating ${locale}/${theme}:`, error.message);
}
}
}
console.log('🎉 All PDFs generated!');
}
// ============================================================================
// CLI
// ============================================================================
async function main() {
const args = process.argv.slice(2);
// Parse arguments
const options = {
locale: 'fr',
theme: 'dark',
all: false,
raster: false
};
for (let i = 0; i < args.length; i++) {
if (args[i] === '--locale' && args[i + 1]) {
options.locale = args[++i];
} else if (args[i] === '--theme' && args[i + 1]) {
options.theme = args[++i];
} else if (args[i] === '--all') {
options.all = true;
} else if (args[i] === '--raster') {
options.raster = true;
} else if (args[i] === '--vector') {
options.raster = false;
}
}
// Validate options
if (!CONFIG.supportedLocales.includes(options.locale)) {
console.error(`❌ Invalid locale: ${options.locale}`);
console.error(` Supported: ${CONFIG.supportedLocales.join(', ')}`);
process.exit(1);
}
if (!CONFIG.supportedThemes.includes(options.theme)) {
console.error(`❌ Invalid theme: ${options.theme}`);
console.error(` Supported: ${CONFIG.supportedThemes.join(', ')}`);
process.exit(1);
}
try {
if (options.all) {
await generateAll({ raster: options.raster });
} else {
await generatePdf(options.locale, options.theme, { raster: options.raster });
}
} catch (error) {
console.error('❌ Error:', error.message);
process.exit(1);
}
}
// Exécuter si appelé directement
if (require.main === module) {
main().catch(error => {
console.error('Fatal error:', error);
process.exit(1);
});
}
module.exports = { generatePdf, generateAll };