Skip to content

Latest commit

History

39 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Docxi gem provides ability to create docx documents using Ruby.

Features

  1. Paragraphs
  2. Text formatting
  3. Header and Footer (limited)
  4. Tables (limited)
  5. Images
  6. Table of Content
  7. Page Numbering

At the moment, the gem supports a limited number of functions, but the development of the gem is still in progress, and perhaps functionality will be expanded in the future. First of all, try to add documentation for the code and tests.

NOTE

Use Latest version of Gem. Old Version has bugs.

Usage

# default attributes (top: 720,right: 1440,left: 1440,bottom: 720,footer: 200, header: 190)word=Docxi::Document.new# or change margins# word = Docxi::Document.new(top: 0,right: 0,left: 0,bottom: 0)document=word.document# Creates Headerheader=Docxi::Word::Headers::Header.newdo |h|
# Inserts image into headerheader_logo=document.add_mediaFile.open('logo.png')h.imageheader_logo,align: 'right',width: 100,height: 100end# Adds headerdocument.add_header(header)# Creates Footerfooter=Docxi::Word::Footers::Footer.newdo |f|
# Adds footer textf.text"Footer Text",size: 8,align: 'center'# Adds page numbersf.page_numbersend# Adds footerdocument.add_footer(footer)# Creates paragraphdocument.p(size: 12,bold: true,align: "center",color: "000000",fill: "AAAAAA")do |p|
p.text"Title",size: 12,bold: true,align: "center",color: "000000"# Adds 3 break linesp.brtimes: 3enddocument.pdo |p|
# Adds text with specific stylesp.text"Subtitle",size: 14,color: '568AB2',bold: trueend# Adds break pagedocument.br(page: true)# Adds TOCdocument.text"Table of Content",size: 18,color: 'D36C8A',bold: truedocument.table_of_content(size: 14,bold: true)do |table|
# Adds TOC elementtable.add_row("Table",1)end# Adds break pagedocument.br(page: true)# Adds tabledocument.table(columns_width: [316,160,135])do |table|
# Adds row with specific stylestable.tr(fill: "FBD4B4",bold: true,align: 'center')do |tr|
# Adds cellstr.tc(borders: {top: 1,right: nil,bottom: 1,left: 1})do |tc|
tc.text"Header 1"endtr.tc(borders: {top: 1,right: nil,bottom: 1,left: nil})do |tc|
tc.text"Header 2"endtr.tc(borders: {top: 1,right: 1,bottom: 1,left: nil})do |tc|
tc.text"Header 3"endend# Adds another rowtable.tr(align: 'center')do |tr|
# Adds rowspantr.tc(rowspan: true,borders: {top: 1,right: 1,bottom: 1,left: 1},valign: 'center')do |tc|
tc.text"Rowspan",align: 'left'endtr.tc(borders: {top: 1,right: 1,bottom: 1,left: 1})do |tc|
tc.text"Cell Value 2"endtr.tc(borders: {top: 1,right: 1,bottom: 1,left: 1})do |tc|
tc.text"Cell Value 3"endendtable.tr(fill: "D9D9D9",bold: true,align: 'center')do |tr|
tr.tc(borders: {top: 1,right: 1,bottom: 1,left: 1},merged: true)# Use with rowspan to mark cell as merged# Adds colspantr.tc(colspan: 2,valign: 'bottom',borders: {top: 1,right: 1,bottom: 1,left: 1})do |tc|
tc.text"Colspan"endendend# Adds table with row height and column indentationdocument.table(columns_width: [316,160,135],height: 268,iwidth: 108)do |table|
# Adds row with specific stylestable.tr(fill: "FBD4B4",bold: true,align: 'center')do |tr|
# Adds cellstr.tc(borders: {top: 1,right: nil,bottom: 1,left: 1})do |tc|
tc.text"Header 1"endtr.tc(borders: {top: 1,right: nil,bottom: 1,left: nil})do |tc|
tc.text"Header 2"endtr.tc(borders: {top: 1,right: 1,bottom: 1,left: nil})do |tc|
tc.text"Header 3"endendend# Adds imagedocument.pdo |p|
p.text"Image Example",size: 16,bold: true,italic: true,underline: true,br: true# Adds image to containermedia=document.add_mediaFile.open('image.png')# Or add image URL# media = document.add_media image_url # Insert media into paragraphp.imagemedia,style: {width: '200pt',height: '200pt'}# Add Hyperlinkhyper=document.add_hyperlink("http://google.com")p.hyperlink"Go to Google","http://google.com",hyperend# Add Underline document.p(bottom: true)do |p|
p.text"Example text"end# Set position of Paragraphdocument.p(fill: "d7d7d7",align: "center",right: -1000,left: -1000)do |p| p.text"Example Text 2"p.brend# Creates Word Documentio=word.renderFile.open('doc_1.docx','wb'){|f| f.write(io.string)}

About

Help to generate formatted docx template.

Resources

Stars

2 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages