Home >Web Front-end >Front-end Q&A >How nodejs converts HTML to Word document
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.
First, we need to install some dependent libraries. Enter the following code in the terminal to install:
npm install mammoth
After the installation is complete, we need to introduce mammoth to convert HTML to Word documents.
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();
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.
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();
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!