Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For native Word .docx output, use Pandoc as the converter and call it from Ruby. If the input is a URL, retrieve and inspect its HTML first; fetching a page and converting HTML are separate steps. A Ruby wrapper such as pandoc-ruby can provide a Ruby interface, but the Pandoc executable must also be installed and available to the application.

Choose the right tool for the job

Pandoc is the clearest route in the documented options for converting HTML to native DOCX: its user guide lists both formats and describes a reference DOCX for controlling Word styles and document properties. Pandoc’s user guide

Approach Role and output Use it when Important caveat
Pandoc called from Ruby Converts HTML to DOCX You need native Word output and want control over document styles Perfect preservation of arbitrary browser layout or CSS is not established; validate your own HTML.
pandoc-ruby Ruby interface to Pandoc You prefer invoking Pandoc through a Ruby wrapper The Pandoc executable must be on PATH or configured explicitly. pandoc-ruby documentation
ruby-docx/docx Reads, edits, and saves existing DOCX files You need to inspect or modify a DOCX, including paragraphs, tables, headers, or footers It is not documented as an HTML-to-DOCX conversion engine. ruby-docx/docx project
Metanorma html2doc Creates legacy .doc from HTML A legacy Word format is acceptable It is not native DOCX; its README notes no SVG support and describes an additional Word-based save workflow to reach DOCX. html2doc README

Install Pandoc and prepare your HTML

Install the Pandoc application in the same environment where the Ruby process will run, then confirm that the executable is discoverable. Installing a Ruby gem wrapper alone does not install Pandoc. The wrapper documentation describes using the executable from PATH or specifying its path.

  1. Install Pandoc using the method appropriate for your operating system or deployment image. Follow the official installation instructions.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
    #1 Best Overall
  2. Check that the command works in the target environment: pandoc --version. If the shell cannot find it, correct PATH or configure the executable location before attempting conversion.

  3. Save the source as a UTF-8 HTML file, or fetch a URL and write the response body to a file after checking that it is the expected page rather than an error, login screen, or script-dependent shell.

URL retrieval is deliberately separate: a conversion engine accepts HTML, but it does not establish that a given URL is publicly accessible, that authentication will work, or that JavaScript-dependent content will be present in a raw HTTP response. Choose an HTTP client and parsing approach suitable for your application, and inspect the saved HTML before converting.

Convert a local HTML file from Ruby

The simplest dependable integration is to run Pandoc as a subprocess. This avoids relying on a wrapper API and makes the input and output paths explicit. The example assumes Pandoc is installed and callable as pandoc.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
require "open3"

input = "page.html"
output = "page.docx"

stdout, stderr, status = Open3.capture3(
  "pandoc", input,
  "--from=html",
  "--to=docx",
  "--output=#{output}"
)

unless status.success?
  warn "Pandoc failed (#{status.exitstatus}): #{stderr}"
  exit status.exitstatus || 1
end

puts "Created #{output}"

Passing arguments as an array to Open3.capture3 avoids shell-string interpolation of file names. Check the exit status rather than assuming that an output file means success. If a conversion fails, the captured standard error usually provides the immediate diagnostic.

Use a reference DOCX for Word styling

When the output needs specific Word styles or document properties, pass a reference DOCX with --reference-doc. Pandoc’s guide recommends starting with a reference document it generates and modifying that file, rather than designing an unrelated template and assuming every style mapping will match.

pandoc --print-default-data-file reference.docx > reference.docx

Then use the reference document during conversion:

require "open3"

stdout, stderr, status = Open3.capture3(
  "pandoc", "page.html",
  "--from=html",
  "--to=docx",
  "--reference-doc=reference.docx",
  "--output=page.docx"
)

abort(stderr) unless status.success?

Adjust the reference DOCX in Word or another compatible editor, then inspect the generated document in the Word viewer your recipients use. The reference-DOCX option controls document styles and properties; it does not make arbitrary web layouts identical to their browser rendering.

