Skip to content

Integrations

Hammad Maqbool edited this page Oct 29, 2026 · 1 revision

English · 简体中文 · 繁體中文 · Español · العربية · Français · Português (Brasil) · Deutsch · हिन्दी · 日本語 · 한국어 · Русский · Bahasa Indonesia · Türkçe · Italiano · Tiếng Việt

Using docxi with Rails, Rack and background jobs

docxi has no plugins and no built-in integrations. It's plain Ruby that returns a StringIO, so it works anywhere you can send or store a string. This page shows the usual ways to connect it. The Rails controller pattern is the one the README shows, and the test suite runs with Rails 5.2 to 8.1 loaded.

Rails controller: send the file to the browser

class ReportsController < ApplicationController
  def show
    word = Docxi::Document.new
    word.document.p { |p| p.text "Report for #{Date.today}" }

    send_data word.render.string,
              filename: "report.docx",
              type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
  end
end

send_data sets Content-Disposition: attachment, so the browser downloads the file. With Bundler, docxi is loaded automatically from the Gemfile.

Rails background job: build the file outside the request

Large documents, or documents built from slow queries, are better made in a job:

class MonthlyReportJob < ApplicationJob
  queue_as :default

  def perform(path)
    word = Docxi::Document.new
    word.document.p { |p| p.text "Monthly report", bold: true, size: 16 }

    File.binwrite(path, word.render.string)
  end
end

The job writes to a path you choose. Where the file goes next, such as storage or email, is up to your app.

Rack and other frameworks

Any Rack application can return the bytes directly:

require "docxi"

class ReportApp
  def call(env)
    word = Docxi::Document.new
    word.document.p { |p| p.text "Generated by a Rack app" }

    headers = {
      "content-type" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
      "content-disposition" => 'attachment; filename="report.docx"'
    }
    [200, headers, [word.render.string]]
  end
end

Mount it with run ReportApp.new in config.ru. The same approach works for Sinatra, Hanami or any framework that can send a string as the response body.

Scripts and command-line tools

docxi needs no server and no office software, so a plain Ruby script works:

require "docxi"

word = Docxi::Document.new
word.document.p { |p| p.text "Nightly export" }
File.binwrite(ARGV.fetch(0, "export.docx"), word.render.string)

Related pages


← Wiki home · README

Clone this wiki locally