Ruby library for generating pretty docx files.
Puredocx allows you to create docx files and assign images, text and tables to it.
To install puredox gem type the following:
$ gem 'puredocx'
You can create 'example.docx' by creating PureDocx object:
PureDocx.create('../example.docx', paginate_pages: 'right') do |doc|
doc.header([
doc.text('header', style: [:italic], size: 28, align: 'left'),
doc.image('../images/logo.jpg', width: 100, in_header: true)
])
doc.content([
doc.text('text', style: [:bold], size: 32, align: 'center'),
doc.image('../images/logo.jpg', width: 100),
doc.table(table, table_options)
])
end
doc.header method will generate header for the docx document and doc.content - its content. If you want to pass more than one image with text in the content or in the header, assign them in the correct sequence inside the array. There are 4 types of methods to prepare xml:
You can specify the footer position in paginate_pages option. It can be 'left', 'right' or 'center'.
If you want to insert table into the document you shold prepare table data as shown below. This mast be an array of rows. Each row contains column object with some parameters. Every columns element will start from a new line.
You should create an object table_options also, to add some styles to the table. This object accepts 3 keys:
If you want to insert table2 with options into another table, you should prepare data for this table and add it to the right column. It is important to set width for nested table(table_width parameter). Look at the following example:
table = [
[
{ column: [doc.text('first column, first row')] },
{
column: [
doc.text('second column a, first row'),
doc.text('second column b, first row'),
doc.text('second column c, first row')
]
},
{ column: [doc.image('../image.jpg', width: 500)] }
],
[
{
column: [
doc.table(table2, table2_options),
doc.brake
]
},
{ column: [doc.text('second column, second row')] },
{ column: [doc.text('third column, second row')] }
]
]
table_options = {
table_width: 4000,
sides_without_border: [:left, :top, :right, :insideH, :insideV],
bold_sides: [:bottom],
col_width: [nil, 2852, nil]
}
This example shows how to create the table data and it's options.
You can see generated docx file example here example.docx
Puredocx gem works fine with Microsoft Word 2016 and Libre Office.
MIT
Free Software
puredocx is maintained and founded by JetRuby Agency, inc.
We love open source software! See our projects or hire us to design, develop, and grow your product.