Repository navigation
Integrations fr
English · 简体中文 · 繁體中文 · Español · العربية · Français · Português (Brasil) · Deutsch · हिन्दी · 日本語 · 한국어 · Русский · Bahasa Indonesia · Türkçe · Italiano · Tiếng Việt
docxi n'a ni plugins ni intégrations intégrées. C'est du Ruby pur qui renvoie un StringIO : il fonctionne partout où vous pouvez envoyer ou stocker une chaîne. Cette page montre les façons habituelles de le brancher. Le contrôleur Rails est celui que montre le README, et la suite de tests s'exécute avec Rails 5.2 à 8.1 chargé.
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 définit Content-Disposition: attachment, si bien que le navigateur télécharge le fichier. Avec Bundler, docxi est chargé automatiquement depuis le Gemfile.
Les gros documents, ou ceux construits à partir de requêtes lentes, sont mieux générés dans une tâche :
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
endLa tâche écrit dans le chemin de votre choix. La suite, par exemple le stockage ou l'envoi par e-mail, dépend de votre application.
Toute application Rack peut renvoyer directement les octets :
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
endMontez-la avec run ReportApp.new dans config.ru. La même approche fonctionne avec Sinatra, Hanami ou tout framework capable d'envoyer une chaîne comme corps de réponse.
docxi n'a besoin ni de serveur ni de logiciel de bureautique : un simple script Ruby suffit.
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