Class: PdfRenderer::Base
- Inherits:
-
Object
- Object
- PdfRenderer::Base
- Includes:
- ActionMailer::AdvAttrAccessor, Helpers
- Defined in:
- lib/pdf_renderer/base.rb
Overview
Base class for rendering PDFs. PDFs are rendered in two passes. The first pass evaluates an LaTeX ERB template. In the second pass the evaluated output is piped through pdflatex. The resulting PDF is returned as a string.
Usage
The usage is very similar to ActionMailer. Example:
class BillPdfRenderer < PdfRenderer::Base
def bill(model)
body :variable => model
end
end
Render the PDF using
pdf_as_string = BillPdfRenderer.render_bill(model)
The default template path is underscored_class_name/action_name.pdf.erb
and is looked for in all specified view_paths.
Saving PDFs
You can also use PdfRenderer::Base to generate and save PDFs to the file system. Instead of calling render_
Helpers
To use view helpers, declare them in class scope. You can declare them as symbols, strings or constants, camelized or underscored. Omit the “Helper” suffix when using strings or symbols.
The LatexHelper included in the PdfRenderer gem is automatically added to the ActionView instance. This helper contains methods for escaping strings to LaTeX.
Options
Inside of render actions, there are a couple of options you can use:
- preprocess
-
Run pdflatex twice for assigning page numbers etc.
- debug
-
Save the rendered tex source to the file system for debugging.
Instance Attribute Summary collapse
-
#tex_out ⇒ Object
readonly
Contains the input for LaTeX.
Class Method Summary collapse
-
.method_missing(method, *params) ⇒ Object
Depending on the method pattern, does one of three things:.
-
.template_root ⇒ Object
The root for view files.
Instance Method Summary collapse
-
#initialize ⇒ Base
constructor
Sets default options.
-
#render(options) ⇒ Object
Delegates the render call to ActionView and runs the resulting LaTeX code through pdflatex.
-
#render! ⇒ Object
Renders the default template.
-
#template_dir ⇒ Object
The directory in which templates are stored by default for this renderer.
-
#template_path ⇒ Object
The complete path to the LaTeX ERB template.
Methods included from Helpers
Constructor Details
#initialize ⇒ Base
Sets default options
- preprocess
-
false
- debug
-
false
61 62 63 64 |
# File 'lib/pdf_renderer/base.rb', line 61 def initialize preprocess false debug false end |
Instance Attribute Details
#tex_out ⇒ Object (readonly)
Contains the input for LaTeX
52 53 54 |
# File 'lib/pdf_renderer/base.rb', line 52 def tex_out @tex_out end |
Class Method Details
.method_missing(method, *params) ⇒ Object
Depending on the method pattern, does one of three things:
-
If the method name starts with
render_
, the action is called on a new instance of the renderer and the rendered PDF is returned as a string. -
If the method starts with
save_
, the action is called, the PDF is generated and saved to the path given in the first method argument. -
Otherwise, the action is called on a new instance of the renderer and a Pdf object is returned.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/pdf_renderer/base.rb', line 76 def self.method_missing(method, *params) if method.to_s =~ /^render_(.*)$/ pdf = send($1, *params) pdf.render! elsif method.to_s =~ /^save_(.*)$/ file_name = params.shift pdf = send($1, *params) pdf.save(file_name) elsif instance_methods.include?(method.to_s) renderer_instance = new renderer_instance.template_name = method renderer_instance.send(renderer_instance.template_name, *params) Pdf.new(renderer_instance) else super end end |
.template_root ⇒ Object
The root for view files.
95 96 97 |
# File 'lib/pdf_renderer/base.rb', line 95 def self.template_root "#{RAILS_ROOT}/app/views" end |
Instance Method Details
#render(options) ⇒ Object
Delegates the render call to ActionView and runs the resulting LaTeX code through pdflatex.
111 112 113 114 |
# File 'lib/pdf_renderer/base.rb', line 111 def render() @tex_out = template_instance.render Latex.new(:preprocess => preprocess, :debug => debug).generate_pdf(tex_out) end |
#render! ⇒ Object
Renders the default template.
117 118 119 |
# File 'lib/pdf_renderer/base.rb', line 117 def render! render :file => template_path end |
#template_dir ⇒ Object
The directory in which templates are stored by default for this renderer.
100 101 102 |
# File 'lib/pdf_renderer/base.rb', line 100 def template_dir self.class.name.underscore end |
#template_path ⇒ Object
The complete path to the LaTeX ERB template.
105 106 107 |
# File 'lib/pdf_renderer/base.rb', line 105 def template_path "#{template_dir}/#{template_name}" end |