0

last few days I am fetching problem with my project when I am converting docx to html and after edit the html with angular editor I am saving the html into docx file again. I am using the following npm module below for converting.

Convert docx to html:
var mammoth = require("mammoth");

Convert html to docx:
var htmlDocx = require('html-docx-js')

Both module is working fine but problem is, when I am converting any html to docx from my website and then trying to convert it into html again, it's giving the response below and do not providing any html as value. enter image description here

Here is my complete nodejs API function below:

Docx to html:
exports.convertDocxToHtml = catchAsync(async (req, res, next) => {
  let url = 'https://teamee-drive-bucket.s3.amazonaws.com/drive/untitle.docx';
  const response = await axios.get(url, { responseType: 'arraybuffer' })
  const result = await mammoth.convertToHtml({ buffer: Buffer.from(response.data, "utf-8") })

  // let url = 'src/controllers/drive/default.docx';
  // // let url = 'src/controllers/drive/Imran4.docx';
  // const result = await mammoth.convertToHtml({ path: url })
  console.log(result)

  res.status(200).json({
    status: 'success',
    data: result.value,
    message: "Data loaded success!"
  });
});

Html to Docx:
exports.saveHtmlToDocx = catchAsync(async (req, res, next) => {
  let body = req.body
  // console.log(body)
  const htmlString = `<!DOCTYPE html>
  <html>
  <head>
  <title></title>
  </head>
  <body>${body.html}</body>
  </html>`
  const opt = {
    margin: {
      top: 100
    },
    orientation: 'landscape'
  }
  var converted = htmlDocx.asBlob(htmlString, opt);
  console.log(converted)

  const s3 = new AWS.S3()
  const param = {
    Bucket: process.env.AWS_BUCKET_NAME + '/drive',
    Key: `${body.document_title}.docx`,
    Body: converted
  }
  let data =  await s3.upload(param).promise()

  const update = await DriveFile.updateOne(
    {_id:body.id},
    {$set:{
      url:data.Location,
      folderId:body.folderId,
      fileName:`${body.document_title}.docx`
    }}
  )
  if (!update) {
      return next(new AppError("Document update faile", 400))
  }
  
  res.status(200).json({
    status: 'success',
    data: update,
    message: "Data save success!"
  });
});

mammoth converting to html perfectly when I am trying with Microsoft office file but problem is when I am trying with generated file from my website.

Al Imran
  • 115
  • 1
  • 2
  • 12
  • @jonsson Actually I know that, what you saying but I expecting a solution from some one who did fixed like this problem already. Thanks for your comment :) – Al Imran Jul 25 '22 at 10:53

0 Answers0