Skip to content

Integrations hi

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 में कोई प्लगइन या बिल्ट-इन इंटीग्रेशन नहीं है। यह साधारण Ruby है जो एक StringIO लौटाती है, इसलिए यह हर उस जगह काम करता है जहां आप कोई स्ट्रिंग भेज या स्टोर कर सकते हैं। यह पेज इसे जोड़ने के आम तरीके दिखाता है। Rails controller वाला तरीका वही है जो README में दिखाया गया है, और टेस्ट सूट Rails 5.2 से 8.1 लोड करके चलता है।

Rails controller: फ़ाइल ब्राउज़र को भेजें

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 बैकग्राउंड जॉब: रिक्वेस्ट के बाहर फ़ाइल बनाएं

बड़े डॉक्यूमेंट, या धीमी क्वेरी से बनने वाले डॉक्यूमेंट, जॉब में बनाना बेहतर है:

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