Certainly! Below is a comprehensive example of an advanced HTML website that allows users to edit CSS styles, input HTML content, preview the styled content in real-time, and generate a Word (.docx) file based on the content and styles provided.
This implementation uses the html-docx-js library to convert HTML and CSS into a Word document. The website includes functionality to add multiple CSS style blocks, edit them, and update the live preview accordingly.
Features
- Editable CSS Styles: Users can add multiple CSS style blocks, edit them, and remove them as needed.
- HTML Content Editor: A
contenteditable div allows users to input and format HTML content.
- Live Preview: A section that displays the styled content in real-time based on the CSS and HTML provided.
- Generate Word File: A button that converts the current HTML and CSS into a
.docx file and triggers a download.
Complete HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Advanced HTML to Word Generator</title>
<!-- Include html-docx.js library -->
<script src="https://unpkg.com/html-docx-js/dist/html-docx.js"></script>
<!-- Include FileSaver.js library for saving files -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
}
h1 {
text-align: center;
color: #333;
}
.container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.editor-section, .preview-section {
background: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
flex: 1 1 45%;
min-width: 300px;
}
.editor-section {
max-width: 600px;
}
.editor-group {
margin-bottom: 20px;
}
.editor-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.style-editor {
width: 100%;
height: 150px;
padding: 10px;
font-family: monospace;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
}
.add-style-btn, .generate-btn {
padding: 10px 15px;
font-size: 16px;
background-color: #28a745;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
}
.add-style-btn {
background-color: #007bff;
margin-bottom: 20px;
}
.add-style-btn:hover, .generate-btn:hover {
background-color: #0056b3;
}
.remove-style-btn {
background-color: #dc3545;
color: #fff;
border: none;
padding: 5px 8px;
border-radius: 3px;
cursor: pointer;
float: right;
}
.remove-style-btn:hover {
background-color: #c82333;
}
#content-editor {
width: 100%;
height: 300px;
padding: 10px;
font-family: Arial, sans-serif;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
overflow: auto;
}
#live-preview {
width: 100%;
min-height: 300px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
overflow: auto;
background-color: #fff;
}
.generate-btn {
display: block;
width: 100%;
margin-top: 20px;
background-color: #28a745;
}
/* Responsive Design */
@media (max-width: 768px) {
.container {
flex-direction: column;
}
}
</style>
</head>
<body>
<h1>Advanced HTML to Word Generator</h1>
<div class="container">
<!-- Editor Section -->
<div class="editor-section">
<!-- Styles Editor -->
<div class="editor-group">
<label>CSS Styles:</label>
<div id="styles-container">
<div class="style-block">
<button class="remove-style-btn" onclick="removeStyleBlock(this)" title="Remove Style">×</button>
<textarea class="style-editor" placeholder="Enter CSS styles here...">
/* Example CSS */
body {
font-family: Arial, sans-serif;
}
h1 {
color: #007bff;
}
p {
font-size: 16px;
line-height: 1.5;
}
</textarea>
</div>
</div>
<button class="add-style-btn" onclick="addStyleBlock()">Add Style</button>
</div>
<!-- Content Editor -->
<div class="editor-group">
<label>HTML Content:</label>
<div id="content-editor" contenteditable="true">
<h1>Welcome to the Word Generator</h1>
<p>This is a sample paragraph. You can style it using the CSS editor above.</p>
</div>
</div>
<!-- Generate Button -->
<button class="generate-btn" onclick="generateWord()">Generate Word File</button>
</div>
<!-- Preview Section -->
<div class="preview-section">
<label>Live Preview:</label>
<div id="live-preview">
<!-- Styled Content will appear here -->
</div>
</div>
</div>
<script>
// Function to add a new style block
function addStyleBlock() {
const stylesContainer = document.getElementById('styles-container');
const styleBlock = document.createElement('div');
styleBlock.className = 'style-block';
styleBlock.innerHTML = `
<button class="remove-style-btn" onclick="removeStyleBlock(this)" title="Remove Style">×</button>
<textarea class="style-editor" placeholder="Enter CSS styles here..."></textarea>
`;
stylesContainer.appendChild(styleBlock);
attachStyleListeners();
}
// Function to remove a style block
function removeStyleBlock(button) {
const styleBlock = button.parentElement;
styleBlock.remove();
updatePreview();
}
// Function to attach input listeners to all style editors
function attachStyleListeners() {
const styleEditors = document.querySelectorAll('.style-editor');
styleEditors.forEach(editor => {
editor.addEventListener('input', updatePreview);
});
}
// Function to update the live preview
function updatePreview() {
const styles = document.querySelectorAll('.style-editor');
let combinedStyles = '';
styles.forEach(style => {
combinedStyles += style.value + '\n';
});
const content = document.getElementById('content-editor').innerHTML;
const preview = document.getElementById('live-preview');
preview.innerHTML = `<style>${combinedStyles}</style>` + content;
}
// Initial attachment of listeners
window.onload = function() {
attachStyleListeners();
document.getElementById('content-editor').addEventListener('input', updatePreview);
updatePreview();
}
// Function to generate Word document
function generateWord() {
const styles = document.querySelectorAll('.style-editor');
let combinedStyles = '';
styles.forEach(style => {
combinedStyles += style.value + '\n';
});
const content = document.getElementById('content-editor').innerHTML;
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Generated Document</title>
<style>
${combinedStyles}
</style>
</head>
<body>
${content}
</body>
</html>
`;
// Convert the HTML content to a Blob
const converted = window.htmlDocx.asBlob(htmlContent);
// Use FileSaver.js to save the file
saveAs(converted, 'document.docx');
}
</script>
</body>
</html>
How It Works
-
HTML Structure:
- The page is divided into two main sections: the Editor Section and the Preview Section.
- Editor Section:
- CSS Styles Editor: Users can input CSS styles. They can add multiple style blocks by clicking the "Add Style" button or remove existing ones using the remove (
×) button.
- HTML Content Editor: A
contenteditable div allows users to input and format their HTML content directly.
- Generate Button: When clicked, it triggers the conversion of the current HTML and CSS into a Word document.
- Preview Section:
- Displays a live preview of the content styled with the provided CSS.
-
JavaScript Functionality:
- addStyleBlock(): Adds a new CSS style editor to the styles container.
- removeStyleBlock(button): Removes the corresponding CSS style editor when the remove button is clicked.
- attachStyleListeners(): Attaches input event listeners to all CSS style editors to update the live preview in real-time whenever the CSS is modified.
- updatePreview(): Combines all CSS styles and applies them to the HTML content, updating the live preview accordingly.
- generateWord():
- Combines the CSS and HTML content into a single HTML string.
- Uses the
html-docx-js library to convert the HTML string into a Blob representing the Word document.
- Utilizes
FileSaver.js to prompt the user to download the generated .docx file.
-
Libraries Used:
- html-docx-js: Converts HTML and CSS into a Word document (
.docx format).
- FileSaver.js: Facilitates the download of the generated Word document on the client side.
-
Styling:
- Basic styling is applied to make the interface user-friendly and responsive.
- Users can resize the CSS and content editors as needed.
Usage Instructions
-
Edit CSS Styles:
- Use the provided CSS editor(s) to define styles.
- Click the "Add Style" button to include additional CSS blocks as needed.
- Remove any unwanted CSS blocks by clicking the
× button on the corresponding block.
-
Edit HTML Content:
- Click inside the "HTML Content" editor to add or modify your HTML content.
- You can use standard HTML tags to format your content (e.g.,
<h1>, <p>, <strong>, <em>, etc.).
-
Preview:
- As you make changes to the CSS or HTML content, the "Live Preview" section will update in real-time to reflect the styles and content.
-
Generate Word Document:
- Once you're satisfied with your content and styling, click the "Generate Word File" button.
- A
.docx file named document.docx will be generated and downloaded to your computer, containing your styled content.
Notes
- Security Considerations: Since this application allows users to input arbitrary HTML and CSS, ensure that it's used in a trusted environment to prevent potential security risks like Cross-Site Scripting (XSS).
- Browser Compatibility: This solution should work on all modern browsers. However, always test across different browsers to ensure compatibility.
- Limitations: Complex CSS or certain advanced HTML features might not be perfectly translated into the Word document due to limitations in the conversion library.
Feel free to customize and extend this template according to your specific requirements!