Working with templates and images - shairontoledo/rghost GitHub Wiki

Template

Rghost can make use of Encapsulated Postscript files to act as templates (EPS). This way you can create the visual layout of the page using a graphics tool and just paint the dynamic pieces over using RGhost.


Above we have mytemplate.eps that was generated by a graphic app, my_ruby_program.rb that takes care of the positioning and ultimately the generated output.

A Template use example: Letβ€˜s say that the files first.eps and content.eps already exist. Now we will see how to create a document that uses the template first.eps for the cover and the rest of the document uses content.eps.

d = Document.new :margin_top => 5, :margin_bottom => 2

Just for the first page

d.before_document_create do
  image "/my/dir/first.eps"                          #loads the template
  text_in :x=> 5, :y=> 17, :text => "My Report", :with => :big
  next_page                                          #go to the next page using cursors
end

To newer versions we should get an object from block

d.before_document_create do |chunk|
  chunk.image "/my/dir/first.eps"                          #loads the template
  chunk.text_in :x=> 5, :y=> 17, :text => "My Report", :with => :big
  next_page                                          #go to the next page using cursors
end

Callback for all other pages.

d.before_page_create :except => 1 do
  image "/my/dir/content.eps"
  text_in :text => "Page %current_page% of %count_pages%", :x => 18, :y => 27, :with => :normal
 end

1500 rows

1500.times do |n|
   d.show_next_row "Value #{n}"
 end

We have a cover page and 1500 rows, judging by the margins each page supports 46 rows, so we have 1500/46 = 32.60 pages plus the cover. Rounding it up totals 34 pages for the :count_pages
d.define_variable(:count_pages, 34)
  d.showpage
  d.render :pdf, :filename => "/tmp/test.pdf"

If we knew the number of pages beforehand we could state it on the creation of the document, i.e.
 :count_pages => 34

The example uses one template per page, but this is not a limit in RGhost. You can have multiple images and templates per page. Just have to define the template:
d=Document.new :margin_top => 5, :margin_bottom => 2
d.define_template(:myform, '/local/template/form1.eps', :x=> 3, :y => 5)

and call it on the document.
d.use_template :myform

Arguments
* :name – Template name.
* :file_path – Path to file.
* :options – Options facade to Image.for(or image)

Image.for

Facade method for load image by file extension. Uses Eps, Gif and Jpeg class. Accepts gif, jpeg, jpg and eps extensions.
Options:
* :x and :y – Coordinates to position.
* :rotate – Angle to image rotation if there is one.
* :zoom – Resize the image proportionally.

EPS
Loading eps file

Examples

doc=Document.new
 doc.set Eps.new "/local/templates/myform.eps", :x => 10, :y => 3

Using Image.for facade
 doc.set Image.for "/local/templates/myform.eps", :x => 10, :y => 3

Using PsFacade or Document.
 doc.image "/local/templates/myform.eps", :x => 10, :y => 3

Gif

Loads GIF image from file. Examples

doc=Document.new
doc.set Gif.new "../public/images/button.gif", :x => 10, :y => 3

Using Image.for facade
 doc.set Image.for "../public/images/button.gif", :x => 10, :y => 3

Using PsFacade or Document.
doc.image "images/button.gif", :x => 10, :y => 3

Using 200 percent zoom

 doc.image "images/button.gif", :zoom => 200

Jpeg

Loads JPEG image from file
Examples

doc=Document.new
doc.set Jpeg.new "../public/images/button.jpg", :x => 10, :y => 3

Using Image.for facade

doc.set Image.for "../public/images/button.jpg", :x => 10, :y => 3

Using PsFacade or Document.
doc.image "images/button.jpg", :x => 10, :y => 3

Using Zoom of the 200 percent
doc.image "images/button.jpg", :zoom => 200
⚠️ **GitHub.com Fallback** ⚠️