Fetch a URL, then convert the retrieved HTML

Keep network handling in the Ruby application and conversion in Pandoc. This compact example uses Ruby’s standard HTTP and URI libraries for an uncomplicated public URL. Production code should define its own redirect, timeout, authentication, and error policies.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
require "net/http"
require "uri"
require "open3"

uri = URI("https://example.com/")
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: 10, read_timeout: 30) do |http|
  http.get(uri.request_uri)
end

unless response.is_a?(Net::HTTPSuccess)
  abort "Fetch failed: HTTP #{response.code} #{response.message}"
end

File.binwrite("page.html", response.body)

stdout, stderr, status = Open3.capture3(
  "pandoc", "page.html",
  "--from=html",
  "--to=docx",
  "--output=page.docx"
)

abort "Conversion failed: #{stderr}" unless status.success?
puts "Created page.docx from #{uri}"

This example does not follow redirects or provide site-specific headers, cookies, or authentication. Add those only as required by the site and your application. A successful HTTP response is not proof that the body contains the complete content you see in a browser: inspect the HTML for the headings, text, tables, images, and links you intend to preserve.

Call Pandoc through the Ruby wrapper

If you want a wrapper interface, pandoc-ruby is a Ruby-facing option, but it still depends on the external Pandoc executable. Install and verify Pandoc first, and consult the wrapper’s current README for its supported API and options rather than assuming the gem alone performs conversion.

For deployment, test the exact command in the same container, server, or worker environment used by the application. A Pandoc executable available in an interactive shell may not be available to a service running with a different PATH.

What HTML content should you validate?

Conversion produces a Word document from the HTML input, not a pixel-identical copy of a browser viewport. Before relying on a workflow, use representative pages and compare the resulting DOCX in the intended Word viewer.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common failures

Symptom Likely cause What to do
pandoc: command not found or executable error Pandoc is not installed in the runtime environment or is missing from that process’s PATH. Install Pandoc in the deployment environment, verify pandoc --version there, or configure the wrapper with the executable path.
Wrapper gem is installed, but conversion cannot start The wrapper is present but its external Pandoc dependency is unavailable. Install and make the Pandoc executable callable; a gem installation is not a substitute.
DOCX is created but content is missing The retrieved HTML may be an error/login page, incomplete, or dependent on browser-side rendering. Inspect the saved HTML before conversion; address fetching, authorization, or rendering requirements separately.
Document formatting differs from the website Web layout and CSS are not guaranteed to map to Word formatting. Validate representative content in the target viewer and configure Word styles through a reference DOCX.
Output is .doc, not .docx The selected tool may be Metanorma html2doc, which generates the older format. Use Pandoc for the direct native DOCX route, or follow the documented Word-based save workflow if a legacy-tool chain is required.
Conversion command exits unsuccessfully Input path, unsupported or malformed source, output permissions, or other command-level error. Capture and inspect Pandoc’s standard error, verify the input file and destination directory, and rerun with a small representative HTML sample.

Or skip the browser setup

If your actual need is a clean visual capture of a URL rather than editable Word content, ScreenshotNeo is a website screenshot API and MCP server. It returns PNG, JPEG, WebP, or PDF; it does not convert HTML into an editable DOCX. A single request looks like this (see the ScreenshotNeo API documentation):

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo removes cookie and consent banners, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, with response headers indicating the page verdict and billing status. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents. The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Can I convert a URL directly to DOCX with Pandoc?

Treat fetching the URL as a separate step: retrieve and inspect the HTML, then pass that HTML to Pandoc.

Does ruby-docx convert HTML into DOCX?

Its documented role is reading and editing existing DOCX files, not HTML conversion.

Will the converted DOCX look exactly like the web page?

That is not established for arbitrary layouts or CSS; validate representative HTML in the Word viewer you intend to use.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.