# nickfrandsen/htmltoword architecture

> Architecture diagram of the GitHub repository nickfrandsen/htmltoword, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nickfrandsen/htmltoword
- Repository: https://github.com/nickfrandsen/htmltoword

## Overview

#### Purpose and Entry Points
`htmltoword` is a Ruby gem that converts HTML into downloadable Microsoft Word `.docx` files. Its public standalone entry point is `Htmltoword::Document.create`; the gem loader and public module wiring live in `lib/htmltoword.rb`. Rails applications can use the registered controller renderer in `lib/htmltoword/action_controller.rb`.

#### Conversion Pipeline
The core document-generation path is implemented by `lib/htmltoword/document.rb`:
1. Accept HTML plus output filename and optional Word template.
2. Transform HTML into WordprocessingML using the bundled XSLT.
3. Unpack a `.docx` template archive.
4. Replace the template’s Word document XML with transformed content.
5. Repackage the archive as the generated `.docx`.

This creates a deliberate boundary between HTML-to-XML rendering and OOXML package assembly.

#### HTML-to-WordML Transformation
`lib/htmltoword/xslt/html_to_wordml.xslt` is the primary rendering definition. It maps standard HTML structure and inline formatting to WordML; it also supports gem-specific conventions such as highlight spans and page-break divs. `lib/htmltoword/xslt/style2.xslt` is an additional stylesheet asset in the transformation layer. XSLT is the main extensibility point for output semantics without changing Ruby orchestration.

#### DOCX Template and Styling Boundary
The bundled base package, `lib/htmltoword/templates/default.docx`, supplies the OOXML document structure and default Word styles. Generated WordML is inserted into this package rather than building every OOXML part from scratch. Consumers may select a custom template, allowing Word-native styles for normal text and headings to be controlled independently of application HTML.

#### Configuration
`lib/htmltoword/configuration.rb` centralizes configurable locations for default/custom templates and XSLT assets. The public configuration surface is exposed through `lib/htmltoword.rb`. This separates installed gem assets from application-provided templates, so deployments can customize document appearance and transformation resources without replacing the core generator.

#### Rails Integration
`lib/htmltoword/action_controller.rb` integrates with Action Controller’s rendering/responding flow. A `.docx.erb` view is rendered as HTML, then passed through the same document pipeline; callers may instead provide HTML directly. Rails is optional: the document API remains usable in standalone Ruby processes.

#### Runtime and Dependencies
The runtime is Ruby, packaged by `htmltoword.gemspec` and dependency-managed through `Gemfile`. The generator depends on filesystem/archive manipulation and XML/XSLT processing to construct OOXML `.docx` ZIP packages. Microsoft Word is not a runtime dependency; it is the target consumer of the produced OOXML files.

