Giter VIP home page Giter VIP logo

puredocx's Introduction

PureDocx

CircleCI Code Climate Test Coverage Issue Count

Puredocx allows you to create docx files and assign images, text and tables to it.

Installation

To install puredox gem type the following:

$ gem 'puredocx'

Document building

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:

  • text - prepares xml for text;
  • image - prepares xml for image. It's very important to add in_header: true into image options for image inside the header!
  • table - prepares xml for table;
  • brake - prepares xml for brake line;
  • new_page - prepares xml to move cursor to another page.

You can specify the footer position in paginate_pages option. It can be 'left', 'right' or 'center'.

Table preparation

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:

  • table_width - an integer number, that represents table width(maximum table width by default);
  • paddings - a hash of values({ top: 10, left: 20}), that represents table paddings(all values equal 0 by default);
  • sides_without_border - an array of symbols, that specify sides, that should be hidden;
  • bold_sides - an array of symbols, that specify sides, that should be bold;
  • col_width - an array of strings and nils. The maximum width of the table should be 10400 according to the document's width. The number elements inside the array must be the same, as the number of columns in the table. If you want to specify only one column's width, pass nil in place of other elements, and their width will be calculated automaticly.

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.

License

MIT

Free Software

About JetRuby

jetruby

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.

puredocx's People

Contributors

freevova avatar fuelen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.