Repository navigation
Integrations ru
English · 简体中文 · 繁體中文 · Español · العربية · Français · Português (Brasil) · Deutsch · हिन्दी · 日本語 · 한국어 · Русский · Bahasa Indonesia · Türkçe · Italiano · Tiếng Việt
У docxi нет плагинов и встроенных интеграций. Это обычный Ruby, который возвращает StringIO, поэтому он работает везде, где можно отправить или сохранить строку. На этой странице показаны обычные способы подключения. Вариант с контроллером Rails совпадает с примером из README, а набор тестов запускается с загруженными Rails от 5.2 до 8.1.
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
endsend_data устанавливает Content-Disposition: attachment, поэтому браузер скачивает файл. С Bundler docxi загружается из Gemfile автоматически.
Большие документы или документы, которые строятся по медленным запросам, лучше формировать в задаче:
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 может вернуть байты напрямую:
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Подключите его через run ReportApp.new в config.ru. Тот же подход работает с 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)docxi wiki · README · Installation · Troubleshooting · Report an issue · MIT License
Start
Build documents
Help
Project
Links
Languages