From f1058df26be6125e27278593e75199b118565f1c Mon Sep 17 00:00:00 2001 From: "codeshwar-preview[bot]" <160849357+codeshwar-preview[bot]@users.noreply.github.com> Date: Thu, 6 Feb 2025 05:12:06 +0000 Subject: [PATCH] Update files in fasdf-74 --- index.html | 15 +++++-- public/js/interactivity.js | 38 ++++++++++++++++- public/styles/main.css | 54 ++++++++++++++++++++++-- routes/jpegToPdfRoute.js | 69 +++++++++++++++++++++++++++++++ server.js | 5 ++- views/jpegToPdf.ejs | 85 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 256 insertions(+), 10 deletions(-) create mode 100644 routes/jpegToPdfRoute.js create mode 100644 views/jpegToPdf.ejs diff --git a/index.html b/index.html index 4a2cbef..7d61178 100644 --- a/index.html +++ b/index.html @@ -1,4 +1,3 @@ - @@ -89,12 +88,18 @@ left: 0; width: 100%; height: 100%; - background-image: url('images/Removebackgroundproject.png'); /* Updated background image */ + background-image: url('images/Removebackgroundproject.png'); background-size: cover; background-repeat: no-repeat; - opacity: 0.2; /* Adjusted the opacity here */ + opacity: 0.2; z-index: 0; } + .jpeg-to-pdf-link { + margin: 0 15px; + text-decoration: none; + color: #007BFF; + font-weight: bold; + } @@ -114,6 +119,7 @@ Layoff Tracker + JPEG to PDF Log in Sign Up @@ -134,4 +140,5 @@

Don't miss your dream tech job // The hover effect for the Login button will be handled in main.css - \ No newline at end of file + + \ No newline at end of file diff --git a/public/js/interactivity.js b/public/js/interactivity.js index dc75b3e..8dc50ed 100644 --- a/public/js/interactivity.js +++ b/public/js/interactivity.js @@ -1,4 +1,3 @@ -document.addEventListener('DOMContentLoaded', function() { document.getElementById('loginForm').addEventListener('submit', function(e) { e.preventDefault(); const username = document.getElementById('username').value; @@ -39,6 +38,19 @@ document.addEventListener('DOMContentLoaded', function() { e.preventDefault(); window.location.href = '/jobpost'; }); + + document.getElementById('uploadForm').addEventListener('submit', function(e) { + e.preventDefault(); + const fileInput = document.getElementById('jpegFile'); + const file = fileInput.files[0]; + + if (!file || file.type !== 'image/jpeg') { + alert('Please upload a valid JPEG file.'); + return; + } + + handleFormSubmission(e); + }); }); const express = require('express'); @@ -146,4 +158,28 @@ function submitJobPosting(jobData) { function validateEmail(email) { const re = /^(([^<>()\[\]\\.,;:\s@\"]+(\.[^<>()\[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(email); +} + +function handleFormSubmission(e) { + const formData = new FormData(e.target); + fetch('/convert', { + method: 'POST', + body: formData + }) + .then(response => response.json()) + .then(data => { + if (data.success) { + alert('Conversion successful! Download your PDF.'); + const downloadLink = document.createElement('a'); + downloadLink.href = data.downloadUrl; + downloadLink.textContent = 'Download PDF'; + document.body.appendChild(downloadLink); + } else { + alert('Conversion failed: ' + data.message); + } + }) + .catch(error => { + console.error('Error during conversion:', error); + alert('An error occurred during conversion. Please try again.'); + }); } \ No newline at end of file diff --git a/public/styles/main.css b/public/styles/main.css index 1048525..0c0aaac 100644 --- a/public/styles/main.css +++ b/public/styles/main.css @@ -1,4 +1,3 @@ -* { margin: 0; padding: 0; box-sizing: border-box; @@ -102,7 +101,6 @@ button:focus { outline: 3px solid #007bff; } - @media (max-width: 768px) { .container { width: 95%; @@ -111,10 +109,60 @@ button:focus { footer { background-color: #000; - color: #000; /* Corrected color to white for visibility */ + color: #fff; padding: 20px; text-align: center; text-shadow: none; z-index: 1000; position: relative; } + +/* Additional styles for JPEG to PDF conversion page */ +#uploadForm { + background: #ffffff; + padding: 20px; + border-radius: 8px; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); + margin-top: 20px; +} + +#uploadForm label { + display: block; + margin-bottom: 10px; + font-weight: bold; +} + +#uploadForm input[type="file"] { + display: block; + margin-bottom: 20px; +} + +#downloadLink { + margin-top: 20px; + padding: 20px; + background: #e9ecef; + border-radius: 8px; + text-align: center; +} + +#downloadLink h2 { + margin-bottom: 10px; +} + +#downloadLink a { + color: #007fff; + text-decoration: none; + font-weight: bold; +} + +#downloadLink a:hover { + text-decoration: underline; +} + +@media (max-width: 768px) { + #uploadForm, + #downloadLink { + width: 95%; + margin: 20px auto; + } +} \ No newline at end of file diff --git a/routes/jpegToPdfRoute.js b/routes/jpegToPdfRoute.js new file mode 100644 index 0000000..90fb598 --- /dev/null +++ b/routes/jpegToPdfRoute.js @@ -0,0 +1,69 @@ +const express = require('express'); +const multer = require('multer'); +const fs = require('fs'); +const PDFDocument = require('pdfkit'); // Using pdfkit for PDF creation +const path = require('path'); + +// Initialize the router +const router = express.Router(); + +// Configure multer for file uploads +const upload = multer({ + dest: 'uploads/', + fileFilter: (req, file, cb) => { + if (file.mimetype === 'image/jpeg') { + cb(null, true); + } else { + cb(new Error('Only JPEG files are allowed!'), false); + } + } +}); + +// Route to handle JPEG to PDF conversion +router.post('/convert', upload.single('jpegFile'), (req, res) => { + try { + // Validate file presence + if (!req.file) { + return res.status(400).send('No file uploaded.'); + } + + // Create a PDF document + const doc = new PDFDocument(); + const outputFilePath = path.join(__dirname, 'output', `${Date.now()}.pdf`); + const writeStream = fs.createWriteStream(outputFilePath); + + // Pipe the PDF document to a file + doc.pipe(writeStream); + + // Add the JPEG image to the PDF + doc.image(req.file.path, { + fit: [500, 400], + align: 'center', + valign: 'center' + }); + + // Finalize the PDF and end the stream + doc.end(); + + // Handle the finish event of the write stream + writeStream.on('finish', () => { + // Send the PDF file as a response + res.download(outputFilePath, (err) => { + if (err) { + res.status(500).send('Error downloading the file.'); + } + + // Clean up the uploaded JPEG file + fs.unlink(req.file.path, (err) => { + if (err) console.error('Error deleting the uploaded JPEG file:', err); + }); + }); + }); + + } catch (error) { + res.status(500).send('An error occurred during the conversion process.'); + } +}); + +// Export the router +module.exports = router; \ No newline at end of file diff --git a/server.js b/server.js index 6b10695..feda232 100644 --- a/server.js +++ b/server.js @@ -1,4 +1,3 @@ -const express = require('express'); const mongoose = require('mongoose'); const session = require('express-session'); const MongoStore = require('connect-mongo'); @@ -10,6 +9,7 @@ const PORT = process.env.PORT || 3000; const authRoutes = require('./routes/authRoutes'); const jobRoutes = require('./routes/jobRoutes'); +const jpegToPdfRoute = require('./routes/jpegToPdfRoute'); mongoose.connect(process.env.DB_CONNECTION_STRING, { useNewUrlParser: true, @@ -32,6 +32,7 @@ app.set('view engine', 'ejs'); app.use(authRoutes); app.use(jobRoutes); +app.use('/convert_jpeg_to_pdf', jpegToPdfRoute); app.use((err, req, res, next) => { console.error(err.stack); @@ -40,4 +41,4 @@ app.use((err, req, res, next) => { app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); -}); +}); \ No newline at end of file diff --git a/views/jpegToPdf.ejs b/views/jpegToPdf.ejs new file mode 100644 index 0000000..0c418a7 --- /dev/null +++ b/views/jpegToPdf.ejs @@ -0,0 +1,85 @@ + + + + + + JPEG to PDF Converter + + +
+

JPEG to PDF Converter

+
+ + + +
+ +
+ + + + +``` + +```javascript +// interactivity.js + +export function handleFormSubmission(event) { + event.preventDefault(); + const formData = new FormData(event.target); + + fetch('/convert', { + method: 'POST', + body: formData + }) + .then(response => response.json()) + .then(data => { + if (data.success) { + const downloadLink = document.getElementById('downloadLink'); + const pdfLink = document.getElementById('pdfLink'); + pdfLink.href = data.pdfUrl; + downloadLink.style.display = 'block'; + } else { + alert('Conversion failed. Please try again.'); + } + }) + .catch(error => console.error('Error:', error)); +} +``` + +```javascript +// server.js + +const express = require('express'); +const multer = require('multer'); +const { convertJpegToPdf } = require('./conversionService'); +const path = require('path'); + +const app = express(); +const upload = multer({ dest: 'uploads/' }); + +app.post('/convert', upload.single('jpegFile'), (req, res) => { + const jpegFilePath = req.file.path; + const pdfFilePath = path.join('converted', `${req.file.filename}.pdf`); + + convertJpegToPdf(jpegFilePath, pdfFilePath) + .then(() => { + res.json({ success: true, pdfUrl: `/converted/${req.file.filename}.pdf` }); + }) + .catch(err => { + console.error(err); + res.json({ success: false }); + }); +}); + +app.use('/converted', express.static(path.join(__dirname, 'converted'))); + +app.listen(3000, () => { + console.log('Server is running on port 3000'); +}); \ No newline at end of file