Pdf_gen¶ ↑
pdf_gen is a high-level wrapper around PDF::Writer library which allows to create PDF documents in an easy way with a layout similar to HTML.
Install¶ ↑
gem install pdf_gen
Usage¶ ↑
All package entities are located within the PDFGen module. PDF file generation can be made in the following way:
require "pdf_gen" PDFGen::document PDF::Writer.new, 0.cm do # pdf generation code here end.save("#{File.basename(__FILE__, ".rb")}.pdf")
Construction Elements¶ ↑
Caption¶ ↑
An object which allows to represent some parts of the text in PDF file. This object has such properties as width, justification, pad_top (indent from the left side of the page), pad_bottom, pad_left, pad_right, text_color, font_size and border. The Border, in its turn, can be characterized by border_color, border_style (solid or dotted) and border_width (in cm or pixels). Support Properties:
pad_top - integer pad_bottom - integer pad_left - integer pad_right - integer paddings - integer border - true|false border_top - true|false border_bottom - true|false border_left - true|false border_right - true|false border_style - :solid | :dotted border_widht - integer border_color - object of color-tools*. Color::RGB::Red|Color::RGB.from_html("#ccc")| other background_color - object of color-tools*. Color::RGB::Red|Color::RGB.from_html("#ccc")| other width - integer height - integer *color-tools http://ruby-pdf.rubyforge.org/color-tools/
Image¶ ↑
An object which allows to add images with filename extensions *.jpg and *.png in PDF file. This object has such properties as width, pad_top, pad_bottom, pad_left, pad_right and border.
require "pdf_gen" PDFGen::document PDF::Writer.new, 2.cm do image_data = open(File.expand_path('ruby_logo.jpg'), "rb") { |file| file.read } image image_data, :width => 250 end.save("#{File.basename(__FILE__, ".rb")}.pdf")
Support Properties:
*the same as caption`s properties
Span¶ ↑
An object which allows setting several objects across the page. This object has such properties as width, pad_top, pad_bottom, pad_left, pad_right, border and background_color.
PDFGen::document PDF::Writer.new, 2.cm do span :width => 10.cm, :pad_left => 2.cm do caption ' first '*50, :width => 4.cm caption ' second '*50, :width => 4.cm elements :border => true #group set params end end.save("#{File.basename(__FILE__, ".rb")}.pdf")
Support Properties:
*the same as caption`s properties vertical_interval - integer vartical_align - true | false
Div¶ ↑
An object which allows setting several elements in the vertical format. This object has such properties as width, horizontal_interval, pad_top, pad_bottom, pad_left, pad_right, border and background_color. Div element can be divided across pages automatically.
PDFGen::document PDF::Writer.new, 2.cm do div :width => 10.cm do caption ' first '*50, :width => 4.cm caption ' second '*50, :width => 4.cm elements :border => true #group set params end end.save("#{File.basename(__FILE__, ".rb")}.pdf")
Support Properties:
*the same as caption`s properties horizontal_interval - integer horizontal_align - true | false
Table¶ ↑
An object which allows creating tables in PDF file. This object has such elements as title, header, body and footer. The columns can be made be adding “spans” inside the body of the table. The table can have borders from specific sides, for example: “:border_right => true” or “:border_right => false”.
require "pdf_gen" PDFGen::document PDF::Writer.new, 0.cm do table do title do caption "Transactions and Triggers", :text_color => Color::RGB::Red, :border => true, :justification => :center, :pad_bottom => 0.25.cm end header do span do caption "Number", :width => av_width / 2, :justification => :center, :border => true caption "Discription", :width => av_width / 2, :justification => :center, :border => true end end body do span do caption " Trigger ", :width => av_width / 2, :border_right => true, :border_left => true, :text_color => Color::RGB::Grey caption " - is a ... ", :width => av_width / 2, :border_right => true, :border_left => true, :text_color => Color::RGB::Grey end end footer do span do caption "1", :width => av_width / 2, :justification => :center, :border => true caption "2", :width => av_width / 2, :justification => :center, :border => true end end end end.save("#{File.basename(__FILE__, ".rb")}.pdf")
Smart Table¶ ↑
An object which allows creating tables from Hash and ActiveRecord. If necessary, you can define the blocks of the title and footer independently. For example:
table_data = {:columns =>['id','name','email'], :body => [[1,'Valeriy','valeriy@example.com'], [2,'Dmitriy','dmitriy@example.com']]} PDFGen::document PDF::Writer.new, 0.cm do div :paddings => 2 do table :data_source => table_data end end.save("#{File.basename(__FILE__, ".rb")}.pdf")
Building a table from ActiveRecord (ROR)
table_data = User.all PDFGen::document PDF::Writer.new, 0.cm do div :paddings => 2 do table :data_source => table_data end end.save("#{File.basename(__FILE__, ".rb")}.pdf")
Custom view
table_data = {:columns =>['id','name','email'], :body => [[1,'Valeriy','valeriy@example.com'], [2,'Dmitriy','dmitriy@example.com']]} PDFGen::document PDF::Writer.new, 0.cm do div :paddings => 2 do table :data_source => table_data do header do row do cell "<b>#{ds.columns[0]}</b>", :justification => :center, :text_color => Color::RGB::Green cell ds.columns[1], :justification => :center, :text_color => Color::RGB::Green cell ds.columns[2], :justification => :center, :text_color => Color::RGB::Green end end body do ds.each do |datarow| row do cell datarow[0] cell "<i>#{datarow[1]}</i>" cell datarow[2] end end end end end end.save("#{File.basename(__FILE__, ".rb")}.pdf")
We can set the width of each column
row do cell data[0], :width => av_width * 2/4 cell data[1], :width => av_width * 1/4 cell data[2], :width => av_width * 1/4 end
Method cell supports blocks, for example:
body do ds.each do |datarow| row do cell datarow[1], :width => av_width / 2 cell datarow[2], :width => av_width / 4 cell :div, :width => av_width / 4, :paddings => 0.2.cm do image(open(File.expand_path("example_image.png"), "rb") { |file| file.read }, :width => av_width / 2) end end end end
In this case the first arguments can be “:div”, “:span”, “:table” and others
Credits¶ ↑
Team’s line-up¶ ↑
-
Sphere Consulting Inc Development Team.
Copyright © 2010 Sphere Consulting Inc., released under the MIT license (see LICENSE).