svg_export 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 29cd48d4c228f1bff9ef0b858af286576259509f
4
+ data.tar.gz: 76599ab388d1411d9ecbf746caefa2b2661f5c53
5
+ SHA512:
6
+ metadata.gz: bd97c57721248ff795a323619c81eab75134ee60043d7245b61ab9f3c34170013c1bec233bda6de1a71e697efe9a9983738b2c56717d445faabfd7e7ccc20467
7
+ data.tar.gz: 0799b34368309f3d4a10dbb18b007fff9374bd17de0a49d1da1f50c52f4c62e3056a586266bd07e515ef0418192c894f4318415ca26ace26402df30de9243281
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Stefan Wienert
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Svg-Export Engine for Ruby on Rails
2
+
3
+ Highcharts compliant svg-rasterizer. To find more about Highcharts Exporting, look [here](http://www.highcharts.com/docs/export-module/export-module-overview)
4
+
5
+ Features:
6
+
7
+ * makes relative URLs absolute (with HTTP_REFERER)
8
+ * relative URLs that begins with /assets are locally linked (Performance boost + works in single-threaded server envs)
9
+
10
+
11
+ ## Installation
12
+
13
+
14
+ ### 1. install Gem
15
+
16
+ ```ruby
17
+ # Gemfile
18
+ gem 'svg_export'
19
+ ```
20
+
21
+ Run ``bundle``
22
+
23
+ ### 2. mount engine + configure
24
+
25
+
26
+ ```ruby
27
+ # config/routes.rb
28
+
29
+ Rails.application.routes.draw do
30
+ #...
31
+
32
+ mount SvgExport::Engine => '/exporting'
33
+ end
34
+ ```
35
+
36
+ ### 3. Download or install Apache Batik
37
+
38
+ This project needs Apache Batik installed on the system.
39
+
40
+ Quick install:
41
+
42
+ ```
43
+ cd bin
44
+ wget http://ftp.halifax.rwth-aachen.de/apache/xmlgraphics/batik/binaries/batik-bin-1.8.tar.gz
45
+ tar xf batik-bin-1.8.tar.gz
46
+ ```
47
+
48
+ And configure the path, e.g. in ``config/initializers/svg_export.rb``
49
+
50
+ ```ruby
51
+ SvgExport::Engine.batik_path = Rails.root.join('bin/batik-1.8/batik-rasterizer-1.8.jar')
52
+ ```
53
+
54
+
55
+ ### 4. Change Highchart configuration to use that server:
56
+
57
+ e.g.:
58
+
59
+ ```javascript
60
+ {
61
+ exporting: {
62
+ url: '/exporting'
63
+ }
64
+ }
65
+ ```
66
+
67
+
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'SvgExport'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+ load 'rails/tasks/statistics.rake'
20
+
21
+
22
+
23
+ Bundler::GemHelper.install_tasks
24
+
@@ -0,0 +1,4 @@
1
+ module SvgExport
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,18 @@
1
+ module SvgExport
2
+ class SvgController < ApplicationController
3
+ skip_before_filter :verify_authenticity_token
4
+
5
+ def create
6
+ if Engine.batik_path.blank?
7
+ raise ArgumentError.new("Set SvgExport::Engine.batik_path = '...' to the correct path of the batik-rasterizer")
8
+ end
9
+ wrapper = CommandWrapper.new(params, base_url: request.referer)
10
+ begin
11
+ file = wrapper.()
12
+ rescue SvgExport::Error => e
13
+ render :text => "Unable to export image;\n #{e}", status: 422
14
+ end
15
+ send_file file, type: wrapper.type, filename: wrapper.filename, disposition: 'attachment', stream: false
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,70 @@
1
+ module SvgExport
2
+ class CommandWrapper
3
+ attr_reader :params
4
+ def initialize(params, options)
5
+ @params = params
6
+ @allowed_types = Engine.default_options[:allowed_types]
7
+ @tmp_path = Engine.default_options[:tmp_path]
8
+ @svg_transformer = Engine.default_options[:svg_transformer]
9
+ @options = options
10
+ end
11
+
12
+ def call
13
+ transformer = @svg_transformer.new(@options)
14
+ svg = transformer.transform(params[:svg])
15
+ infile.write(svg)
16
+ infile.flush
17
+ result = run!
18
+ fs = File.size?( outfile.path)
19
+ if fs.nil? || fs < 10
20
+ raise SvgExport::Error.new( "Output file empty; #{result}")
21
+ end
22
+ File.open(outfile)
23
+ end
24
+
25
+ def filename
26
+ base = params[:filename].blank? ? "chart" : params[:filename]
27
+ "#{base}.#{ext}"
28
+ end
29
+
30
+ def type
31
+ @type ||= begin
32
+ @allowed_types.find{|t| t == params[:type] } or raise SvgExport::Error.new("Unknown type #{params[:type]}")
33
+ end
34
+ end
35
+
36
+ protected
37
+
38
+ def run!
39
+ cmd = "java -jar #{Engine.batik_path} -m #{type} -d #{outfile.path} #{width} #{infile.path} 2>&1"
40
+ result = `#{cmd}`
41
+ if result.index("success").nil?
42
+ raise SvgExport::Error.new(rsp)
43
+ end
44
+ result
45
+ end
46
+
47
+ def width
48
+ if params[:width].to_i > 0
49
+ "-w #{Shellwords.escape(params[:width])}"
50
+ end
51
+ end
52
+
53
+ def outfile
54
+ @outfile ||= Tempfile.new(['highcharts-out',".#{ext}"], @tmp_path)
55
+ end
56
+
57
+ def infile
58
+ @infile ||= Tempfile.open(['highcharts-in',".svg"], @tmp_path)
59
+ end
60
+
61
+ def ext
62
+ { 'image/png' => 'png',
63
+ 'image/jpeg' => 'jpg',
64
+ 'application/pdf' => 'pdf'
65
+ }[type]
66
+ end
67
+
68
+
69
+ end
70
+ end
@@ -0,0 +1,4 @@
1
+ module SvgExport
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,54 @@
1
+ module SvgExport
2
+ class SvgTransformer
3
+ def initialize(base_url, options={})
4
+ @tempfiles = []
5
+ @base_url = options[:base_url]
6
+ end
7
+
8
+ def transform(svg)
9
+ doc = Nokogiri::XML.parse(svg)
10
+ doc.search('image').each do |image|
11
+ if image['xlink:href'] and image['xlink:href'][/^\//]
12
+ if image['xlink:href'][%r{/assets/([^\?]+)}, 1] and path = find_asset_path($1)
13
+ # local Asset path
14
+ body = File.read(path)
15
+ url = path
16
+ elsif @base_url
17
+ url = URI.join(@base_url, image['xlink:href']).to_s
18
+ body = download(url)
19
+ else
20
+ next
21
+ end
22
+
23
+ ext = url[/\.(\w+)$/, 1] || 'png'
24
+ tf = Tempfile.new(["highchart-tmp-inline", ".#{ext}", Rails.root.join('tmp')])
25
+ tf.binmode
26
+ @tempfiles << tf
27
+ tf.write(body)
28
+ tf.flush
29
+ # relative url
30
+ image['xlink:href'] = "file://" + tf.path
31
+ end
32
+ end
33
+ doc.to_s
34
+ end
35
+
36
+ protected
37
+
38
+ def download(url)
39
+ require 'open-uri'
40
+ open(url)
41
+ end
42
+
43
+ def find_asset_path(partial_path)
44
+ Rails.application.config.assets.paths.each do |asset_dir|
45
+ Dir.glob("#{asset_dir}/*").each do |full_path|
46
+ if full_path.ends_with?(partial_path)
47
+ return full_path
48
+ end
49
+ end
50
+ end
51
+ nil
52
+ end
53
+ end
54
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ SvgExport::Engine.routes.draw do
2
+ root to: 'svg#create'
3
+ match '/' => 'svg#create', via: [:post]
4
+ end
@@ -0,0 +1,18 @@
1
+ module SvgExport
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace SvgExport
4
+ cattr_accessor :batik_path
5
+ cattr_accessor :default_options
6
+ self.default_options = {
7
+ }
8
+ initializer 'svg_export.init' do
9
+ Engine.default_options.reverse_merge!({
10
+ allowed_types: ['image/png', 'image/jpeg', 'application/pdf'],
11
+ tmp_path: Rails.root.join('tmp'),
12
+ svg_transformer: SvgTransformer
13
+ })
14
+ end
15
+ end
16
+ end
17
+
18
+ # SvgExport::Engine.batik_path = SvgExport::Engine.root.join('bin/batik-rasterizer-1.8.jar')
@@ -0,0 +1,3 @@
1
+ module SvgExport
2
+ VERSION = "0.0.1"
3
+ end
data/lib/svg_export.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "svg_export/engine"
2
+
3
+ module SvgExport
4
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :svg_export do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: svg_export
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stefan Wienert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Rails engine for rasterizing Highcharts SVG files
42
+ email:
43
+ - stefan.wienert@pludoni.de
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/controllers/svg_export/application_controller.rb
52
+ - app/controllers/svg_export/svg_controller.rb
53
+ - app/services/svg_export/command_wrapper.rb
54
+ - app/services/svg_export/error.rb
55
+ - app/services/svg_export/svg_transformer.rb
56
+ - config/routes.rb
57
+ - lib/svg_export.rb
58
+ - lib/svg_export/engine.rb
59
+ - lib/svg_export/version.rb
60
+ - lib/tasks/svg_export_tasks.rake
61
+ homepage: https://github.com/pludoni/svg_export
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.2
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Rails engine for rasterizing Highcharts SVG files
85
+ test_files: []