0% found this document useful (0 votes)
1 views4 pages

API

The document outlines a Node.js application using Express and Puppeteer to generate PDF resumes based on user input. It includes a POST endpoint '/generate-pdf' that accepts resume data, dynamically creates HTML content based on selected templates, and returns a generated PDF. Additionally, it features a React component for editing content and a function to handle PDF generation requests from the client side.

Uploaded by

Nilanjana Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

API

The document outlines a Node.js application using Express and Puppeteer to generate PDF resumes based on user input. It includes a POST endpoint '/generate-pdf' that accepts resume data, dynamically creates HTML content based on selected templates, and returns a generated PDF. Additionally, it features a React component for editing content and a function to handle PDF generation requests from the client side.

Uploaded by

Nilanjana Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

const express = require('express');

const puppeteer = require('puppeteer');

const app = express();


const PORT = 5000;

app.use(express.json());

app.post('/generate-pdf', async (req, res) => {


const { basicInfoData, eduData, expData, summaryText, skills } = req.body;

const html = `
<html>
<head>
<style>
body { font-family: Arial, sans-serif; padding: 40px; }
h1 { color: #222; }
.section { margin-bottom: 20px; }
</style>
</head>
<body>
<h1>${basicInfoData.fullName}</h1>

<div class="section">
<h2>Education</h2>
${eduData.map(edu => `<p>${edu.degree} at
${edu.institution}</p>`).join('')}
</div>

<div class="section">
<h2>Experience</h2>
${expData.map(exp => `<p>${exp.position} at
${exp.company}</p>`).join('')}
</div>

<div class="section">
<h2>Skills</h2>
<p>${skills.join(', ')}</p>
</div>

<div class="section">
<h2>Summary</h2>
<p>${summaryText}</p>
</div>
</body>
</html>
`;

try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(html, { waitUntil: 'networkidle0' });

const pdf = await page.pdf({ format: 'A4' });


await browser.close();

res.set({
'Content-Type': 'application/pdf',
'Content-Disposition': 'inline; filename="resume.pdf"',
});

res.send(pdf);
} catch (error) {
console.error('PDF generation error:', error);
res.status(500).send('Failed to generate PDF');
}
});

app.listen(PORT, () => {
console.log(`PDF generator running at http://localhost:${PORT}`);
});

<ReactQuill
className="custom-editor"
modules={modules}
formats={formats}
placeholder="Write your additional details...."
onChange={handleContentChange}
value={customField}
/>

<style jsx global>{`


.custom-editor .ql-container {
border: none;
background-color: rgb(239, 242, 249);
min-height: 2220px;
}

.custom-editor .ql-editor {
background-color: rgb(239, 242, 249);
min-height: 2220px;
}
`}</style>

app.post('/generate-pdf', async (req, res) => {


const { basicInfoData, eduData, expData, summaryText, skills, selectedTemplate }
= req.body;

// Dynamically generate HTML based on selected template


let htmlTemplate = '';

if (selectedTemplate === 'template1') {


htmlTemplate = `
<html>
<head>
<style>
body { font-family: Arial, sans-serif; padding: 40px; }
h1 { color: #222; }
.section { margin-bottom: 20px; }
</style>
</head>
<body>
<h1>${basicInfoData.fullName}</h1>
<div class="section">
<h2>Education</h2>
${eduData.map(edu => `<p>${edu.degree} at
${edu.institution}</p>`).join('')}
</div>
<div class="section">
<h2>Experience</h2>
${expData.map(exp => `<p>${exp.position} at
${exp.company}</p>`).join('')}
</div>
<div class="section">
<h2>Skills</h2>
<p>${skills.join(', ')}</p>
</div>
<div class="section">
<h2>Summary</h2>
<p>${summaryText}</p>
</div>
</body>
</html>
`;
} else if (selectedTemplate === 'template2') {
htmlTemplate = `
<html>
<head>
<style>
body { font-family: 'Times New Roman', serif; padding: 30px; }
h1 { text-align: center; color: #333; }
.section { margin-bottom: 30px; }
</style>
</head>
<body>
<h1>${basicInfoData.fullName}</h1>
<div class="section">
<h2>Education</h2>
${eduData.map(edu => `<p><strong>${edu.degree}</strong> at <em>$
{edu.institution}</em></p>`).join('')}
</div>
<div class="section">
<h2>Experience</h2>
${expData.map(exp => `<p><strong>${exp.position}</strong> at <i>$
{exp.company}</i></p>`).join('')}
</div>
<div class="section">
<h2>Skills</h2>
<ul>${skills.map(skill => `<li>${skill}</li>`).join('')}</ul>
</div>
<div class="section">
<h2>Summary</h2>
<p>${summaryText}</p>
</div>
</body>
</html>
`;
}

try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(htmlTemplate, { waitUntil: 'networkidle0' });

const pdf = await page.pdf({


scale: 1,
displayHeaderFooter: true,
printBackground: true,
margin: {
top: 10,
right: 10,
bottom: 10,
left: 10,
},
format: 'A4',
});
await browser.close();

res.set({
'Content-Type': 'application/pdf',
'Content-Disposition': 'inline; filename="resume.pdf"',
});

res.send(pdf);
} catch (error) {
console.error('PDF generation error:', error);
res.status(500).send('Failed to generate PDF');
}
});

const handleGeneratePDF = async () => {


const resumeData = {
basicInfoData: { fullName: "John Doe", email: "[email protected]" },
eduData: [{ degree: "Bachelor's", institution: "XYZ University" }],
expData: [{ position: "Software Engineer", company: "ABC Corp" }],
summaryText: "Passionate about software development...",
skills: ["JavaScript", "React", "Node.js"],
selectedTemplate: selectedTemplate // 'template1' or 'template2'
};

try {
const response = await fetch('http://localhost:5000/generate-pdf', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(resumeData),
});

if (response.ok) {
const blob = await response.blob();
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'resume.pdf';
link.click();
} else {
console.error('Failed to generate PDF');
}
} catch (error) {
console.error('Error:', error);
}
};

You might also like