infopark_rails_connector 6.8.0.515.34928522 → 6.8.0.518.30999728

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +0,0 @@
1
- module RailsConnector
2
- # @api public
3
- class Configuration
4
- # @api public
5
- module PdfGenerator
6
- @@hosts = []
7
-
8
- # @api public
9
- def self.host_whitelist(*hosts)
10
- @@hosts = hosts
11
- end
12
-
13
- def self.host_allowed?(host)
14
- @@hosts.include?(host)
15
- end
16
- end
17
- end
18
- end
@@ -1,18 +0,0 @@
1
- module RailsConnector
2
- module Fop
3
- #
4
- # Is raised if the PDF generation fails.
5
- # The error message contains the Java error stack trace of the Apache FOP.
6
- #
7
- class GenerationFailed < RuntimeError; end
8
-
9
- CMD = 'fop -xml "%s" -xsl "%s" -pdf "%s" 2>&1'
10
-
11
- def self.generate_pdf(xml, xsl, pdf)
12
- output = `#{CMD % [xml, xsl, pdf]}`
13
- unless `file -pb "#{pdf}"`.include?('PDF')
14
- raise GenerationFailed, output
15
- end
16
- end
17
- end
18
- end
@@ -1,92 +0,0 @@
1
- module RailsConnector
2
- #
3
- # This module provides an interface for generating PDF files.
4
- #
5
- module FopOnRails
6
- #
7
- # Is raised if the response was not 200.
8
- #
9
- class DownloadError < RuntimeError; end
10
-
11
- READ_TIMEOUT = 300
12
-
13
- #
14
- # Generates a PDF from <code>xml_location</code> and <code>xsl_location</code> and returns its
15
- # path in the file system.
16
- #
17
- # If the file <code>xml_location</code> doesn't already exist, then <code>xml_location</code>
18
- # and <code>xsl_location</code> are interpreted as URLs and will be downloaded before
19
- # the PDF is generated.
20
- #
21
- # If the download of <code>xml_location</code> or <code>xsl_location</code> fails, then
22
- # the {RailsConnector::FopOnRails::DownloadError} is raised.
23
- #
24
- # To generate a PDF, the given XML <b>must</b> be valid. If for some reasons it is not, then you can use the
25
- # <code>tidy</code> option to repair it.
26
- #
27
- # If the PDF generation fails, then the {RailsConnector::Fop::GenerationFailed} error
28
- # is raised.
29
- #
30
- # Example:
31
- #
32
- # RailsConnector::FopOnRails.generate_pdf('/tmp/test.xml', '/tmp/test.xsl')
33
- # # This will generate a PDF and return its path
34
- #
35
- # RailsConnector::FopOnRails.generate_pdf('http://mywebsite.com/test.xml', 'http://mywebsite.com/test.xsl', true)
36
- # # This will download both files, repair the XML, generate a PDF, and return its path
37
- #
38
- def self.generate_pdf(xml_location, xsl_location, tidy = false)
39
- log("\nStarting PDF generation...")
40
-
41
- w_dir = File.join(Rails.root, %w(tmp fop_on_rails), $$.to_s)
42
- FileUtils.rm_rf(w_dir)
43
- FileUtils.mkdir_p(w_dir)
44
-
45
- pdf = "#{w_dir}/pdf"
46
-
47
- if File.exist?(xml_location)
48
- xml = xml_location
49
- xsl = xsl_location
50
- log("\trepairing XML")
51
- FopOnRails::Document.repair(xml)
52
- else
53
- xml = "#{w_dir}/xml"
54
- xsl = "#{w_dir}/xsl"
55
-
56
- log("\tgetting XSL source from #{xsl_location}")
57
- download(xml_location, xml)
58
- log("\tgetting XML source from #{xml_location}")
59
- download(xsl_location, xsl)
60
- end
61
-
62
- if tidy
63
- log("\trepairing XML")
64
- FopOnRails::Document.repair(xml, xml_location)
65
- end
66
-
67
- log("\tgenerating PDF\n\n")
68
- Fop.generate_pdf(xml, xsl, pdf)
69
-
70
- return pdf
71
- end
72
-
73
- private
74
-
75
- def self.download(src, dest)
76
- url = URI.parse(src)
77
- http = Net::HTTP.new(url.host, url.port)
78
- http.read_timeout = READ_TIMEOUT
79
- resp = http.get(url.request_uri)
80
- unless resp.kind_of?(Net::HTTPSuccess)
81
- raise DownloadError, "Could not download #{src}"
82
- end
83
- File.open dest, 'w' do |f|
84
- f.write resp.body
85
- end
86
- end
87
-
88
- def self.log(msg)
89
- Rails.logger.info(msg)
90
- end
91
- end
92
- end
@@ -1,25 +0,0 @@
1
- require 'rails_connector/fop_on_rails/document/dom_tree'
2
- require 'rails_connector/fop_on_rails/document/images'
3
- require 'rails_connector/fop_on_rails/document/tables'
4
-
5
- module RailsConnector
6
- module FopOnRails
7
- module Document
8
- def self.repair(path, xml_uri = nil)
9
- xml = DomTree.repair(File.read(path))
10
-
11
- if xml_uri
12
- Images.absolutize_src_attrs(xml, xml_uri)
13
- else
14
- Images.dump_images(xml)
15
- end
16
-
17
- Tables.repair(xml)
18
-
19
- File.open(path, 'w') do |f|
20
- f.write(xml.to_s)
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,42 +0,0 @@
1
- require 'tidy'
2
- require "rexml/document"
3
-
4
- module RailsConnector
5
- module FopOnRails
6
- module Document
7
- module DomTree
8
- TIDY_CONF = {
9
- 'clean' => true,
10
- 'doctype' => 'omit',
11
- 'output-xhtml' => true,
12
- 'numeric-entities' => true,
13
- 'drop-font-tags' => true,
14
- 'enclose-block-text' => true,
15
- 'char-encoding' => 'utf8'
16
- }
17
-
18
- def self.repair(data)
19
- xml = REXML::Document.new(tidy(data))
20
- clean_namespace(xml)
21
- return xml
22
- end
23
-
24
- private
25
-
26
- def self.tidy(data)
27
- %w(dylib so).each do |ext|
28
- if File.exist?(path = "/usr/lib/libtidy.#{ext}")
29
- Tidy.path = path
30
- break
31
- end
32
- end
33
- Tidy.open(TIDY_CONF) { |tidy| tidy.clean(data) }
34
- end
35
-
36
- def self.clean_namespace(xml)
37
- xml.root.delete_namespace
38
- end
39
- end
40
- end
41
- end
42
- end
@@ -1,34 +0,0 @@
1
- module RailsConnector
2
- module FopOnRails
3
- module Document
4
- module Images
5
- def self.absolutize_src_attrs(xml, url)
6
- @url = URI.parse(url)
7
- xml.elements.each('//img') do |img|
8
- url = URI.parse(img.attributes['src'])
9
- unless (url.host == @url.host)
10
- url = @url.merge(url)
11
- end
12
- img.attributes['src'] = url.to_s
13
- end
14
- xml
15
- end
16
-
17
- def self.dump_images(xml)
18
- xml.elements.each('//img') do |img|
19
- unless URI.parse(src = img.attributes['src']).absolute?
20
- if relative_url_root = ActionController::Base.config.relative_url_root
21
- src.sub!(/\A#{relative_url_root}/i, '')
22
- end
23
- params = Rails.application.routes.recognize_path(src)
24
- if params[:controller] == 'rails_connector/cms_dispatch' and params[:id]
25
- img.attributes['src'] = Obj.find(params[:id]).body_data_path
26
- end
27
- end
28
- end
29
- xml
30
- end
31
- end
32
- end
33
- end
34
- end
@@ -1,67 +0,0 @@
1
- module RailsConnector
2
- module FopOnRails
3
- module Document
4
- module Tables
5
- def self.repair(xml)
6
- xml.elements.each '//table' do |table|
7
- add_missing_cols(table)
8
- add_col_metadata(table)
9
- end
10
- end
11
-
12
- private
13
-
14
- def self.add_missing_cols(table)
15
- @@max = 0
16
- trs = table.get_elements('./tr | ./tbody/tr | ./thead/tr | ./tfoot/tr')
17
- trs.each do |tr|
18
- n = 0
19
- tr.each_element('./td | ./th') do |elem|
20
- colspan = elem.attributes['colspan'].to_i
21
- if colspan > 0
22
- n += colspan
23
- else
24
- n += 1
25
- elem.delete_attribute 'colspan'
26
- end
27
- end
28
- @@max = n if n > @@max
29
- end
30
- trs.each do |tr|
31
- n = @@max - tr.get_elements('./td | ./th').size
32
- tr.each_element('./td | ./th') do |elem|
33
- colspan = elem.attributes['colspan'].to_i
34
- n -= colspan if colspan > 0
35
- end
36
- n.times do
37
- tr.add_element 'td'
38
- end
39
- end
40
- end
41
-
42
- def self.add_col_metadata(table)
43
- default_width = 480
44
- cols = table.get_elements('./col | ./colgroup/col')
45
- vals, total, not_nulls = [], 0, 0
46
- [@@max, cols.size].min.times do |i|
47
- width = cols[i].attributes['width'].to_i
48
- if width > 0
49
- vals[i] = width
50
- not_nulls += 1
51
- end
52
- total += width
53
- end
54
- total = default_width unless total > 0
55
- @@max.times do |i|
56
- if val = vals[i]
57
- vals[i] = (val * default_width * not_nulls) / (total * @@max)
58
- else
59
- vals[i] = default_width / @@max
60
- end
61
- end
62
- table.attributes['cols'] = vals * "\s"
63
- end
64
- end
65
- end
66
- end
67
- end