Word to PDF Converter
npm install express mammoth
const express = require(‘express’);
const multer = require(‘multer’);
const mammoth = require(‘mammoth’);
const fs = require(‘fs’);
const app = express();
const port = 3000;
// Set up multer for handling file uploads
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
app.use(express.static(‘public’));
app.post(‘/convert’, upload.single(‘wordFile’), (req, res) => {
if (!req.file) {
return res.status(400).json({ error: ‘No file uploaded.’ });
}
const wordBuffer = req.file.buffer;
mammoth.extractRawText({ arrayBuffer: wordBuffer })
.then(result => {
const htmlContent = result.value;
// Use a PDF conversion library (e.g., html-pdf) here
// to convert the extracted HTML content to a PDF file.
// For simplicity, we’re saving the HTML content to a file.
fs.writeFileSync(‘output.html’, htmlContent);
res.json({ success: true, message: ‘Conversion successful.’ });
})
.catch(error => {
console.error(error);
res.status(500).json({ error: ‘Error during conversion.’ });
});
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
Word to PDF Converter
node server.js