How nickfrandsen/htmltoword works
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: document.rb HTML → WordML · configuration.rb Configuration · htmltoword.rb Gem loader · action_controller.rb Action Controller renderer
Components 17
Public APIs
- Gem loader · Exposes the public API and configuration surface.
lib/htmltoword.rb - Document.create · Standalone HTML-to-DOCX entry point.
lib/htmltoword/document.rb - Configuration · Resolves template and XSLT locations.
lib/htmltoword/configuration.rb
DOCX Generation
- HTML input
- HTML → WordML · Converts HTML structure and formatting into WordprocessingML.
lib/htmltoword/document.rb - Unpack template
lib/htmltoword/document.rb - Replace document XML · Inserts generated WordML into the unpacked package.
lib/htmltoword/document.rb - Repackage DOCX
lib/htmltoword/document.rb - Generated .docx
Rendering Assets
- HTML-to-WordML XSLT · Defines HTML, highlight, and page-break rendering semantics.
lib/htmltoword/xslt/html_to_wordml.xslt - Style XSLT
lib/htmltoword/xslt/style2.xslt - Default DOCX template · Supplies base package structure and Word-native styles.
lib/htmltoword/templates/default.docx
Rails Integration
- Action Controller renderer · Renders .docx.erb views or accepts direct HTML.
lib/htmltoword/action_controller.rb - .docx.erb view · Rendered to HTML before document generation.
Other components
- Microsoft Word · Consumes the generated OOXML package; not a runtime dependency.
- Gem package · Declares the Ruby gem and its dependencies.
htmltoword.gemspec - Runtime dependencies · Provides XML/XSLT and archive manipulation dependencies.
Gemfile
Connections 17
- 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