npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-css-export-word

v1.0.0

Published

Rough and ready way to convert CSS-styled HTML to Word

Downloads

54

Readme

html-css-export-word

A function that converts CSS-styled HTML to a "single-page web page", which can be opened, edited, and saved on Microsoft Word for Mac.

Origins

This is a port of jQuery-Export-Word, only with the jQuery-specific syntax removed, and the image-processing functionality removed. The hard work was already done by Mark Swindoll 10 years ago in 2014.

Other tools

My research on other similiar existing tools are in this Stack Overflow answer.

Specially, on the server side, pandoc-server in pandoc is worth considering. But it comes with some extra costs as elaborated in my answer above.

On the choice of .doc over .docx, see this article for this Ruby tool.

Demo

Interactive demo: enter html and css and download the resulting Word file (source code).

Example in use: the "Download as Word.doc file" button at the lower right corner.

To test on your local machine, open test.html with your browser.

Usage

React (prior to React 19)

import {htmlCssExportWord} from "html-css-export-word"

<div className="hidden">
    <ComponentToPrint ref={sourceRef} id="source-html" />
</div>
<button onClick={() => {
    htmlCssExportWord(
    sourceRef.current.innerHTML, cssFile,
    "exported-document.doc")}}>
    Download as Word.doc file
</button>

Plain html

const htmlInput = document.getElementById("htmlInput")
const cssInput = document.getElementById("cssInput")
const result = document.getElementById("result").contentWindow.document

const input = document.querySelector("input")
const button = document.querySelector("button")

function updateResult() {
  result.open()
  result.write(
    `<html><head><style>${cssInput.value}</style></head><body>${htmlInput.value}</body></html>`
  )
  result.close()
}

htmlInput.addEventListener("input", updateResult)
cssInput.addEventListener("input", updateResult)

button.addEventListener("click", () => {
  if (isMobile) {
    alert("The download to Word function is designed for desktop use only.")
    return
  }
  htmlCssExportWord(htmlInput.value, cssInput.value, input.value)
})
window.onload = updateResult()

How it works

  1. Imports:
  • The "file-saver" module is imported to save files generated on the client-side.
  1. Function Declaration:
  • The function has three parameters:
    • html: HTML content to be exported to Word.
    • styles: Optional CSS styles to be applied to the exported content. Defaults to an empty string.
    • filename: Optional filename for the exported Word document. Defaults to "exported-document.doc".
  1. Generating MHTML:
  • The function prepares a representation of the content in MHTML format. MHTML is a way to package HTML content along with its resources like images and stylesheets into a single file.
  • Specifically, the function defines
    • a template for the top part of the MHTML file, including metadata and the HTML content.
    • a template for the bottom part of the MHTML file.
  • The function then aggregates these templates and the actual HTML content into a single string representing the entire MHTML file.
  1. Creating Blob:
  • The function then creates a Blob (Binary Large Object) containing the MHTML content. A Blob represents raw data and can be of any type.
  • The Blob constructor takes two parameters: an array containing the data, and an options object specifying the MIME type: in this case application/msword;charset=utf-8.
  1. Saving the File:
  • The function uses the imported saveAs function to save the Blob as a file with the specified filename.

Compatibility

  • I have yet to consider a systematic way on testing on different devices. Based on anecodtal reports.

  • Does not work on:

    • Mobile
    • Linux ubuntu 24.04 + Chrome
  • Works on:

    • Windows 11 + Chrome
    • Mac 14.4.1 + Chrome + Firefoox

Limitations

  • Microsoft Word cannot correct display many CSS features. In particular, neither flexbox nor grid is supported.
  • I have considered adding support by using polyfills (with flexibility for flex and css-grid-polyfill for grid): but have not decided whether to spend the time to do so.

To do

  • (Possible future modification, inspired by a conversation with Georgios Christou) add an optional 4th argument so that user can download data in different MIME types.

Contact