Sha256: ebaea89527a3c1d738f0c0a0c23f3ca18c0e755ecd8ebe6888c594cc95d2631e

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 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(" ")
    # help message goes to STDERR (from mn2pdf v1.36)
    blank_message, help_message, = Open3.capture3(cmd)
    help_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

1 entries across 1 versions & 1 rubygems

Version Path
mn2pdf-1.36 lib/mn2pdf.rb