Free Ruby API for Word DOCX Creation & Processing
Open Source Ruby Library that gives Programmers the Capability to Make Professional Office Word Documents, Modify DOCX Files, add Page Margins, Insert Nested Tables using HTML-like Syntax.
What is Caracal?
Caracal library gives Ruby developers the capability to generate professional Office Word documents inside their own applications using HTML-like syntax. The library makes Developer's jobs easy by enabling them to use some simple commands to create and manage Office Open XML (OOXML). The library is open source and is available under the MIT license.
The library is very well designed and it separates the instruction for document parsing from the instructions of processing. This strategy gives the rendering process a great amount of flexibility. The library has incorporated several important features related to word document processing such as word documents creation, setting page size, page margins support, page break, page numbers, fonts support, styles and formatting support, adding a paragraph, using links and bookmarks, inserting tables, nested tables support, images support and many more.
Getting Started with Caracal
Please add the following command application's Gemfile for a smooth use of the library.
Install Caracal by adding code to Application's Gemfile
gem 'caracal'
Install Caracal by adding code to Application's Gemfile
bundle install
Word Docx Files Creation using Ruby
The open source Caracal library enables Ruby developers to generate DOCX files inside their apps using Ruby code. The DOCX file format is a zipped collection of XML documents and is created using the OOXML standard. The library helps programmers by creating the entire structure of the document and zipped it the output documents. The library supports modifying the existing files, adjusting page size, setting margins, applying styles, and much more.
How to Create Word Documents via Ruby Caracal Library?
Caracal::Document.save 'example.docx' do |docx|
# page 1
docx.h1 'Page 1 Header'
docx.hr
docx.p
docx.h2 'Section 1'
docx.p 'Lorem ipsum dolor....'
docx.p
docx.table @my_data, border_size: 4 do
cell_style rows[0], background: 'cccccc', bold: true
end
# page 2
docx.page
docx.h1 'Page 2 Header'
docx.hr
docx.p
docx.h2 'Section 2'
docx.p 'Lorem ipsum dolor....'
docx.ul do
li 'Item 1'
li 'Item 2'
end
docx.p
docx.img 'https://www.example.com/logo.png', width: 500, height: 300
end
Add Tables and Nested Tables in Word Files
The free Caracal library enables software developers to add and modify tables to their word document with just couple of lines of Ruby code. The library has included several important functions related tables management such as add title, add new columns and rows, styling the top, bottom or left & right, apply style to a specific cell, merge or split cells and many more. The library also included support for nested tables.
Use Lists inside Word Documents
The open source Caracal library has included support for using lists inside DOCX files using Ruby commands. The library allows adding ordered as well as unordered lists inside word documents. It also supports deep nested lists and mixing in other combination. You can easily define styles for the lists. The library supports 9 levels of default styles for both ordered and unordered lists.
How to Use Lists inside Word Documents via Ruby Library?
docx.ol do
li 'First item'
li do
text 'Second item with a '
link 'link', 'http://www.google.com'
text '.'
br
text 'This sentence follows a line break.'
end
end
Insert Paragraph and Bookmarks to DOCX File
The Caracal library has provided functionality for adding paragraph to their word documents with ease. The library has included several function related to paragraphs handling, such as text alignment, fonts selection, defining font color and font size setting background color, apply vertical alignment and so on. It also provides support for inserting bookmarks directly to the document or inside paragraph blocks with ease.
How to Add Bookmarks to Word Documents via Ruby Library ?
# document-level bookmark
dox.bookmark_start id: 's1', name: 'section1'
docx.h2 'Section Heading'
docx.bookmark_end id: 's1'
docx.p 'Section content.'
# pargraph-level bookmark
docx.h2 'Section Heading'
docx.p do
text 'Pretend this paragraph has a lot of text and we want to bookmark '
bookmark_start id: 'p1', name: 'phrase1'
text 'a single phrase'
bookmark_end id: 'p1'
text ' inside the larger block.'
end