Node.js Express Word Document DOCX or DOC to PDF Converter Full App Using LibreOffice Library 2020

Node.js Express Word Document DOCX or DOC to PDF Converter Full App Using LibreOffice Library 2020

 

 

 

 

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

 

<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>
<!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>

 

 

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}`);
});

 

See also  Build a CRUD Random Password Generator and Manager Using jQueryUI and Modernizr Library in Browser Full Project For Beginners

 

 

 

 

 

 

 

Leave a Reply