Caracal-Rails
Caracal is a ruby library for dynamically creating professional-quality Microsoft Word documents.
Caracal-Rails is a drop in solution for registering the Microsoft Word mime type and for establishing a template handler in Rails for the :docx format. All caracal documents are rendered with an explicit block passing a reference to the Caracal::Document object named docx.
Please see the caracal-example repository for a working demonstration of the library's capabilities.
Installation
Add this line to your application's Gemfile:
gem 'caracal-rails'
Then execute:
$ bundle install
Usage
Design your controller normally and respond to the docx format:
class ReportsController < ApplicationController
def show
# collect your models/view models here
respond_to do |format|
format.docx { headers["Content-Disposition"] = "attachment; filename=\"caracal.docx\"" }
end
end
end
Then add a view file with the Caracal extension:
apps/views/reports/show.docx.caracal
Specify the format docx in your route:
resources :reports, only: [:show], format: 'docx'
Inside your view, simply issue Caracal commands on the document object:
docx.font name: 'Droid Serif'
docx.style do
id 'special'
name 'Special'
font 'Droid Serif'
italic true
size 16
end
docx.h1 'Quarterly Report'
docx.hr
@report.clients.each do |client|
docx.h2 client.name
docx.p 'Lorem ipsum dolor...'
docx.p do
text 'This is a sentence with a '
link 'link', 'http://www.google.com/', style: 'special'
text ' in the middle.'
end
docx.img image_url('https://www.example.com/logo.png'), width: 300, height: 200, align: 'right'
docx.table client.tablular_data, border_size: 4 do
cell_style rows[0], background: '3366cc', color: 'ffffff', bold: true
end
docx.page
docx.p 'This is an bulleted list.'
docx.ul do
li do
text 'Item 1', bold: true
text ' More text'
end
li 'Item 2'
end
end
See the Caracal library for more details.
Contributing
- Fork it ( https://github.com/urvin-compliance/caracal-rails/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Why is It Called Caracal?
Because my son likes caracals. :)
Inspiration
A tip of the hat to the wonderful PDF generation libraries Prawn and Prawnto.
License
Copyright (c) 2014 Urvin LLC