Skip to content

Integrations ko

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

Rails, Rack, 백그라운드 작업에서 docxi 사용하기

docxi에는 플러그인이나 내장 통합 기능이 없습니다. StringIO를 반환하는 평범한 Ruby이므로, 문자열을 보내거나 저장할 수 있는 곳이면 어디서든 동작합니다. 이 페이지에서는 일반적인 연결 방법을 소개합니다. Rails 컨트롤러 예제는 README에 나온 것과 같으며, 테스트 스위트는 Rails 5.2부터 8.1까지 로드한 상태로 실행됩니다.

Rails 컨트롤러: 파일을 브라우저로 보내기

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가 Content-Disposition: attachment를 설정하므로 브라우저가 파일을 내려받습니다. Bundler를 사용하면 docxi는 Gemfile에서 자동으로 로드됩니다.

Rails 백그라운드 작업: 요청 밖에서 파일 만들기

큰 문서나 느린 쿼리로 만드는 문서는 작업(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

작업은 지정한 경로에 파일을 씁니다. 그 뒤 파일을 저장소나 이메일 등 어디로 보낼지는 앱에서 정합니다.

Rack과 다른 프레임워크

Rack 애플리케이션이라면 바이트를 바로 반환할 수 있습니다.

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

config.ru에서 run ReportApp.new로 마운트합니다. 같은 방법은 Sinatra, Hanami를 비롯해 문자열을 응답 본문으로 보낼 수 있는 모든 프레임워크에서 동작합니다.

스크립트와 명령줄 도구

docxi는 서버도 오피스 소프트웨어도 필요 없으므로 평범한 Ruby 스크립트로도 충분합니다.

require "docxi"

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

관련 페이지


← 위키 홈 · README

Clone this wiki locally