class Latexpdf::PdfGenerator

Attributes

errors[R]
pdf_file[R]
template[R]

Public Class Methods

new(tex) click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 11
def initialize(tex)
  @template = tex
end

Public Instance Methods

cleanup() click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 29
def cleanup
  if File.directory?(build_path)
    FileUtils.remove_entry_secure(build_path)
  end
  @build_path = nil
end
content() click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 40
def content
  File.read(pdf_file) if pdf_exist?
end
generate(target_file=nil) click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 15
def generate(target_file=nil)
  write_tex
  Latexpdf.configuration.passes.times do
    run_tex
  end

  if pdf_exist?
    @pdf_file = target_pdf_file
    FileUtils.cp target_pdf_file, target_file if target_file
  else
    raise LatexpdfError.new "Tex failed: No PDF generated"
  end
end
pdf_exist?() click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 36
def pdf_exist?
  File.exist?(target_pdf_file)
end

Protected Instance Methods

build_path() click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 46
def build_path
  @build_path ||= make_build_path
end

Private Instance Methods

build_id() click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 78
def build_id
  @build_id ||= SecureRandom.uuid
end
make_build_path() click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 82
def make_build_path
  build_dir = Pathname.new(Latexpdf.configuration.build_path)
  new_build_path = build_dir.join(build_id)
  FileUtils.mkdir_p(new_build_path)
  new_build_path
end
run_tex(generate_pdf: true) click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 63
def run_tex(generate_pdf: true)
  args = %w[-halt-on-error -shell-escape -interaction batchmode]
  args += %w[-no-pdf] unless generate_pdf
  args = args + ["#{target_tex_file}"]

  cmd = "cd #{build_path} && #{tex_command} #{args.join(' ')}"
  result = system cmd, [:out, :err] => "/dev/null"

  raise LatexpdfError.new "Tex failed:\n#{tex_log}" unless result
end
target_pdf_file() click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 89
def target_pdf_file
  build_path.join("input.pdf")
end
target_tex_file() click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 97
def target_tex_file
  build_path.join("input.tex")
end
tex_command() click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 59
def tex_command
  "xelatex"
end
tex_log() click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 74
def tex_log
  @errors ||= File.read(tex_log_file)
end
tex_log_file() click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 93
def tex_log_file
  build_path.join("input.log")
end
write_tex() click to toggle source
# File lib/latexpdf/pdf_generator.rb, line 52
def write_tex
  File.open(target_tex_file, "w") do |f|
    f.write template
    f.close
  end
end