Sha256: 4f5a4bc23d678bb589aeb29323ed4ee716925b6d5cd3a8751c1d45e66c6e166c

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

require "open3"
require "rbconfig"
require "mn2pdf/version"

module Mn2pdf
  MN2PDF_JAR_PATH = File.join(File.dirname(__FILE__), "../bin/mn2pdf.jar")

  def self.jvm_options
    options = []

    if RbConfig::CONFIG["host_os"].match?(/darwin|mac os/)
      options << "-Dapple.awt.UIElement=true"
    end

    options
  end

  def self.help
    cmd = ["java", *jvm_options, "-jar", MN2PDF_JAR_PATH].join(" ")
    message, = Open3.capture3(cmd)
    message
  end

  def self.version
    cmd = ["java", *jvm_options, "-jar", MN2PDF_JAR_PATH, "-v"].join(" ")
    message, = Open3.capture3(cmd)
    message.strip
  end

  def self.convert(url_path, output_path, xsl_stylesheet, options = "")
    return if url_path.nil? || output_path.nil? || xsl_stylesheet.nil?

    cmd = ["java", "-Xss5m", "-Xmx2048m", *jvm_options,
           "-jar", MN2PDF_JAR_PATH, "--xml-file", url_path,
           "--xsl-file", xsl_stylesheet, "--pdf-file", output_path, options].join(" ")

    puts cmd
    stdout_str, error_str, status = Open3.capture3(cmd)

    raise prepare_error_msg(stdout_str, error_str) unless status.success?
  end

  def self.prepare_error_msg(stdout_str, error_str)
    # Strip default mn2pdf message
    stdout_str = stdout_str.gsub("Preparing...", "").strip
    ["[mn2pdf] Fatal:", stdout_str, error_str].join(" ").strip
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mn2pdf-1.35 lib/mn2pdf.rb
mn2pdf-1.34 lib/mn2pdf.rb
mn2pdf-1.33 lib/mn2pdf.rb
mn2pdf-1.32 lib/mn2pdf.rb
mn2pdf-1.31.2 lib/mn2pdf.rb