Class: ArTeX::PDF

Inherits:
Object
  • Object
show all
Defined in:
lib/artex/pdf.rb

Instance Method Summary collapse

Instance Method Details

#get_template_name_from_partial_name(partial_name) ⇒ Object

‘bills/form/billing_position/_billing_position.pdf.rtex’ -> ‘bills/form/billing_position/_billing_position.pdf.rtex’



48
49
50
51
52
53
54
55
# File 'lib/artex/pdf.rb', line 48

def get_template_name_from_partial_name(partial_name)
  parts = partial_name.split(/\//)

  # "_" vor das letzte Element setzen.
  parts[-1] = "_" + parts.last
  template_name = parts.join("/")
  template_name
end

#get_template_path(path) ⇒ Object



85
86
87
# File 'lib/artex/pdf.rb', line 85

def get_template_path(path)
  raise "unimplemented"
end

#handle_locals(options = {}) ⇒ Object

creates the posibility of locals renderign



58
59
60
61
62
63
64
65
# File 'lib/artex/pdf.rb', line 58

def handle_locals(options = {})
  return unless options[:locals]
  options[:locals].each do |name, value|
    l = lambda { return value }
    self.class.send(:define_method, name.to_sym, l )

  end
end

#is_collection?(options) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/artex/pdf.rb', line 31

def is_collection?(options)
  options[:collection] && options[:collection].is_a?(Array)    
end

#l(*args) ⇒ Object



89
90
91
# File 'lib/artex/pdf.rb', line 89

def l(*args)
  return ArTeX::Document.escape(*args)
end

#latex_to_pdf(latex_content) ⇒ Object

Nimmt Latex und schreibt ein PDF. Gibt den Pfad zum PDF zurück.



76
77
78
79
80
81
82
83
# File 'lib/artex/pdf.rb', line 76

def latex_to_pdf(latex_content)
  pdf_file = ""
  ::ArTeX::Document.new(latex_content, {:process => true} ).to_pdf do |filename|
    pdf_file = Tempfile.new('artex-pdf')
    FileUtils.mv filename, pdf_file.path
  end
  return pdf_file.path
end

#render(options = {}) ⇒ Object

Wir bilden das Verhalten von render :partial nach. So wird die Abhängigkeit zu ActionView aufgelöst. Dabei gehen sicherlich Möglichkeiten verloren, die hier aber auch nicht benötigt werden. Ziel ist die Verlagerung der PDF-Generierung aus dem Controller in Klassen bzw. Models, so dass dies auch im Hintergrund erfolgen kann.

Beispiele

render :partial => "layouts/brandings/#{@bill.branding.template_name}"
render :partial => 'bills/form/billing_position/_billing_position.pdf.rtex', :collection => @bill.billing_positions


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/artex/pdf.rb', line 14

def render(options = {})
  content = ""

  # Relativer Pfad zum Template
  # z.B. 'bills/form/billing_position/_billing_position.pdf.rtex'
  template_name = get_template_name_from_partial_name(options[:partial])

  handle_locals(options)

  if is_collection?(options) then
    content = render_collection(options[:collection], binding, template_name)
  else
    content = render_to_latex(binding, template_name)
  end
  return content
end

#render_collection(collection, binding, template_name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/artex/pdf.rb', line 35

def render_collection(collection, binding, template_name)
  content = ""
  
  #TODO refactor
  collection.each do |billing_position|      
      @billing_position = billing_position
      latex = render_to_latex(binding, template_name)        
      content += latex
  end
  return content
end

#render_to_latex(binding, path) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/artex/pdf.rb', line 67

def render_to_latex(binding, path)
  template_path = get_template_path(path)
  template = File.read(template_path)

  latex_code = ERB.new(template).result(binding)
  return latex_code
end