This guide demonstrates how to open an input document, convert it to an intermediate EditableDocument, and save it to disk as an HTML file with a resource folder using GroupDocs.Editor for Node.js via Java.
Almost all HTML WYSIWYG client-side editors are able to open HTML documents from disk (from a path). GroupDocs.Editor for Node.js via Java allows you to open any supported document, convert it to HTML, and save it to disk, which may be very useful for subsequent editing in a WYSIWYG editor. The code below demonstrates such an example.
// Import necessary modules
constfs=require('fs');constpath=require('path');constgroupdocsEditor=require('groupdocs-editor');// Specify the input file path
constinputFilePath='C:\\input_path\\document.docx';// Path to some document
// Create load options
constloadOptions=newgroupdocsEditor.WordProcessingLoadOptions();// Create an Editor instance with the input file and load options
consteditor=newgroupdocsEditor.Editor(inputFilePath,loadOptions);// Open the document for editing with format-specific edit options
consteditOptions=newgroupdocsEditor.WordProcessingEditOptions();consteditableDocument=editor.edit(editOptions);// Get the HTML content
consthtmlContent=editableDocument.getContent();// Specify the output HTML file path
constoutputHtmlFilePath='C:\\output_path\\document.html';// Save the HTML content to a file
fs.writeFileSync(outputHtmlFilePath,htmlContent);// If there are resources (images, stylesheets, fonts), save them to a folder
constresourcesFolderPath='C:\\output_path\\document_files';// Ensure the resources folder exists
if(!fs.existsSync(resourcesFolderPath)){fs.mkdirSync(resourcesFolderPath,{recursive:true});}// Get all resources
constresources=editableDocument.getAllResources();// Save each resource to the resources folder
resources.forEach((resource)=>{constresourceName=resource.getResourceName();constresourceContent=resource.getContent();constresourcePath=path.join(resourcesFolderPath,resourceName);fs.writeFileSync(resourcePath,resourceContent);});// Dispose of resources
editableDocument.dispose();editor.dispose();
In this example, we load an input WordProcessing (DOCX) document into the Editor class with load options specific for this document family type—WordProcessingLoadOptions. Then, the document is converted to an EditableDocument using the editor.edit() method, which, in turn, uses document-specific WordProcessingEditOptions. We then obtain the HTML content and save it to an HTML file on disk, specified by an absolute path.
Please note that GroupDocs.Editor does not automatically create an accompanying folder with resources in Node.js via Java, so we manually extract the resources from the EditableDocument and save them to a specified folder. The resources may include images, stylesheets, and fonts used by the HTML document.
By the way, don’t forget to dispose of all resources when you’re done to free up system resources.
// Dispose of resources
editableDocument.dispose();editor.dispose();
By following this guide, you can effectively save edited documents as HTML files along with their associated resources using GroupDocs.Editor for Node.js via Java. This is particularly useful when you need to provide the HTML content to client-side WYSIWYG editors or for other purposes that require the HTML files to be stored on disk.
Note: Be sure to replace the paths in the code examples with the actual paths on your system.