How nodejs converts HTML to Word document

PHPz
Release: 2023-04-06 09:40:30
Original
1903 people have browsed it

At work, we often need to convert HTML format documents to Word documents, such as converting a resume or report file from web format to Word format. The traditional method is to use Microsoft Word or other paid software for processing, but these software are expensive and not fully compatible with various HTML tags and styles. In this case, we can consider using Node.js to convert HTML to Word.

This article will introduce how to use Node.js and its related npm library to convert HTML into Word documents.

  1. Install dependent libraries

First, we need to install some dependent libraries. Enter the following code in the terminal to install:

npm install mammoth
Copy after login

After the installation is complete, we need to introduce mammoth to convert HTML to Word documents.

  1. Convert HTML to Docx

Use the following code to convert the HTML file to a Word document in docx format:

const mammoth = require("mammoth");

mammoth.convertToHtml({ path: "input.html"})
.then((result) => {
  const options = {
    styleMap: [
      "p[style-name='Section Title'] => h1",
      "p[style-name='Subsection Title'] => h2"
    ]
  };
  return mammoth.convertToDocx({ buffer: result.value }, options);
})
.then((result) => {
  console.log(result);
})
.done();
Copy after login

in the code The convertToHtml method can convert an HTML file to HTML in Word format, and then we can use the convertToDocx method to convert it into a Word document. During this process, we can also add style mapping rules to specify which styles in HTML are mapped to styles in Word documents through the styleMap parameter.

  1. Full code example

The following is a complete example that demonstrates how to convert an HTML file to a Word document. In the code example, we convert input.html to a Word document and save it to the output.docx file.

const mammoth = require("mammoth");
const fs = require("fs");

mammoth.convertToHtml({ path: "input.html"})
.then((result) => {
  const options = {
    styleMap: [
      "p[style-name='Section Title'] => h1",
      "p[style-name='Subsection Title'] => h2"
    ]
  };
  return mammoth.convertToDocx({ buffer: result.value }, options);
})
.then((result) => {
  fs.writeFileSync("output.docx", result.value);
})
.done();
Copy after login

After running the above code to complete the conversion, the converted Word document can be saved in the output.docx file.

Conclusion

This article introduces how to use Node.js and related dependent libraries to convert HTML files into Word documents. The benefits of using Node.js are that you avoid expensive paid software and can do custom style mapping. If you need to convert HTML to Word at work, you might as well try the Node.js method!

The above is the detailed content of How nodejs converts HTML to Word document. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!