Live Demo
You can see the live demo of the tool here
Get Started
In order to get started you need to install the libraries which are given below
npm i express
npm i multer
npm i libreoffice-convert
npm i nodemon
Now add some html code for the web application like this
1 2 3 4 5 6 7 8 |
<div class="col-md-4 col-sm-6 col-xs-12"> <div class="content colour-2"> <h3 class="text-center">PDF to DOCX DEMO</h3> <div class="text-center"> <a class="btn btn-danger" href="/docxtopdfdemo">Convert Now</a> </div> </div> </div> |
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 |
<!DOCTYPE html> <html> <head> <title>DOCX to PDF Converter</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" /> </head> <body> <div class="container"> <h1 class="text-center"> DOCX to PDF Converter </h1> <form action="/docxtopdfdemo" method="post" enctype="multipart/form-data"> <div class="form-group"> <input type="file" name="file" id="" required class="form-control"> </div> <div class="form-group"> <button class="btn btn-danger btn-block"> Convert to DOCX </button> </div> </form> </div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> </html> |
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 |
const express = require("express"); const bodyParser = require("body-parser"); const libre = require('libreoffice-convert'); const fs = require("fs"); const path = require("path"); var outputFilePath; const multer = require("multer"); const app = express(); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); const PORT = process.env.PORT || 5000; app.use(express.static("public")); var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, "public/uploads"); }, filename: function (req, file, cb) { cb( null, file.fieldname + "-" + Date.now() + path.extname(file.originalname) ); }, }); app.get('/docxtopdfdemo',(req,res) => { res.render('docxtopdfdemo',{title:"DOCX to PDF Converter - Free Media Tools"}) }) const docxtopdfdemo = function (req, file, callback) { var ext = path.extname(file.originalname); if ( ext !== ".docx" && ext !== ".doc" ) { return callback("This Extension is not supported"); } callback(null, true); }; const docxtopdfdemoupload = multer({storage:storage,fileFilter:docxtopdfdemo}) app.post('/docxtopdfdemo',docxtopdfdemoupload.single('file'),(req,res) => { if(req.file){ console.log(req.file.path) const file = fs.readFileSync(req.file.path); outputFilePath = Date.now() + "output.pdf" libre.convert(file,".pdf",undefined,(err,done) => { if(err){ fs.unlinkSync(req.file.path) fs.unlinkSync(outputFilePath) res.send("some error taken place in conversion process") } fs.writeFileSync(outputFilePath, done); res.download(outputFilePath,(err) => { if(err){ fs.unlinkSync(req.file.path) fs.unlinkSync(outputFilePath) res.send("some error taken place in downloading the file") } fs.unlinkSync(req.file.path) fs.unlinkSync(outputFilePath) }) }) } }) app.listen(PORT, () => { console.log(`App is listening on Port ${PORT}`); }); |