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 🙏

© 2025 – Pkg Stats / Ryan Hefner

file-to-html-converter

v1.0.0

Published

Convert DOCX and PDF files to clean semantic HTML

Readme

file2html

Convert DOCX and PDF files to clean semantic HTML.

Features

  • DOCX Support: Convert Microsoft Word documents to semantic HTML
  • PDF Support: Convert PDF files to semantic HTML
  • Clean Output: Generates semantic HTML with proper tags like <h1>, <h2>, <p>, <ul>, <li>, <strong>, <em>, <img>
  • No Inline Styles: Output is clean HTML without inline styles or absolute positioning
  • TypeScript Support: Full TypeScript definitions included

Installation

npm install file2html

Usage

DOCX to HTML

import { docxToHtml } from 'file2html';

const html = await docxToHtml('document.docx');
console.log(html);

PDF to HTML

import { pdfToHtml } from 'file2html';

const html = await pdfToHtml('document.pdf');
console.log(html);

Complete Example

import { docxToHtml, pdfToHtml } from 'file2html';

async function convertFiles() {
  try {
    // Convert DOCX file
    const docxHtml = await docxToHtml('sample.docx');
    console.log('DOCX HTML:', docxHtml);
    
    // Convert PDF file
    const pdfHtml = await pdfToHtml('sample.pdf');
    console.log('PDF HTML:', pdfHtml);
  } catch (error) {
    console.error('Conversion failed:', error.message);
  }
}

convertFiles();

API Reference

docxToHtml(filePath: string): Promise<string>

Converts a DOCX file to semantic HTML.

Parameters:

  • filePath (string): Path to the DOCX file

Returns:

  • Promise<string>: Clean semantic HTML

Features:

  • Converts paragraphs to <p> tags
  • Maps bold text to <strong> tags
  • Maps italic text to <em> tags
  • Converts tables to <table>, <tr>, <td> structure
  • Detects headings based on paragraph styles and converts to <h1>, <h2>, etc.
  • Extracts images and converts to <img> tags with base64 data URLs

pdfToHtml(filePath: string): Promise<string>

Converts a PDF file to semantic HTML.

Parameters:

  • filePath (string): Path to the PDF file

Returns:

  • Promise<string>: Clean semantic HTML

Features:

  • Groups text into paragraphs
  • Detects headings based on text patterns and formatting
  • Converts to semantic HTML structure

Output Format

The library generates clean, semantic HTML without inline styles:

<h1>Document Title</h1>
<p>This is a paragraph with <strong>bold text</strong> and <em>italic text</em>.</p>
<h2>Section Heading</h2>
<ul>
  <li>List item 1</li>
  <li>List item 2</li>
</ul>
<table>
  <tr>
    <td>Cell 1</td>
    <td>Cell 2</td>
  </tr>
</table>
<img src="data:image/png;base64,..." alt="">

Error Handling

Both functions throw errors for:

  • Non-existent files
  • Invalid file formats
  • Corrupted files
  • Permission issues
try {
  const html = await docxToHtml('invalid-file.docx');
} catch (error) {
  console.error('Conversion failed:', error.message);
}

Development

Building

npm run build

Testing

npm test

Running Tests in Watch Mode

npm run test:watch

Dependencies

  • adm-zip: For extracting DOCX files
  • fast-xml-parser: For parsing WordprocessingML XML
  • pdf-parse: For extracting text from PDF files

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.