Welcome folks today in this blog post we will be building word document to pdf document
in browser using libreOffice
in node.js
and express using Javascript. All the source code of the application is shown below.
Get Started
In order to get started you need to make a new node.js
project using the below command as shown below
npm init -y
npm i multer
npm i express
npm i libreoffice-convert
After that you need to make an index.html
file and copy paste the following code
index.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 |
<!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> |
As you can see we have the input field where we allow the user to upload the docx
file to convert to pdf
document using the libreoffice library.
And now we need to make a POST
request to the docxtodemopdf
as shown below in the index.js
file which is present inside the root directory
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 |
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}`); }); |
As you can see we are using the libreoffice-convert
library to convert the docx
to pdf documents inside the web app.