proforma-prawn-renderer 1.0.0.pre.alpha

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ac74c9e31440fb51da3a35d2a83f66d4d2003bade1012dffb0607fe30317de96
4
+ data.tar.gz: ca3305a73f6bd5f1f3de011b6606e8e43ef5820aac35e380282f99bf86bde3ef
5
+ SHA512:
6
+ metadata.gz: c570b4a567c7160b32044d7d5204ed5d967528ea776f1975cf8de54f1af5e6a94763ae6a0820010ac8855527b66eac23b7e0f8e7a1aa78aec2727e2883469d3d
7
+ data.tar.gz: 3473499c3ecf5cfb7a0dd704c666aa208e107d954d78837d366936caaa4b31b8cd0086fae33151e9a99dc165cd27c6a740a238182600352b37c67790743d6bba
data/.editorconfig ADDED
@@ -0,0 +1,8 @@
1
+ # See http://editorconfig.org/
2
+
3
+ [*]
4
+ trim_trailing_whitespace = true
5
+ indent_style = space
6
+ indent_size = 2
7
+ insert_final_newline = true
8
+ end_of_line = lf
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ *.gem
3
+ /tmp
4
+ /coverage
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ Metrics/LineLength:
2
+ Max: 100
3
+
4
+ Metrics/BlockLength:
5
+ ExcludedMethods: ['let', 'it', 'describe', 'context', 'specify']
6
+
7
+ Metrics/MethodLength:
8
+ Max: 25
9
+
10
+ AllCops:
11
+ TargetRubyVersion: 2.3
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.0
data/.travis.yml ADDED
@@ -0,0 +1,20 @@
1
+ env:
2
+ global:
3
+ - CC_TEST_REPORTER_ID=f4247e1b007aa38ed314c32b6e25be8617336be2a34b0fe906b57119d26434c3
4
+ language: ruby
5
+ rvm:
6
+ # Build on the latest stable of all supported Rubies (https://www.ruby-lang.org/en/downloads/):
7
+ - 2.3.8
8
+ - 2.4.5
9
+ - 2.5.3
10
+ - 2.6.0
11
+ cache: bundler
12
+ before_script:
13
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
14
+ - chmod +x ./cc-test-reporter
15
+ - ./cc-test-reporter before-build
16
+ script:
17
+ - bundle exec rubocop
18
+ - bundle exec rspec spec --format documentation
19
+ after_script:
20
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 0.0.1 (April 17th, 2019)
2
+
3
+ Library Shell.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,129 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ proforma-prawn-renderer (1.0.0.pre.alpha)
5
+ prawn (~> 2)
6
+ prawn-table (~> 0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ Ascii85 (1.0.3)
12
+ acts_as_hashable (1.0.5)
13
+ afm (0.2.2)
14
+ ansi (1.5.0)
15
+ ast (2.4.0)
16
+ coderay (1.1.2)
17
+ diff-lcs (1.3)
18
+ docile (1.3.1)
19
+ ffi (1.9.25)
20
+ formatador (0.2.5)
21
+ guard (2.15.0)
22
+ formatador (>= 0.2.4)
23
+ listen (>= 2.7, < 4.0)
24
+ lumberjack (>= 1.0.12, < 2.0)
25
+ nenv (~> 0.1)
26
+ notiffany (~> 0.0)
27
+ pry (>= 0.9.12)
28
+ shellany (~> 0.0)
29
+ thor (>= 0.18.1)
30
+ guard-compat (1.2.1)
31
+ guard-rspec (4.7.3)
32
+ guard (~> 2.1)
33
+ guard-compat (~> 1.1)
34
+ rspec (>= 2.99.0, < 4.0)
35
+ hashery (2.1.2)
36
+ hirb (0.7.3)
37
+ jaro_winkler (1.5.2)
38
+ json (2.1.0)
39
+ listen (3.1.5)
40
+ rb-fsevent (~> 0.9, >= 0.9.4)
41
+ rb-inotify (~> 0.9, >= 0.9.7)
42
+ ruby_dep (~> 1.2)
43
+ lumberjack (1.0.13)
44
+ method_source (0.9.2)
45
+ nenv (0.3.0)
46
+ notiffany (0.1.1)
47
+ nenv (~> 0.1)
48
+ shellany (~> 0.0)
49
+ parallel (1.13.0)
50
+ parser (2.6.0.0)
51
+ ast (~> 2.4.0)
52
+ pdf-core (0.7.0)
53
+ pdf-inspector (1.3.0)
54
+ pdf-reader (>= 1.0, < 3.0.a)
55
+ pdf-reader (2.2.0)
56
+ Ascii85 (~> 1.0.0)
57
+ afm (~> 0.2.1)
58
+ hashery (~> 2.0)
59
+ ruby-rc4
60
+ ttfunk
61
+ powerpack (0.1.2)
62
+ prawn (2.2.2)
63
+ pdf-core (~> 0.7.0)
64
+ ttfunk (~> 1.5)
65
+ prawn-table (0.2.2)
66
+ prawn (>= 1.3.0, < 3.0.0)
67
+ proforma (1.0.0.pre.alpha)
68
+ acts_as_hashable (~> 1)
69
+ pry (0.12.2)
70
+ coderay (~> 1.1.0)
71
+ method_source (~> 0.9.0)
72
+ rainbow (3.0.0)
73
+ rb-fsevent (0.10.3)
74
+ rb-inotify (0.9.10)
75
+ ffi (>= 0.5.0, < 2)
76
+ rspec (3.8.0)
77
+ rspec-core (~> 3.8.0)
78
+ rspec-expectations (~> 3.8.0)
79
+ rspec-mocks (~> 3.8.0)
80
+ rspec-core (3.8.0)
81
+ rspec-support (~> 3.8.0)
82
+ rspec-expectations (3.8.2)
83
+ diff-lcs (>= 1.2.0, < 2.0)
84
+ rspec-support (~> 3.8.0)
85
+ rspec-mocks (3.8.0)
86
+ diff-lcs (>= 1.2.0, < 2.0)
87
+ rspec-support (~> 3.8.0)
88
+ rspec-support (3.8.0)
89
+ rubocop (0.63.1)
90
+ jaro_winkler (~> 1.5.1)
91
+ parallel (~> 1.10)
92
+ parser (>= 2.5, != 2.5.1.1)
93
+ powerpack (~> 0.1)
94
+ rainbow (>= 2.2.2, < 4.0)
95
+ ruby-progressbar (~> 1.7)
96
+ unicode-display_width (~> 1.4.0)
97
+ ruby-progressbar (1.10.0)
98
+ ruby-rc4 (0.1.5)
99
+ ruby_dep (1.5.0)
100
+ shellany (0.0.1)
101
+ simplecov (0.16.1)
102
+ docile (~> 1.1)
103
+ json (>= 1.8, < 3)
104
+ simplecov-html (~> 0.10.0)
105
+ simplecov-console (0.4.2)
106
+ ansi
107
+ hirb
108
+ simplecov
109
+ simplecov-html (0.10.2)
110
+ thor (0.20.3)
111
+ ttfunk (1.5.1)
112
+ unicode-display_width (1.4.1)
113
+
114
+ PLATFORMS
115
+ ruby
116
+
117
+ DEPENDENCIES
118
+ guard-rspec (~> 4.7)
119
+ pdf-inspector (~> 1)
120
+ proforma (>= 1.0.0.pre.alpha)
121
+ proforma-prawn-renderer!
122
+ pry (~> 0)
123
+ rspec (~> 3.8)
124
+ rubocop (~> 0.63.1)
125
+ simplecov (~> 0.16.1)
126
+ simplecov-console (~> 0.4.2)
127
+
128
+ BUNDLED WITH
129
+ 1.17.3
data/Guardfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard :rspec, cmd: 'bundle exec rspec' do
4
+ require 'guard/rspec/dsl'
5
+ dsl = Guard::RSpec::Dsl.new(self)
6
+
7
+ # RSpec files
8
+ rspec = dsl.rspec
9
+ watch(rspec.spec_helper) { rspec.spec_dir }
10
+ watch(rspec.spec_support) { rspec.spec_dir }
11
+ watch(rspec.spec_files)
12
+
13
+ # Ruby files
14
+ ruby = dsl.ruby
15
+ dsl.watch_spec_files_for(ruby.lib_files)
16
+ end
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2019 Blue Marble Payroll, LLC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # Proforma Prawn Renderer
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/proforma-prawn-renderer.svg)](https://badge.fury.io/rb/proforma-prawn-renderer) [![Build Status](https://travis-ci.org/bluemarblepayroll/proforma-prawn-renderer.svg?branch=master)](https://travis-ci.org/bluemarblepayroll/proforma-prawn-renderer) [![Maintainability](https://api.codeclimate.com/v1/badges/c7807c3864ca2c32e244/maintainability)](https://codeclimate.com/github/bluemarblepayroll/proforma-prawn-renderer/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/c7807c3864ca2c32e244/test_coverage)](https://codeclimate.com/github/bluemarblepayroll/proforma-prawn-renderer/test_coverage) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
+
5
+ [Proforma](https://github.com/bluemarblepayroll/proforma) ships with a basic plain-text renderer. Luckily we can plug in different rendering engines to provide additional business value. This library gives Proforma the ability to render PDF's instead of simple plain-text.
6
+
7
+ ## Installation
8
+
9
+ To install through Rubygems:
10
+
11
+ ````
12
+ gem install install proforma-prawn-renderer
13
+ ````
14
+
15
+ You can also add this to your Gemfile:
16
+
17
+ ````
18
+ bundle add proforma-prawn-renderer
19
+ ````
20
+
21
+ ## Examples
22
+
23
+ ### Connecting to Proforma Rendering Pipeline
24
+
25
+ To use this plugin within Proforma:
26
+
27
+ 1. Install [Proforma](https://github.com/bluemarblepayroll/proforma)
28
+ 2. Install this library
29
+ 3. Require both libraries
30
+ 4. Pass in an instance of Proforma::PrawnRenderer into the Proforma#render method
31
+
32
+ ````ruby
33
+ require 'proforma'
34
+ require 'proforma/prawn_renderer'
35
+
36
+ data = [
37
+ { id: 1, name: 'Chicago Bulls' },
38
+ { id: 2, name: 'Indiana Pacers' },
39
+ { id: 3, name: 'Boston Celtics' }
40
+ ]
41
+
42
+ template = {
43
+ title: 'nba_team_list',
44
+ children: [
45
+ { type: 'Header', value: 'NBA Teams' },
46
+ { type: 'Separator' },
47
+ {
48
+ type: 'DataTable',
49
+ columns: [
50
+ { header: 'Team ID #', body: '$:id' },
51
+ { header: 'Team Name', body: '$:name' },
52
+ ]
53
+ }
54
+ ]
55
+ }
56
+
57
+ documents = Proforma.render(data, template, renderer: Proforma::PrawnRenderer.new)
58
+ ````
59
+
60
+ The `documents` attribute will now be an array with one object:
61
+
62
+ ```ruby
63
+ expected_documents = [
64
+ {
65
+ contents: "...", # PDF Data
66
+ extension: '.pdf',
67
+ title: 'nba_team_list'
68
+ }
69
+ ]
70
+ ```
71
+
72
+ The `contents` attribute will contain the native PDF data.
73
+
74
+ ### Prawn Customization
75
+
76
+ All options for Prawn are passed through the Proforma::PrawnRenderer::Options object:
77
+
78
+ Name | Default
79
+ ---------------- | -------
80
+ bold_font_style | :bold
81
+ header_font_size | 15
82
+ text_font_size | 10
83
+ font_name | null
84
+
85
+ These options will be used during the PDF rendering process.
86
+
87
+ ## Contributing
88
+
89
+ ### Development Environment Configuration
90
+
91
+ Basic steps to take to get this repository compiling:
92
+
93
+ 1. Install [Ruby](https://www.ruby-lang.org/en/documentation/installation/) (check proforma-prawn-renderer.gemspec for versions supported)
94
+ 2. Install bundler (gem install bundler)
95
+ 3. Clone the repository (git clone git@github.com:bluemarblepayroll/proforma-prawn-renderer.git)
96
+ 4. Navigate to the root folder (cd proforma)
97
+ 5. Install dependencies (bundle)
98
+
99
+ ### Running Tests
100
+
101
+ To execute the test suite run:
102
+
103
+ ````
104
+ bundle exec rspec spec --format documentation
105
+ ````
106
+
107
+ Alternatively, you can have Guard watch for changes:
108
+
109
+ ````
110
+ bundle exec guard
111
+ ````
112
+
113
+ Also, do not forget to run Rubocop:
114
+
115
+ ````
116
+ bundle exec rubocop
117
+ ````
118
+
119
+ ### Publishing
120
+
121
+ Note: ensure you have proper authorization before trying to publish new versions.
122
+
123
+ After code changes have successfully gone through the Pull Request review process then the following steps should be followed for publishing new versions:
124
+
125
+ 1. Merge Pull Request into master
126
+ 2. Update `lib/proforma/prawn_renderer/version.rb` using [semantic versioning](https://semver.org/)
127
+ 3. Install dependencies: `bundle`
128
+ 4. Update `CHANGELOG.md` with release notes
129
+ 5. Commit & push master to remote and ensure CI builds master successfully
130
+ 6. Build the project locally: `gem build proforma-prawn-renderer`
131
+ 7. Publish package to RubyGems: `gem push proforma-prawn-renderer-X.gem` where X is the version to push
132
+ 8. Tag master with new version: `git tag <version>`
133
+ 9. Push tags remotely: `git push origin --tags`
134
+
135
+ ## License
136
+
137
+ This project is MIT Licensed.
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'proforma/prawn_renderer'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ require 'pry'
11
+ Pry.start
data/bin/render ADDED
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'proforma/prawn_renderer'
6
+ require 'fileutils'
7
+ require 'yaml'
8
+ require 'pry'
9
+ require 'pdf/inspector'
10
+
11
+ def read_yaml(filename)
12
+ # rubocop:disable Security/YAMLLoad
13
+ YAML.load(read(filename))
14
+ # rubocop:enable Security/YAMLLoad
15
+ end
16
+
17
+ def read(filename)
18
+ File.open(filename, 'r:bom|utf-8').read
19
+ end
20
+
21
+ def data_merge(data, hash)
22
+ if data.is_a?(Array)
23
+ data.map do |record|
24
+ record.merge(hash)
25
+ end
26
+ elsif data.is_a?(Hash)
27
+ data.merge(hash)
28
+ end
29
+ end
30
+
31
+ jobs_dir = File.join('tmp', 'jobs')
32
+ output_dir = File.join('tmp', 'jobs_output')
33
+
34
+ jobs = Dir[File.join(jobs_dir, '*.yml')]
35
+
36
+ if jobs.empty?
37
+ puts "No jobs detected in the path: #{jobs_dir}"
38
+ puts '--------------------------------------------------------------'
39
+ puts "To use, place YAML files into #{jobs_dir} directory."
40
+ puts 'You can find example YAML files in: spec/fixtures/snapshots'
41
+ puts '--------------------------------------------------------------'
42
+
43
+ exit
44
+ end
45
+
46
+ images_dir = File.join('tmp', 'images', '*')
47
+
48
+ images_hash = Dir[images_dir].map do |image_filename|
49
+ key = File.basename(image_filename, File.extname(image_filename))
50
+ [key, File.open(image_filename, 'rb')]
51
+ end.to_h
52
+
53
+ jobs.each do |job|
54
+ job_name = File.basename(job, File.extname(job))
55
+
56
+ puts "Rendering: #{job_name} (#{job})"
57
+
58
+ contents = read_yaml(job)
59
+ data = data_merge(contents['data'], images_hash)
60
+ template = contents['template']
61
+ evaluator = contents['evaluator'] || Proforma::HashEvaluator.new
62
+ renderer = contents['renderer'] || Proforma::PrawnRenderer.new
63
+ job_output_dir = File.join(output_dir, job_name)
64
+
65
+ FileUtils.mkdir_p(job_output_dir)
66
+
67
+ outputs = Proforma.render(
68
+ data,
69
+ template,
70
+ evaluator: evaluator,
71
+ renderer: renderer
72
+ )
73
+
74
+ outputs.each_with_index do |output, index|
75
+ name_without_extension = [
76
+ index.to_s,
77
+ output.title.to_s
78
+ ].reject(&:empty?).join('.')
79
+
80
+ filename = "#{name_without_extension}#{output.extension}"
81
+
82
+ IO.write(File.join(job_output_dir, filename), output.contents)
83
+ end
84
+
85
+ puts "Done Rendering: #{job} (output can be found in: #{job_output_dir})"
86
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (c) 2019-present, Blue Marble Payroll, LLC
5
+ #
6
+ # This source code is licensed under the MIT license found in the
7
+ # LICENSE file in the root directory of this source tree.
8
+ #
9
+
10
+ module Proforma
11
+ class PrawnRenderer
12
+ # This class understands how to ender a Proforma::Modeling::Banner component.
13
+ class BannerRenderer < Renderer
14
+ def render(banner)
15
+ pdf.table(
16
+ make_rows(banner),
17
+ width: total_width,
18
+ column_widths: make_column_widths(banner)
19
+ )
20
+ end
21
+
22
+ private
23
+
24
+ def make_rows(banner)
25
+ row = []
26
+ row << make_image_cell(banner) if banner.image
27
+ row << make_text_cell(banner) unless text_contents(banner).empty?
28
+ [row]
29
+ end
30
+
31
+ def make_column_widths(banner)
32
+ column_widths = []
33
+
34
+ if banner.image && banner.image_width && !text_contents(banner).empty?
35
+ column_widths << banner.image_width
36
+ end
37
+
38
+ column_widths
39
+ end
40
+
41
+ def make_text_cell(banner)
42
+ pdf.make_cell(text_contents(banner), text_cell_style(!banner.image.nil?))
43
+ end
44
+
45
+ def make_image_cell(banner)
46
+ {
47
+ image: banner.image,
48
+ image_width: banner.image_width,
49
+ image_height: banner.image_height,
50
+ padding: 0,
51
+ border_width: 0
52
+ }
53
+ end
54
+
55
+ def text_contents(banner)
56
+ contents = []
57
+ contents << "<b>#{banner.title}</b>" unless banner.title.empty?
58
+ contents << banner.details.to_s unless banner.details.empty?
59
+ contents.join("\n")
60
+ end
61
+
62
+ def cell_style
63
+ @cell_style ||= {
64
+ border_width: 0,
65
+ padding: 0,
66
+ font: options.font_name,
67
+ size: options.text_font_size
68
+ }
69
+ end
70
+
71
+ def text_cell_style(pad_left)
72
+ padding = pad_left ? [0, 0, 0, 10] : [0, 0, 0, 0]
73
+
74
+ cell_style.merge(
75
+ inline_format: true,
76
+ padding: padding
77
+ )
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (c) 2019-present, Blue Marble Payroll, LLC
5
+ #
6
+ # This source code is licensed under the MIT license found in the
7
+ # LICENSE file in the root directory of this source tree.
8
+ #
9
+
10
+ require_relative 'renderer'
11
+
12
+ module Proforma
13
+ class PrawnRenderer
14
+ # This class understands how to ender a Proforma::Modeling::Header component.
15
+ class HeaderRenderer < Renderer
16
+ def render(text)
17
+ pdf.text(
18
+ text.value.to_s,
19
+ font: options.font_name,
20
+ size: options.header_font_size,
21
+ style: options.bold_font_style
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (c) 2019-present, Blue Marble Payroll, LLC
5
+ #
6
+ # This source code is licensed under the MIT license found in the
7
+ # LICENSE file in the root directory of this source tree.
8
+ #
9
+
10
+ module Proforma
11
+ class PrawnRenderer
12
+ # A list of options available for customizing the rendering engine.
13
+ class Options
14
+ DEFAULT_BOLD_FONT_STYLE = :bold
15
+ DEFAULT_FONT_NAME = ''
16
+ DEFAULT_TEXT_FONT_SIZE = 10
17
+ DEFAULT_HEADER_FONT_SIZE = DEFAULT_TEXT_FONT_SIZE + 5
18
+
19
+ attr_writer :bold_font_style,
20
+ :header_font_size,
21
+ :text_font_size
22
+
23
+ attr_accessor :font_name
24
+
25
+ def bold_font_style
26
+ @bold_font_style || DEFAULT_BOLD_FONT_STYLE
27
+ end
28
+
29
+ def header_font_size
30
+ @header_font_size ? @header_font_size.to_f : DEFAULT_HEADER_FONT_SIZE
31
+ end
32
+
33
+ def text_font_size
34
+ @text_font_size ? @text_font_size.to_f : DEFAULT_TEXT_FONT_SIZE
35
+ end
36
+ end
37
+ end
38
+ end