• Stars
    star
    119
  • Rank 297,930 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Find and replace variables inside a Microsoft Word document (.docx) template

Docx Replace

This gem allows you to generate .docx files in your rails or ruby app by embedding variables inside of a .docx template. This is purposefully meant to be simple and feature-light.

Installation

Add this line to your application's Gemfile:

gem 'docx_replace'

And then execute:

$ bundle

Or install it yourself as:

$ gem install docx_replace

Usage

Inside of a rails controller, your code might look something like this (although I would recommend extracting most of this into a separate class):

def user_report
  @user = User.find(params[:user_id])

  respond_to do |format|
    format.docx do
      # Initialize DocxReplace with your template
      doc = DocxReplace::Doc.new("#{Rails.root}/lib/docx_templates/my_template.docx", "#{Rails.root}/tmp")

      # Replace some variables. $var$ convention is used here, but not required.
      doc.replace("FIRSTNAME", @user.first_name)
      doc.replace("LASTNAME", @user.last_name)
      doc.replace("USERBIO", @user.bio)

      # Replace multiple occurrences
      doc.replace("BIRTHDATE", @user.birth_date, true)

      # Write the document back to a temporary file
      tmp_file = Tempfile.new('word_template', "#{Rails.root}/tmp")
      doc.commit(tmp_file.path)

      # Respond to the request by sending the temp file
      send_file tmp_file.path, filename: "user_#{@user.id}_report.docx", disposition: 'attachment'
    end
  end
end

Note: Word sometimes wraps characters in XML tags, causing the replacement to not work. I recommend not using any special characters in your variable names.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Credits

Much of this code is based on an older gem called docxedit. This has a few more features, but is very sensitive to the formatting of the .docx template.

More Repositories

1

ngQuickDate

An Angular.js Date/Time picker directive that stresses speed of data entry and configuration
CoffeeScript
362
star
2

ngOnboarding

A tooltip-tutorial / onboarding framework for Angular.js
CoffeeScript
151
star
3

ngModal

Very basic modal dialog directive for Angular.js
CoffeeScript
115
star
4

angular-starter-kit

An opinionated starter template for crafting single-page web applications using Angular.js, Coffeescript, SCSS, Jade, Bootstrap, UI-Router, Font-Awesome and a few other carefully picked tools.
CoffeeScript
38
star
5

hacker-news-for-chrome

A simple chrome extension for displaying the latest from Hacker News
JavaScript
35
star
6

angular-date-picker-polyfill

HTML5 Date Input Polyfill for AngularJS
CoffeeScript
19
star
7

angular-currency-mask

Angular directive for adding commas and proper formatting to a currency input text field.
JavaScript
17
star
8

MarkerClusterer

This is a fork of Xiaoxi Wu's MarkerClusterer library for Google Maps V3
JavaScript
8
star
9

angular-pdf-viewer

An angular.js directive that displays PDF documents using Mozilla's PDF.js library.
CoffeeScript
7
star
10

angular-lib-template

Starter template for creating an Angular.js library, such as a directive or service. Includes auto-compilation for Coffeescript and Less as well as testing support.
CoffeeScript
5
star
11

pandora_plus

A Google Chrome extension for improving the user experience of Pandora.com.
JavaScript
4
star
12

can_i_get_a_receipt_with_that

This is a contestant for the Google-sponsored Data Viz Challenge, in which we visualize how our individual federal income taxes are spent.
JavaScript
3
star
13

mobile_fixed_header_test

1
star
14

aa_rails_template

This is my very basic starter template for all new Rails projects
Ruby
1
star
15

ngTreeNav

Vertical tree-structed nav menu directive for Angular.js
JavaScript
1
star
16

capybara-page-object-helper

Helper module for creating page objects using Capybara and Rspec
Ruby
1
star
17

DotVim

My .vim folder, including my vimrc and gvimrc files
Vim Script
1
star
18

capybara-request-spec-helpers

A number of useful methods for nicer-looking integration/request specs using Rspec and Capybara
Ruby
1
star
19

vim_config_for_windows

My vim configuration on Windows
Vim Script
1
star