Create Dynamic Word Files from Template via Free Ruby API
Leading Open Source Ruby Library Designed to Create Dynamic Microsoft Word (.docx) Documents From Pre-designed Templates.
What is Ruby-Docx-Templater?
Generating dynamic Microsoft Word documents (.docx) from a Ruby application can be a surprisingly complex task. You might need to generate reports, invoices, or letters that non-technical users have designed. While many solutions exist, they often require heavy dependencies like LibreOffice or local Word installations. Enter Ruby-Docx-Templater, a lightweight gem that takes a different approach. It allows users to use a standard .docx file as your template, manipulating it entirely in memory to inject their data. This makes it a fast, secure, and deployment-friendly option for any Ruby developer.
At its core the Ruby-Docx-Templater library is a templating engine specifically for .docx files. It is a lightweight, powerful Ruby gem designed to create dynamic Microsoft Word (.docx) documents from pre-designed templates. It works by using a simple tag-based system within a standard Word document. You create a template in Word, place special tags where you want dynamic content to appear, and then let the gem replace those tags with actual data from your Ruby application. Its memory-based processing, support for complex table structures, and preservation of formatting make it ideal for business document automation.
Getting Started with Ruby-Docx-Templater
The recommend way to install Ruby-Docx-Templater is using RubyGems. Please use the following command for a smooth installation.
Install Ruby-Docx-Templater via RubyGems
gem install ruby-docx-templater Install Ruby-Docx-Templater via GitHub
git clone https://github.com/jawspeak/ruby-docx-templater.git You can also download it directly from GitHub.Create Word Docx File from Template via Ruby
The most basic feature of Ruby-Docx-Templater is simple text replacement. You can place placeholders anywhere in your Word document, and the library will replace them with actual values. The beauty of this approach is that formatting is preserved. If you make $COMPANY_NAME$ bold and red in your template, it will remain bold and red in the output. This gives designers complete control over document appearance. In your Word template, you define keys using the $KEY$ syntax. The dollar signs act as delimiters, making it clear where substitutions should occur.
Create a Word Docx File using Template File via Ruby?
require 'docx_templater'
# Load your template file
doc = DocxTemplater::TemplateProcessor.new('invoice_template.docx')
# Define your data as a hash
data = {
'COMPANY_NAME' => 'Acme Corporation',
'INVOICE_NUMBER' => 'INV-2024-001',
'INVOICE_DATE' => '2024-11-04',
'CLIENT_NAME' => 'John Smith',
'TOTAL_AMOUNT' => '$1,250.00'
}
# Render the document with your data
doc.render(data)
# Save the output
doc.save('output_invoice.docx')
Multi-Row Table Loops Support
One of the most powerful features is the ability to generate dynamic tables with multiple rows. This is essential for creating itemized lists in invoices, product catalogs, or any report with repeating data. You define a loop region in your table and the library will duplicate everything between these markers for each item in your data array. You can format each cell individually in Word. For instance, you might right-align numbers, make headers bold, or apply specific fonts. All formatting will be preserved in the generated rows.
How to Create a Product Report with Multiple Rows in Docx Formats via Ruby Library?
require 'docx_templater'
doc = DocxTemplater::TemplateProcessor.new('product_report.docx')
# Define items as an array of hashes
data = {
'REPORT_TITLE' => 'Monthly Sales Report',
'REPORT_DATE' => 'November 2024',
'ITEMS_LIST' => [
{
'PRODUCT_NAME' => 'Laptop Pro 15"',
'QUANTITY' => '5',
'UNIT_PRICE' => '$1,200.00',
'TOTAL' => '$6,000.00'
},
{
'PRODUCT_NAME' => 'Wireless Mouse',
'QUANTITY' => '12',
'UNIT_PRICE' => '$25.00',
'TOTAL' => '$300.00'
},
{
'PRODUCT_NAME' => 'USB-C Cable',
'QUANTITY' => '20',
'UNIT_PRICE' => '$15.00',
'TOTAL' => '$300.00'
}
]
}
doc.render(data)
doc.save('output_sales_report.docx')
Memory-Based Processing
The open source Ruby-Docx-Templater library is very easy to handle and support memory-based processing inside Ruby applications. All document manipulation happens in memory, which means your sensitive data never touches the file system during the templating process. This is crucial for applications dealing with confidential information like medical records, financial documents, or personal data.
Working with Word XML
One unique aspect of Ruby-Docx-Templater is that you sometimes need to manually edit the underlying XML of your Word document. Word often inserts formatting markup that can split your template keys, breaking the substitution process. When you type $CUSTOMER_NAME$ in Word, it might appear correct on screen, but the underlying XML could look like this.