Module: Mn2pdf
- Defined in:
- lib/mn2pdf.rb,
lib/mn2pdf/version.rb
Constant Summary collapse
- MN2PDF_JAR_PATH =
File.join(File.dirname(__FILE__), "../bin/mn2pdf.jar")
- DEFAULT_JAVA_OPTS =
%w[-Xss10m -Xmx3g -Djava.awt.headless=true].freeze
- FONTS_MANIFEST =
:font_manifest
- VERSION =
"1.96".freeze
- MN2PDF_JAR_VERSION =
VERSION
Class Method Summary collapse
- .build_cmd(url, output, xslt) ⇒ Object
- .convert(url_path, output_path, xsl_stylesheet, options = {}) ⇒ Object
- .dump_fontist_manifest_locations(manifest) ⇒ Object
- .help ⇒ Object
- .jvm_options ⇒ Object
- .mn2pdf(cmd) ⇒ Object
- .mn2pdf_hash(cmd, options) ⇒ Object
- .options_to_cmd(options, cmd) ⇒ Object
- .parse_error_msg(stderr) ⇒ Object
- .puts_error_log(stdout, stderr) ⇒ Object
- .quote(str) ⇒ Object
- .version ⇒ Object
Class Method Details
.build_cmd(url, output, xslt) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/mn2pdf.rb', line 51 def self.build_cmd(url, output, xslt) return if url.nil? || output.nil? || xslt.nil? ["java", *, "-jar", MN2PDF_JAR_PATH, "--xml-file", quote(url), "--xsl-file", quote(xslt), "--pdf-file", quote(output)] end |
.convert(url_path, output_path, xsl_stylesheet, options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mn2pdf.rb', line 36 def self.convert(url_path, output_path, xsl_stylesheet, = {}) cmd = build_cmd(url_path, output_path, xsl_stylesheet) return unless cmd case when String mn2pdf(cmd + []) when Hash mn2pdf_hash(cmd, ) else warn "Unsupported options type #{.class}" end end |
.dump_fontist_manifest_locations(manifest) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/mn2pdf.rb', line 107 def self.dump_fontist_manifest_locations(manifest) Tempfile.create(["fontist_locations", ".yml"]) do |f| f.write manifest.to_yaml f.flush yield f.path end end |
.help ⇒ Object
23 24 25 26 27 28 |
# File 'lib/mn2pdf.rb', line 23 def self.help cmd = ["java", *, "-jar", MN2PDF_JAR_PATH].join(" ") # help message goes to STDERR (from mn2pdf v1.36) _, , = Open3.capture3(cmd) end |
.jvm_options ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/mn2pdf.rb', line 11 def self. = DEFAULT_JAVA_OPTS.dup if RbConfig::CONFIG["host_os"].match?(/darwin|mac os/) << "-Dapple.awt.UIElement=true" end << "-Duser.home=#{Dir.home}" end |
.mn2pdf(cmd) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/mn2pdf.rb', line 74 def self.mn2pdf(cmd) cmd = cmd.join(" ") puts cmd stdout, stderr, status = Open3.capture3(cmd) unless status.success? puts_error_log(stdout, stderr) raise StandardError.new("mn2pdf failed! #{parse_error_msg(stderr)}") end end |
.mn2pdf_hash(cmd, options) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mn2pdf.rb', line 61 def self.mn2pdf_hash(cmd, ) manifest = .delete(FONTS_MANIFEST) (, cmd) if manifest dump_fontist_manifest_locations(manifest) do |manifest_path| cmd << "--font-manifest" << quote(manifest_path) mn2pdf(cmd) end else mn2pdf(cmd) end end |
.options_to_cmd(options, cmd) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/mn2pdf.rb', line 116 def self.(, cmd) .each do |k, v| sep = k.to_s.end_with?("=") ? "" : " " cmd << "#{k}#{sep}#{quote(v)}" end end |
.parse_error_msg(stderr) ⇒ Object
94 95 96 97 98 |
# File 'lib/mn2pdf.rb', line 94 def self.parse_error_msg(stderr) err = stderr.split("\n").detect { |line| line.start_with? "Error: " } err ? err[7..-1] : stderr.strip end |
.puts_error_log(stdout, stderr) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/mn2pdf.rb', line 86 def self.puts_error_log(stdout, stderr) puts ["Fatal error!", "STDOUT:", stdout.strip, "STDERR:", stderr.strip] .join("\n") .split("\n") .map { |line| "[mn2pdf] #{line}" } .join("\n") end |
.quote(str) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/mn2pdf.rb', line 100 def self.quote(str) return "" if str.nil? || str.empty? return str if /^'.*'$/.match(str) || /^".*"$/.match(str) %("#{str}") end |
.version ⇒ Object
30 31 32 33 34 |
# File 'lib/mn2pdf.rb', line 30 def self.version cmd = ["java", *, "-jar", MN2PDF_JAR_PATH, "-v"].join(" ") , = Open3.capture3(cmd) .strip end |