slim2pdf 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +42 -19
- data/doc/examples/agreement/script.rb +5 -5
- data/doc/examples/invoices/script.rb +3 -3
- data/lib/slim2pdf/version.rb +1 -1
- data/lib/slim2pdf/writer.rb +14 -3
- data/test/writer_test.rb +32 -2
- metadata +4 -4
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Slim2pdf [![Code Climate](https://codeclimate.com/github/macuk/slim2pdf.png)](https://codeclimate.com/github/macuk/slim2pdf)
|
2
2
|
|
3
|
-
Slim2pdf renders slim template with data hash and
|
3
|
+
Slim2pdf renders [slim template](http://slim-lang.com/) with data hash and saves the results as pdf file.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -23,55 +23,78 @@ Or install it yourself as:
|
|
23
23
|
### Create writer object
|
24
24
|
|
25
25
|
writer = Slim2pdf::Writer.new
|
26
|
-
writer.template = '
|
27
|
-
writer.data = {
|
26
|
+
writer.template = 'template.slim'
|
27
|
+
writer.data = {name: 'John', surname: 'Doe'}
|
28
28
|
|
29
29
|
### Set parameters in initializer
|
30
30
|
|
31
|
-
writer = Slim2pdf::Writer.new('
|
31
|
+
writer = Slim2pdf::Writer.new('template.slim', {name: 'John', surname: 'Doe'})
|
32
32
|
|
33
33
|
### Possible actions
|
34
34
|
|
35
35
|
writer.render_to_html # return rendered html as string
|
36
36
|
|
37
|
-
writer.save_to_html(
|
37
|
+
writer.save_to_html('output.html') # saves rendered html to file
|
38
38
|
|
39
|
-
writer.save_to_pdf(
|
39
|
+
writer.save_to_pdf('output.pdf') # saves rendered html as pdf file
|
40
|
+
|
41
|
+
### Changing default wkhtmltopdf command
|
42
|
+
|
43
|
+
writer = Slim2pdf::Writer.new
|
44
|
+
writer.wkhtmltopdf_command = '/your/path/to/wkhtmltopdf --your --params'
|
45
|
+
|
46
|
+
Input (html) and output (pdf) files will be added automatically.
|
40
47
|
|
41
48
|
## Examples
|
42
49
|
|
43
|
-
###
|
50
|
+
### Save simple slim template without data to pdf file
|
44
51
|
|
45
52
|
require 'slim2pdf'
|
46
53
|
|
47
|
-
writer = Slim2pdf::Writer.new('
|
48
|
-
writer.save_to_pdf('
|
54
|
+
writer = Slim2pdf::Writer.new('simple.slim')
|
55
|
+
writer.save_to_pdf('simple.pdf')
|
49
56
|
|
50
|
-
|
57
|
+
See: [doc/examples/simple](https://github.com/macuk/slim2pdf/tree/master/doc/examples/simple)
|
58
|
+
|
59
|
+
### Generate agreement with footer
|
51
60
|
|
52
61
|
require 'slim2pdf'
|
53
62
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
63
|
+
writer = Slim2pdf::Writer.new('agreement.slim')
|
64
|
+
writer.data = {date: '2014-01-14', part1: 'Google', part2: 'Microsoft'}
|
65
|
+
writer.footer_text = 'Agreement footer'
|
66
|
+
writer.save_to_pdf('agreement.pdf')
|
67
|
+
|
68
|
+
See: [doc/examples/agreement](https://github.com/macuk/slim2pdf/tree/master/doc/examples/agreement)
|
59
69
|
|
60
|
-
### Generate bulk invoices
|
70
|
+
### Generate bulk invoices
|
61
71
|
|
62
72
|
require 'slim2pdf'
|
63
73
|
|
64
|
-
|
74
|
+
writer = Slim2pdf::Writer.new('invoice.slim')
|
65
75
|
buyers = ['Buyer A', 'Buyer B', 'Buyer C', 'Buyer D']
|
66
76
|
buyers.each_with_index do |buyer, index|
|
67
77
|
number = index + 1
|
68
|
-
|
78
|
+
writer.data = {
|
69
79
|
seller: 'Seller', buyer: buyer, number: "#{number}/2014",
|
70
80
|
item_name: 'Service', price: '$100', date: '2014-01-14'
|
71
81
|
}
|
72
|
-
|
82
|
+
writer.save_to_pdf("invoice-#{number}.pdf")
|
73
83
|
end
|
74
84
|
|
85
|
+
See: [doc/examples/invoices](https://github.com/macuk/slim2pdf/tree/master/doc/examples/invoices)
|
86
|
+
|
87
|
+
## Debuging
|
88
|
+
|
89
|
+
### Setting logger
|
90
|
+
|
91
|
+
require 'logger'
|
92
|
+
|
93
|
+
writer = Slim2pdf::Writer.new
|
94
|
+
writer.logger = Logger.new(STDERR)
|
95
|
+
|
96
|
+
If you use Slim2pdf with Rails, the Rails.logger will be set automatically.
|
97
|
+
|
75
98
|
## Contributing
|
76
99
|
|
77
100
|
1. Fork it
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'slim2pdf'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
writer = Slim2pdf::Writer.new
|
4
|
+
writer.template = 'doc/examples/agreement/template.slim'
|
5
|
+
writer.data = {date: '2014-01-14', part1: 'Google', part2: 'Microsoft'}
|
6
|
+
writer.footer_text = 'Agreement footer'
|
7
|
+
writer.save_to_pdf('doc/examples/agreement/output.pdf')
|
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'slim2pdf'
|
2
2
|
|
3
|
-
|
3
|
+
writer = Slim2pdf::Writer.new('doc/examples/invoices/template.slim')
|
4
4
|
buyers = ['Buyer A', 'Buyer B', 'Buyer C', 'Buyer D']
|
5
5
|
buyers.each_with_index do |buyer, index|
|
6
6
|
number = index + 1
|
7
|
-
|
7
|
+
writer.data = {
|
8
8
|
seller: 'Seller', buyer: buyer, number: "#{number}/2014",
|
9
9
|
item_name: 'Service', price: '$100', date: '2014-01-14'
|
10
10
|
}
|
11
|
-
|
11
|
+
writer.save_to_pdf("doc/examples/invoices/output-#{number}.pdf")
|
12
12
|
end
|
data/lib/slim2pdf/version.rb
CHANGED
data/lib/slim2pdf/writer.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
module Slim2pdf
|
2
2
|
class Writer
|
3
3
|
attr_accessor :template, :data
|
4
|
+
attr_accessor :wkhtmltopdf_command
|
4
5
|
attr_accessor :footer_text, :footer_font, :footer_font_size
|
6
|
+
attr_accessor :logger
|
5
7
|
|
6
8
|
def initialize(template=nil, data={})
|
7
9
|
@template = template
|
@@ -15,17 +17,26 @@ module Slim2pdf
|
|
15
17
|
def save_to_html(path)
|
16
18
|
create_dir(path)
|
17
19
|
File.write(path, render_to_html)
|
20
|
+
logger.info "[Slim2pdf] HTML file saved: #{path}" if logger
|
18
21
|
end
|
19
22
|
|
20
23
|
def save_to_pdf(path)
|
21
24
|
create_dir(path)
|
22
25
|
html = create_tmp_html
|
23
|
-
|
26
|
+
full_command = "#{wkhtmltopdf_command} #{html.path} #{path}"
|
27
|
+
logger.debug "[Slim2pdf] Run command: #{full_command}" if logger
|
28
|
+
`#{full_command}`
|
24
29
|
html.unlink
|
30
|
+
logger.info "[Slim2pdf] PDF file saved: #{path}" if logger
|
25
31
|
end
|
26
32
|
|
27
|
-
|
28
|
-
|
33
|
+
# wkhtmltopdf command without html and pdf file params
|
34
|
+
def wkhtmltopdf_command
|
35
|
+
@wkhtmltopdf_command || "wkhtmltopdf #{footer_params} -q"
|
36
|
+
end
|
37
|
+
|
38
|
+
def logger
|
39
|
+
@logger ||= defined?(Rails) ? Rails.logger : nil
|
29
40
|
end
|
30
41
|
|
31
42
|
private
|
data/test/writer_test.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
require 'logger'
|
3
|
+
require 'stringio'
|
2
4
|
|
3
5
|
module Slim2pdf
|
4
6
|
class WriterTest < MiniTest::Unit::TestCase
|
@@ -59,17 +61,45 @@ module Slim2pdf
|
|
59
61
|
assert_equal false, File.exists?(path)
|
60
62
|
end
|
61
63
|
|
64
|
+
def test_wkhtmltopdf_command
|
65
|
+
assert_match /wkhtmltopdf/, @writer.wkhtmltopdf_command
|
66
|
+
assert_match /-q/, @writer.wkhtmltopdf_command
|
67
|
+
@writer.wkhtmltopdf_command = 'test -a -b -c'
|
68
|
+
assert_equal 'test -a -b -c', @writer.wkhtmltopdf_command
|
69
|
+
end
|
70
|
+
|
62
71
|
def test_footer_params
|
63
72
|
@writer.footer_text = 'Footer'
|
64
|
-
command = @writer.wkhtmltopdf_command
|
73
|
+
command = @writer.wkhtmltopdf_command
|
65
74
|
assert_match /Footer/, command
|
66
75
|
assert_match /10/, command
|
67
76
|
assert_match /verdana/, command
|
68
77
|
@writer.footer_font = 'arial'
|
69
78
|
@writer.footer_font_size = 14
|
70
|
-
command = @writer.wkhtmltopdf_command
|
79
|
+
command = @writer.wkhtmltopdf_command
|
71
80
|
assert_match /14/, command
|
72
81
|
assert_match /arial/, command
|
73
82
|
end
|
83
|
+
|
84
|
+
def test_logger_accessor
|
85
|
+
assert_nil @writer.logger
|
86
|
+
stderr = Logger.new(STDERR)
|
87
|
+
@writer.logger = stderr
|
88
|
+
assert_equal stderr, @writer.logger
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_logging
|
92
|
+
sio = StringIO.new
|
93
|
+
@writer.logger = Logger.new(sio)
|
94
|
+
@writer.logger.level = Logger::DEBUG
|
95
|
+
@writer.save_to_html('/tmp/out.html')
|
96
|
+
File.unlink('/tmp/out.html')
|
97
|
+
@writer.save_to_pdf('/tmp/out.pdf')
|
98
|
+
File.unlink('/tmp/out.pdf')
|
99
|
+
log = sio.string
|
100
|
+
assert_match %r(/tmp/out.html), log
|
101
|
+
assert_match %r(/tmp/out.pdf), log
|
102
|
+
assert_match /Run command: wkhtmltopdf/, log
|
103
|
+
end
|
74
104
|
end
|
75
105
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slim2pdf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-01-
|
12
|
+
date: 2014-01-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -138,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
138
138
|
version: '0'
|
139
139
|
segments:
|
140
140
|
- 0
|
141
|
-
hash:
|
141
|
+
hash: -782601368619973164
|
142
142
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
143
|
none: false
|
144
144
|
requirements:
|
@@ -147,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
147
|
version: '0'
|
148
148
|
segments:
|
149
149
|
- 0
|
150
|
-
hash:
|
150
|
+
hash: -782601368619973164
|
151
151
|
requirements: []
|
152
152
|
rubyforge_project:
|
153
153
|
rubygems_version: 1.8.25
|