Skip to content

Integrations ar

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

استخدام docxi مع Rails وRack والمهام في الخلفية

ليس لمكتبة docxi إضافات ولا تكاملات مدمجة. إنها شيفرة Ruby عادية تعيد كائن StringIO، لذا تعمل في أي مكان يمكنك فيه إرسال سلسلة نصية أو تخزينها. تعرض هذه الصفحة الطرق المعتادة لربطها. نمط وحدة التحكم في 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: بناء الملف خارج الطلب

من الأفضل إنشاء المستندات الكبيرة، أو المستندات المبنية من استعلامات بطيئة، في مهمة في الخلفية:

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

شغّله باستخدام 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)

صفحات ذات صلة


→ الصفحة الرئيسية للويكي · README

Clone this wiki locally