Document and Callbacks - shairontoledo/rghost GitHub Wiki
The document is a big facade with two other PsFacade and DocumentCallbackFacade
Creating document with 80 rows per page and custom paper
doc=RGhost::Document.new :rows_per_page => 80, paper => [30,5]
Document A4 with row height 0.5 and font encoding CodePage1252
doc=RGhost::Document.new :row_height => 0.5, :font_encoding => 'CodePage1252'
Defining all margins
doc=RGhost::Document.new :margin => 0.5
Parameters
- :paper – Facade to paper defined on the construction of Paper class
- :margin, :duplex and :tuble – Facade to options defined on the construction of Paper class
- :rows_per_page – Specifies count of rows per pages.
- :landscape – Whether true invert de size(width per height)
- :count_pages – Defines postscript internal variable to display with class TextIn. Example:
Sample
doc=RGhost::Document.new :count_pages => 10
doc.before_page_create :except => 1 do
text_in :x => 15, :y => 5, :write => "Page %current_page% of %count_pages%"
end
To newer versions we should get an object from block
it’ll improve way as the things works inside of RGhost
doc=RGhost::Document.new :count_pages => 10
doc.before_page_create :except => 1 do |chunk|
chunk.text_in :x => 15, :y => 5, :write => "Page %current_page% of %count_pages%"
end
The value above count_pages will be evaluated inside of document for all pages except page one.
- :fontsize – Defines the size of tag :default_font.
- :row_height and row_padding – Its names say by itself :)
- :font_encoding – Specifies encoding of data input. You can look for supported encoding using the method RGhost::Config.encode_test
Callbacks are custom code blocks defined inside Document. All callbacks implicitly receive an instance of PsFacade to create any PsObject. The callbacks‘ execution depends on the algorithms predefined in Postscript core library. There are two kinds of callbacks, Static and Dynamic callbacks.
In a Static callback there aren‘t parameters to control the inclusion and exception in the current scope, which is usually applied to events which happen one time, for example after_document_create: “after document create” will always execute once for each document. Otherwise, with Dynamic callbacks there is control of scope using conditional :only or :except; this is the only difference in relation to static callbacks. The parameters of a Dynamic callback must be one integer or one array of integers.
Example: For all pages except page 3
doc.before_page_create :except => 3 do
# do something
end
For just 2 and 4 pages
doc.before_page_create :only => [2,4] do
# do something
end
Most callbacks are defined in facades such as DocumentCallbackFacade and Grid::CallbackFacade
This module is included inside Document class. It will create methods to make it easier to use.
Example for a document with 4 pages; looking at the picture you can see the order in which the callbacks are executed.
You can combine the callback before_page_create and text_in for product page numbering using internal variables. Example:
doc=RGhost::Document.new :count_pages => 3
doc.before_page_create :except => 1 do |chunk|
chunk.text_in :x=> 3, :y=> 5.5, :text => "%current_page% of %count_pages%"
end
doc.next_page
doc.next_page
doc.next_page
Starts and Ends internal benchmark will write at bottom of page.
Example
doc=RGhost::Document.new
doc.benchmark :start
doc.... #do something
doc.benchmarck :stop
doc.render ...
Facade to RGhost::Engine#render converts a document to an output format, such as :pdf, :png, :ps, :jpeg, :tiff etc The parameter device can be found at RGhost::Constants::Devices or at Details of Ghostscript output devices
Method render has the following options available.
- :filename – File path.
- :logfile – Writes the converter‘s process into a file.
- :multipage – if true the output will be one page per file posfixed by 0001.ext, for example, one file name ‘test.png’ with two pages will create test_001.png and test002.png
- :resolution – Integer value for output resolution.
- :size – Crops a single page using a string of dimensions, example, ‘200x180’, ‘140x90’.
- :range – Specifies range of pages (PDF only)
- :quality – PDF Quality
Arrays of Hashes for Ghostscript interpreter. Look at Parameter switch for more details. You can use two parameter :s and :d, examples
:s => [{:GenericResourceDir => /dir, :DEFAULTPAPERSIZE=> "a3"}]
:d => [ {:TextAlphaBits => 2} ]
Or one string using the parameter :raw, as below
:raw => "-sGenericResourceDir=/test -dTextAlphaBits=2"
Examples
doc=RGhost::Document.new
#do something
doc.render :pdf, :filename => 'foo.pdf # PDF output
doc.render :jpeg, :filename => 'foo.jpg' # JPEG output
doc.render :png, :filename => 'foo.png', :multipage => true # PNG output one page per file
doc.render :tiff, :filename => 'foo.tiff', :resolution => 300 # TIFF with 300dpi
doc.render :ps, :raw => '-sFONTMAP=/var/myoptional/font/map', :filename => 'test.ps'
doc=Document.new
doc.raw "hahahah!" #it produced an error in ps stack
doc.render :jpeg, :filename => 'with_error.jpg'
puts r.errors if r.error?
#=> GPL Ghostscript 8.61: Unrecoverable error, exit code 1.\ Error: /undefined in hahahah!