-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_pdf.rb
executable file
·47 lines (42 loc) · 1.13 KB
/
create_pdf.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env ruby
def create_tex(src_dir)
jpegs = Dir["#{src_dir}/*.jpg"].sort_by { |f| File.stat(f).mtime }
# jpegs = Dir["#{src_dir}/*.jpg"].sort
if jpegs.size == 0
STDERR.puts "no jpegs found in #{src_dir}"
exit
end
STDERR.puts(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|
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
def create_pdf(tex_file)
STDERR.puts "output=#{tex_file}"
exit_code = %x( pdflatex -interaction=batchmode #{tex_file} 2>&1 )
# STDERR.puts "exit code #{exit_code}"
end
def clean_up(work_dir)
# STDERR.puts "cleanup #{work_dir}"
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
work_dir = Dir.pwd
tex = create_tex(work_dir)
pdf = create_pdf(tex)
clean_up(work_dir)