-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (78 loc) · 2.29 KB
/
index.js
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
const express = require('express');
const axios = require('axios');
const { PDFDocument, rgb } = require('pdf-lib');
const app = express();
const PORT = process.env.PORT || 3000;
// Generate CV endpoint
app.get('/generate-cv', async (req, res) => {
const { username } = req.query;
if (!username) {
return res.status(400).json({ error: 'Username is required' });
}
try {
// Fetch user data from GitHub API
const userResponse = await axios.get(`https://api.github.com/users/${username}`);
const reposResponse = await axios.get(`https://api.github.com/users/${username}/repos`);
// Extract relevant user and repository information
const userData = userResponse.data;
const repositories = reposResponse.data;
// Generate PDF
const pdfDoc = await generatePdf(userData, repositories);
// Send PDF as response
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', `attachment; filename="${username}_CV.pdf"`);
res.send(await pdfDoc.save());
} catch (error) {
console.error('Failed to generate CV:', error.message);
res.status(500).json({ error: 'Failed to generate CV' });
}
});
// Function to generate PDF
async function generatePdf(userData, repositories) {
const pdfDoc = await PDFDocument.create();
const page = pdfDoc.addPage();
const { width, height } = page.getSize();
// Set font and font size
const font = await pdfDoc.embedFont('Helvetica');
const fontSize = 18;
// Add header
page.drawText(`GitHub CV for ${userData.login}`, {
x: 50,
y: height - 100,
size: fontSize,
font,
color: rgb(0, 0, 0),
});
// Add user information
page.drawText(`Name: ${userData.name || 'N/A'}`, {
x: 50,
y: height - 150,
size: fontSize,
font,
color: rgb(0, 0, 0),
});
// Add repository list
page.drawText('Repositories:', {
x: 50,
y: height - 200,
size: fontSize,
font,
color: rgb(0, 0, 0),
});
let repoY = height - 230;
repositories.slice(0, 5).forEach((repo) => {
page.drawText(`- ${repo.name}`, {
x: 70,
y: repoY,
size: fontSize - 2,
font,
color: rgb(0, 0, 0),
});
repoY -= 20;
});
return pdfDoc;
}
// Start server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});