Module: Img2pdf

Defined in:
lib/img2pdf.rb,
lib/img2pdf/cli.rb,
lib/img2pdf/img2pdf.rb,
lib/img2pdf/version.rb

Defined Under Namespace

Classes: CLI, Error

Constant Summary collapse

VERSION =
"0.0.5"

Class Method Summary collapse

Class Method Details

.clean_upObject



28
29
30
31
32
33
34
# File 'lib/img2pdf/img2pdf.rb', line 28

def self.clean_up
  warn "cleanup #{Dir.pwd}"
  # Dir.chdir work_dir
  Dir["*.log"].each { |f| File.delete f }
  Dir["*.aux"].each { |f| File.delete f }
  Dir["*.tex"].each { |f| File.delete f }
end

.convert(work_dir) ⇒ Object



36
37
38
39
40
41
# File 'lib/img2pdf/img2pdf.rb', line 36

def self.convert(work_dir)
  Dir.chdir work_dir
  tex = create_tex(work_dir)
  create_pdf(tex)
  clean_up
end

.create_pdf(tex_file) ⇒ Object



22
23
24
25
26
# File 'lib/img2pdf/img2pdf.rb', line 22

def self.create_pdf(tex_file)
  warn "file=#{tex_file}"
  exit_code = `pdflatex -interaction=batchmode #{tex_file}`
  warn "exit code #{exit_code}"
end

.create_tex(src_dir) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/img2pdf/img2pdf.rb', line 2

def self.create_tex(src_dir)
  jpegs = Dir["#{src_dir}/*.jpg"].sort_by { |f| File.stat(f).mtime }
  warn(jpegs)
  cwd = File.basename(Dir.pwd)
  File.open("#{cwd}.tex", "w") do |tex|
    tex.write(<<~END_OF_TEX_HEADER)
      \\documentclass{article}
      \\usepackage{pdfpages}
      \\begin{document}
    END_OF_TEX_HEADER
    jpegs.each do |page|
      tex.write("\\includepdf{\"#{page[0, page.size - 4]}\".jpg\}\n")
    end
    tex.write(<<~END_OF_TEX_FOOTER)
      \\end{document}
    END_OF_TEX_FOOTER
  end
  "#{cwd}.tex"
end