slim2pdf 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slim2pdf.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Piotr Macuk
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # Slim2pdf [![Code Climate](https://codeclimate.com/github/macuk/slim2pdf.png)](https://codeclimate.com/github/macuk/slim2pdf)
2
+
3
+ Slim2pdf renders slim template with data hash and save the results as pdf file.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'slim2pdf'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install slim2pdf
18
+
19
+ ## Usage
20
+
21
+ require 'slim2pdf'
22
+
23
+ ### Create writer object
24
+
25
+ writer = Slim2pdf::Writer.new
26
+ writer.template = 'path/to/template.slim'
27
+ writer.data = {key1: value1, key2: value2}
28
+
29
+ ### Set parameters in initializer
30
+
31
+ writer = Slim2pdf::Writer.new('tpl.slim', {key1: 'a', key2: 'b'})
32
+
33
+ ### Possible actions
34
+
35
+ writer.render_to_html # return rendered html as string
36
+
37
+ writer.save_to_html(html_path) # saves rendered html to file
38
+
39
+ writer.save_to_pdf(pdf_path) # saves rendered html as pdf file
40
+
41
+ ## Examples
42
+
43
+ ### Change slim template to pdf file (doc/examples/simple/).
44
+
45
+ require 'slim2pdf'
46
+
47
+ writer = Slim2pdf::Writer.new('doc/examples/simple/template.slim')
48
+ writer.save_to_pdf('doc/examples/simple/output.pdf')
49
+
50
+ ### Generate agreement (doc/examples/agreement/).
51
+
52
+ require 'slim2pdf'
53
+
54
+ w = Slim2pdf::Writer.new
55
+ w.template = 'doc/examples/agreement/template.slim'
56
+ w.data = {date: '2014-01-14', part1: 'Google', part2: 'Microsoft'}
57
+ w.footer_text = 'Agreement footer'
58
+ w.save_to_pdf('doc/examples/agreement/output.pdf')
59
+
60
+ ### Generate bulk invoices (doc/examples/invoices/).
61
+
62
+ require 'slim2pdf'
63
+
64
+ w = Slim2pdf::Writer.new('doc/examples/invoices/template.slim')
65
+ buyers = ['Buyer A', 'Buyer B', 'Buyer C', 'Buyer D']
66
+ buyers.each_with_index do |buyer, index|
67
+ number = index + 1
68
+ w.data = {
69
+ seller: 'Seller', buyer: buyer, number: "#{number}/2014",
70
+ item_name: 'Service', price: '$100', date: '2014-01-14'
71
+ }
72
+ w.save_to_pdf("doc/examples/invoices/output-#{number}.pdf")
73
+ end
74
+
75
+ ## Contributing
76
+
77
+ 1. Fork it
78
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
79
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
80
+ 4. Push to the branch (`git push origin my-new-feature`)
81
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/*_test.rb']
7
+ end
8
+
9
+ task :default => :test
Binary file
@@ -0,0 +1,7 @@
1
+ require 'slim2pdf'
2
+
3
+ w = Slim2pdf::Writer.new
4
+ w.template = 'doc/examples/agreement/template.slim'
5
+ w.data = {date: '2014-01-14', part1: 'Google', part2: 'Microsoft'}
6
+ w.footer_text = 'Agreement footer'
7
+ w.save_to_pdf('doc/examples/agreement/output.pdf')
@@ -0,0 +1,15 @@
1
+ doctype html
2
+ html
3
+ head
4
+ title Example Agreement
5
+ body
6
+ p = date
7
+ h1 Example Agreement
8
+ #content
9
+ h2
10
+ | Agreement between #{part1} and #{part2}
11
+ h2 Agreement text
12
+ ol
13
+ li Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.
14
+ li Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.
15
+ li Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,12 @@
1
+ require 'slim2pdf'
2
+
3
+ w = Slim2pdf::Writer.new('doc/examples/invoices/template.slim')
4
+ buyers = ['Buyer A', 'Buyer B', 'Buyer C', 'Buyer D']
5
+ buyers.each_with_index do |buyer, index|
6
+ number = index + 1
7
+ w.data = {
8
+ seller: 'Seller', buyer: buyer, number: "#{number}/2014",
9
+ item_name: 'Service', price: '$100', date: '2014-01-14'
10
+ }
11
+ w.save_to_pdf("doc/examples/invoices/output-#{number}.pdf")
12
+ end
@@ -0,0 +1,16 @@
1
+ doctype html
2
+ html
3
+ head
4
+ title Example Invoice
5
+ body
6
+ p
7
+ | Created at #{date}
8
+ h1
9
+ | Invoice nr #{number}
10
+ #content
11
+ h2
12
+ | Seller: #{seller}
13
+ h2
14
+ | Buyer: #{buyer}
15
+ p
16
+ | Item name: #{item_name}, price: #{price}
Binary file
@@ -0,0 +1,4 @@
1
+ require 'slim2pdf'
2
+
3
+ writer = Slim2pdf::Writer.new('doc/examples/simple/template.slim')
4
+ writer.save_to_pdf('doc/examples/simple/output.pdf')
@@ -0,0 +1,7 @@
1
+ doctype html
2
+ html
3
+ head
4
+ title Example Title
5
+ body
6
+ h1 Example Header
7
+ p Example Body
data/lib/slim2pdf.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "slim2pdf/version"
2
+ require "slim"
3
+ require "ostruct"
4
+ require "fileutils"
5
+ require "pathname"
6
+ require "tempfile"
7
+ require "slim2pdf/writer"
@@ -0,0 +1,3 @@
1
+ module Slim2pdf
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,58 @@
1
+ module Slim2pdf
2
+ class Writer
3
+ attr_accessor :template, :data
4
+ attr_accessor :footer_text, :footer_font, :footer_font_size
5
+
6
+ def initialize(template=nil, data={})
7
+ @template = template
8
+ @data = data
9
+ end
10
+
11
+ def render_to_html
12
+ Slim::Template.new(template).render(scope)
13
+ end
14
+
15
+ def save_to_html(path)
16
+ create_dir(path)
17
+ File.write(path, render_to_html)
18
+ end
19
+
20
+ def save_to_pdf(path)
21
+ create_dir(path)
22
+ html = create_tmp_html
23
+ `#{wkhtmltopdf_command(html.path, path)}`
24
+ html.unlink
25
+ end
26
+
27
+ def wkhtmltopdf_command(html_path, pdf_path)
28
+ "wkhtmltopdf #{footer_params} -q #{html_path} #{pdf_path}"
29
+ end
30
+
31
+ private
32
+ # Change hash data to scope object
33
+ def scope
34
+ OpenStruct.new(data)
35
+ end
36
+
37
+ # Create dir if not exists
38
+ def create_dir(path)
39
+ FileUtils.mkdir_p(Pathname.new(path).dirname)
40
+ end
41
+
42
+ def create_tmp_html
43
+ tmp = Tempfile.new ['slim2pdf', '.html']
44
+ begin
45
+ tmp.write render_to_html
46
+ ensure
47
+ tmp.close
48
+ end
49
+ tmp
50
+ end
51
+
52
+ def footer_params
53
+ return unless footer_text
54
+ "--footer-line --footer-font-name '#{footer_font || 'verdana'}' " +
55
+ "--footer-font-size '#{footer_font_size || 10}' --footer-left '#{footer_text}'"
56
+ end
57
+ end
58
+ end
data/slim2pdf.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slim2pdf/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slim2pdf"
8
+ spec.version = Slim2pdf::VERSION
9
+ spec.authors = ["Piotr Macuk"]
10
+ spec.email = ["piotr@macuk.pl"]
11
+ spec.description = %q{Slim2pdf renders slim template with data hash and save the results as pdf file.}
12
+ spec.summary = %q{Slim template to pdf file conversion tool.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "simplecov"
24
+
25
+ spec.add_dependency 'slim', '~> 2.0.2'
26
+ spec.add_dependency "wkhtmltopdf-binary", "~> 0.9.9.1"
27
+ end
@@ -0,0 +1,8 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter '/test/'
4
+ end
5
+
6
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
7
+ require 'slim2pdf'
8
+ require 'minitest/autorun'
@@ -0,0 +1,8 @@
1
+ doctype html
2
+ html
3
+ head
4
+ title = title
5
+ body
6
+ h1 = title
7
+ #content
8
+ p = content
@@ -0,0 +1,75 @@
1
+ require 'test_helper'
2
+
3
+ module Slim2pdf
4
+ class WriterTest < MiniTest::Unit::TestCase
5
+ def setup
6
+ @data = {title: 'Slim2pdf', content: 'Slim to PDF conversion gem'}
7
+ @template = File.expand_path('../tpl/test.slim', __FILE__)
8
+ @writer = Writer.new(@template, @data)
9
+ end
10
+
11
+ def test_init
12
+ Writer.new
13
+ end
14
+
15
+ def test_initialize
16
+ assert_equal @template, @writer.template
17
+ assert_equal @data, @writer.data
18
+ end
19
+
20
+ def test_accessors
21
+ writer = Writer.new
22
+ writer.template = @template
23
+ writer.data = @data
24
+ assert_equal @template, writer.template
25
+ assert_equal @data, writer.data
26
+ end
27
+
28
+ def test_render_to_html
29
+ html = @writer.render_to_html
30
+ assert_match %r(<title>Slim2pdf</title>), html
31
+ assert_match %r(<p>Slim to PDF conversion gem</p>), html
32
+ end
33
+
34
+ def test_save_to_html
35
+ path = '/tmp/test.html'
36
+ @writer.save_to_html(path)
37
+ assert File.exists?(path)
38
+ html = File.read(path)
39
+ assert_match %r(<title>Slim2pdf</title>), html
40
+ assert_match %r(<p>Slim to PDF conversion gem</p>), html
41
+ File.unlink(path)
42
+ assert_equal false, File.exists?(path)
43
+ end
44
+
45
+ def test_save_to_html_with_dir_creation
46
+ path = '/tmp/a/b/c/test.html'
47
+ @writer.save_to_html(path)
48
+ html = File.read(path)
49
+ assert_match %r(<html>), html
50
+ FileUtils.rm_rf('/tmp/a')
51
+ assert_equal false, File.exists?('/tmp/a')
52
+ end
53
+
54
+ def test_save_to_pdf
55
+ path = '/tmp/test.pdf'
56
+ @writer.save_to_pdf(path)
57
+ assert File.exists?(path)
58
+ File.unlink(path)
59
+ assert_equal false, File.exists?(path)
60
+ end
61
+
62
+ def test_footer_params
63
+ @writer.footer_text = 'Footer'
64
+ command = @writer.wkhtmltopdf_command('tmp.html', 'out.pdf')
65
+ assert_match /Footer/, command
66
+ assert_match /10/, command
67
+ assert_match /verdana/, command
68
+ @writer.footer_font = 'arial'
69
+ @writer.footer_font_size = 14
70
+ command = @writer.wkhtmltopdf_command('tmp.html', 'out.pdf')
71
+ assert_match /14/, command
72
+ assert_match /arial/, command
73
+ end
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slim2pdf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Piotr Macuk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: simplecov
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: slim
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.0.2
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.0.2
78
+ - !ruby/object:Gem::Dependency
79
+ name: wkhtmltopdf-binary
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 0.9.9.1
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 0.9.9.1
94
+ description: Slim2pdf renders slim template with data hash and save the results as
95
+ pdf file.
96
+ email:
97
+ - piotr@macuk.pl
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - Gemfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - doc/examples/agreement/output.pdf
108
+ - doc/examples/agreement/script.rb
109
+ - doc/examples/agreement/template.slim
110
+ - doc/examples/invoices/output-1.pdf
111
+ - doc/examples/invoices/output-2.pdf
112
+ - doc/examples/invoices/output-3.pdf
113
+ - doc/examples/invoices/output-4.pdf
114
+ - doc/examples/invoices/script.rb
115
+ - doc/examples/invoices/template.slim
116
+ - doc/examples/simple/output.pdf
117
+ - doc/examples/simple/script.rb
118
+ - doc/examples/simple/template.slim
119
+ - lib/slim2pdf.rb
120
+ - lib/slim2pdf/version.rb
121
+ - lib/slim2pdf/writer.rb
122
+ - slim2pdf.gemspec
123
+ - test/test_helper.rb
124
+ - test/tpl/test.slim
125
+ - test/writer_test.rb
126
+ homepage: ''
127
+ licenses:
128
+ - MIT
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ! '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ segments:
140
+ - 0
141
+ hash: 613998296001512521
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ segments:
149
+ - 0
150
+ hash: 613998296001512521
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 1.8.25
154
+ signing_key:
155
+ specification_version: 3
156
+ summary: Slim template to pdf file conversion tool.
157
+ test_files:
158
+ - test/test_helper.rb
159
+ - test/tpl/test.slim
160
+ - test/writer_test.rb