Class: SvgTemplateToPdf
- Inherits:
-
Object
- Object
- SvgTemplateToPdf
- Defined in:
- lib/svg_template_to_pdf.rb,
lib/svg_template_to_pdf/version.rb
Constant Summary collapse
- VERSION =
"0.1.1"
Instance Method Summary collapse
-
#initialize(svg_filename, csv_filename, output_folder = "~", pdf_filename = nil) ⇒ SvgTemplateToPdf
constructor
A new instance of SvgTemplateToPdf.
- #process ⇒ Object
-
#variables ⇒ Object
Get header names from svg.
Constructor Details
#initialize(svg_filename, csv_filename, output_folder = "~", pdf_filename = nil) ⇒ SvgTemplateToPdf
Returns a new instance of SvgTemplateToPdf.
8 9 10 11 12 13 |
# File 'lib/svg_template_to_pdf.rb', line 8 def initialize(svg_filename, csv_filename, output_folder = "~", pdf_filename = nil) @svg_input = File.read(svg_filename) @csv_filename = csv_filename @output_folder = output_folder @pdf_filename = pdf_filename end |
Instance Method Details
#process ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/svg_template_to_pdf.rb', line 23 def process output_files = [] CSV.read(@csv_filename, headers: true).each_with_index do |row, i| # for each row produce pdf with data content = @svg_input.dup variables.each { |col| content.sub!("%VAR_#{col}%", row[col].to_s) } output_files << Svg2pdf.convert_from_data(content, :pdf, working_dir: @output_folder, output_name: "#{@pdf_filename}#{i}") end output_files end |
#variables ⇒ Object
Get header names from svg
16 17 18 19 20 21 |
# File 'lib/svg_template_to_pdf.rb', line 16 def variables result = @svg_input.scan(/%VAR_(\w+)%/).flatten duplicates = get_duplicates(result) raise StandardError, "More than 1 use of variables #{duplicates.join(", ")} in the SVG file." if duplicates.size != 0 result end |