Main entry points: [`lib/htmltoword/document.rb`](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/document.rb) (HTML → WordML), [`lib/htmltoword/configuration.rb`](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/configuration.rb) (Configuration), [`lib/htmltoword.rb`](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword.rb) (Gem loader), [`lib/htmltoword/action_controller.rb`](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/action_controller.rb) (Action Controller renderer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Public APIs"]
  node_gem["Gem loader<br/>Ruby module<br/>[htmltoword.rb]"]
  node_document_api["Document.create<br/>document API<br/>[document.rb]"]
  node_configuration["Configuration<br/>[configuration.rb]"]
end

subgraph group_pipeline["DOCX Generation"]
  node_html_input["HTML input<br/>source content"]
  node_transform["HTML → WordML<br/>XSLT transform<br/>[document.rb]"]
  node_unpack["Unpack template<br/>archive stage<br/>[document.rb]"]
  node_replace_xml["Replace document XML<br/>OOXML assembly<br/>[document.rb]"]
  node_repackage["Repackage DOCX<br/>archive stage<br/>[document.rb]"]
  node_docx_output["Generated .docx<br/>output file"]
end

subgraph group_assets["Rendering Assets"]
  node_html_to_wordml["HTML-to-WordML XSLT<br/>primary stylesheet"]
  node_style_xslt["Style XSLT<br/>stylesheet asset<br/>[style2.xslt]"]
  node_default_template["Default DOCX template<br/>OOXML template<br/>[default.docx]"]
end

subgraph group_rails["Rails Integration"]
  node_rails_renderer["Action Controller renderer<br/>Rails renderer"]
  node_docx_erb[".docx.erb view<br/>Rails view"]
end

node_word_consumer["Microsoft Word<br/>target consumer"]
node_gem_package["Gem package<br/>Ruby packaging<br/>[htmltoword.gemspec]"]
node_dependencies["Runtime dependencies<br/>dependency manifest"]

node_gem -->|"exposes"| node_document_api
node_gem -->|"exposes"| node_configuration
node_configuration -->|"locates XSLT"| node_transform
node_configuration -->|"selects template"| node_unpack
node_document_api -->|"accepts"| node_html_input
node_html_input -->|"renders"| node_transform
node_html_to_wordml -->|"defines mapping"| node_transform
node_style_xslt -.->|"stylesheet asset"| node_transform
node_default_template -->|"base package"| node_unpack
node_transform -->|"WordML"| node_replace_xml
node_unpack -->|"unpacked parts"| node_replace_xml
node_replace_xml -->|"updated package"| node_repackage
node_repackage -->|"writes"| node_docx_output
node_docx_output -->|"opened by"| node_word_consumer
node_rails_renderer -->|"uses pipeline"| node_document_api
node_docx_erb -->|"renders as HTML"| node_rails_renderer
node_gem_package -.->|"dependency-managed by"| node_dependencies

click node_gem "https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword.rb"
click node_document_api "https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/document.rb"
click node_configuration "https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/configuration.rb"
click node_transform "https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/document.rb"
click node_unpack "https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/document.rb"
click node_replace_xml "https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/document.rb"
click node_repackage "https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/document.rb"
click node_html_to_wordml "https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/xslt/html_to_wordml.xslt"
click node_style_xslt "https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/xslt/style2.xslt"
click node_default_template "https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/templates/default.docx"
click node_rails_renderer "https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/action_controller.rb"
click node_gem_package "https://github.com/nickfrandsen/htmltoword/blob/master/htmltoword.gemspec"
click node_dependencies "https://github.com/nickfrandsen/htmltoword/blob/master/Gemfile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_gem,node_document_api,node_configuration toneBlue
class node_html_input,node_transform,node_unpack,node_replace_xml,node_repackage,node_docx_output toneAmber
class node_html_to_wordml,node_style_xslt,node_default_template toneMint
class node_rails_renderer,node_docx_erb toneRose
class node_word_consumer,node_gem_package,node_dependencies toneNeutral
```

## Components

### Public APIs

- [Gem loader](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword.rb): Ruby module · Exposes the public API and configuration surface. · `lib/htmltoword.rb`
- [Document.create](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/document.rb): document API · Standalone HTML-to-DOCX entry point. · `lib/htmltoword/document.rb`
- [Configuration](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/configuration.rb): asset configuration · Resolves template and XSLT locations. · `lib/htmltoword/configuration.rb`

### DOCX Generation

- **HTML input**: source content
- [HTML → WordML](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/document.rb): XSLT transform · Converts HTML structure and formatting into WordprocessingML. · `lib/htmltoword/document.rb`
- [Unpack template](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/document.rb): archive stage · `lib/htmltoword/document.rb`
- [Replace document XML](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/document.rb): OOXML assembly · Inserts generated WordML into the unpacked package. · `lib/htmltoword/document.rb`
- [Repackage DOCX](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/document.rb): archive stage · `lib/htmltoword/document.rb`
- **Generated .docx**: output file

### Rendering Assets

- [HTML-to-WordML XSLT](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/xslt/html_to_wordml.xslt): primary stylesheet · Defines HTML, highlight, and page-break rendering semantics. · `lib/htmltoword/xslt/html_to_wordml.xslt`
- [Style XSLT](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/xslt/style2.xslt): stylesheet asset · `lib/htmltoword/xslt/style2.xslt`
- [Default DOCX template](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/templates/default.docx): OOXML template · Supplies base package structure and Word-native styles. · `lib/htmltoword/templates/default.docx`

### Rails Integration

- [Action Controller renderer](https://github.com/nickfrandsen/htmltoword/blob/master/lib/htmltoword/action_controller.rb): Rails renderer · Renders .docx.erb views or accepts direct HTML. · `lib/htmltoword/action_controller.rb`
- **.docx.erb view**: Rails view · Rendered to HTML before document generation.

### Other components

- **Microsoft Word**: target consumer · Consumes the generated OOXML package; not a runtime dependency.
- [Gem package](https://github.com/nickfrandsen/htmltoword/blob/master/htmltoword.gemspec): Ruby packaging · Declares the Ruby gem and its dependencies. · `htmltoword.gemspec`
- [Runtime dependencies](https://github.com/nickfrandsen/htmltoword/blob/master/Gemfile): dependency manifest · Provides XML/XSLT and archive manipulation dependencies. · `Gemfile`

## Connections

- Gem loader → Document.create: exposes
- Gem loader → Configuration: exposes
- Configuration → HTML → WordML: locates XSLT
- Configuration → Unpack template: selects template
- Document.create → HTML input: accepts
- HTML input → HTML → WordML: renders
- HTML-to-WordML XSLT → HTML → WordML: defines mapping
- Style XSLT → HTML → WordML: stylesheet asset
- Default DOCX template → Unpack template: base package
- HTML → WordML → Replace document XML: WordML
- Unpack template → Replace document XML: unpacked parts
- Replace document XML → Repackage DOCX: updated package
- Repackage DOCX → Generated .docx: writes
- Generated .docx → Microsoft Word: opened by
- Action Controller renderer → Document.create: uses pipeline
- .docx.erb view → Action Controller renderer: renders as HTML
- Gem package → Runtime dependencies: dependency-managed by
