Skip to content

Instantly share code, notes, and snippets.

@abenori
Last active October 18, 2021 11:35
Show Gist options
  • Save abenori/2429fea3838692bcc7cba1e7006d74d9 to your computer and use it in GitHub Desktop.
Save abenori/2429fea3838692bcc7cba1e7006d74d9 to your computer and use it in GitHub Desktop.
画像をPDFにまとめる
local engine = "platex"
local target = "original"
local do_not_use_latex_for_pdf_file = false
local style = "abmrepos"
local lfs = require "lfs"
local latexopt = "-interaction=nonstopmode"
engine = string.lower(engine)
local doccls
if engine == "pdflatex" or engine == "lualatex" then
doccls = "\\documentclass[a4paper]{article}"
elseif engine == "platex" or engine == "uplatex" then
doccls = "\\documentclass[a4paper,autodetect-engine,dvipdfmx]{jsarticle}"
else
print("UnKnown engine " .. engine)
os.exit(1)
end
doccls = doccls .. "\n"
local head = doccls .. "\\usepackage{pdfpages,grffile," .. style .. "}\n\\begin{document}\n"
local foot = "\\end{document}"
function make_style(file)
if (not lfs.attributes(file)) then
local fp = io.open(file,"w")
fp:write([[
\usepackage{ifthen,xparse}
\newsavebox{\abbox}
\newlength{\ablengtha}
\newlength{\ablengthb}
\NewDocumentCommand{\abincludeimage}{s O{} m}{%
\IfBooleanT{#1}{\includepdf[#2]{#3}}{%
\savebox{\abbox}{\includegraphics{#3}}%
\settowidth{\ablengtha}{\usebox{\abbox}}%
\settoheight{\ablengthb}{\usebox{\abbox}}%
\ifthenelse{\lengthtest{\ablengtha<\ablengthb}}%
{\includepdf{#3}}%
{\includepdf[#2]{#3}}%
}%
}]]
)
fp:close()
end
end
function delete_spaces(dir)
local file,dobj
local dir_with_slash = dir
if dir_with_slash:match("/$") == nil then dir_with_slash = dir_with_slash .. "/" end
for file,dobj in lfs.dir(dir) do
local mode = lfs.attributes(file,"mode")
if mode == "directory" then goto continue end
if file:find(" ") ~= nil then
local newfile = file:gsub(" ","")
print("\"" .. file .. "\" contains a space, replaced to \"" .. newfile .. "\"")
os.rename(dir_with_slash .. file,dir_with_slash .. newfile)
end
::continue::
end
end
function make_table(dir)
local file,dobj
local tab = {}
local dir_with_slash = dir
if dir_with_slash:match("/$") == nil then dir_with_slash = dir_with_slash .. "/" end
for file,dobj in lfs.dir(dir) do
local mode = lfs.attributes(file,"mode")
if mode == "directory" then
print(file .. " is a directory, skip\n")
goto continue
end
-- if file:match("^...%d%d%d%d%d%d") == nil then goto continue end
local number = file:sub(1,9)
local ext = file:match("%.[^%.]*$")
if ext == nil or ext:lower() == ".xlsx" then goto continue end
if tab[number] == nil then
tab[number] = {{file = dir_with_slash .. file,ext = ext}}
else
table.insert(tab[number],{file = dir_with_slash .. file,ext = ext})
end
::continue::
end
return tab
end
function write_tex(number,images)
local fp,msg = io.open(number .. ".tex","w")
if fp == nil then return false,msg end
fp:write(head)
for _,image in ipairs(images) do
if image.ext:lower() == ".pdf" then
fp:write("\\includepdf[pages=-]{" .. image.file .. "}\n")
else
fp:write("\\abincludeimage[angle=270]{" .. image.file .. "}\n")
end
end
fp:write(foot)
fp:close()
return true,nil
end
function execute_tex(texfile)
local r = os.execute(engine .. " " .. latexopt .. " " .. texfile .. ".tex")
local s = 0
if engine == "platex" or engine == "uplatex" then
s = os.execute("dvipdfmx " .. texfile)
end
os.remove(texfile .. ".log")
os.remove(texfile .. ".aux")
os.remove(texfile .. ".dvi")
if r + s == 0 then return true else return false end
end
function copy_file(from,to)
local infp = io.open(from,"rb")
local data = infp:read("*all")
infp:close()
local outfp = io.open(to,"wb")
outfp:write(data)
outfp:close()
end
local lfs = require "lfs"
make_style(style .. ".sty")
delete_spaces(target)
local t = make_table(target)
local report_num = 0
for _,_ in pairs(t) do report_num = report_num + 1 end
print("number of reports: " .. tostring(report_num) .. "\n")
local msg = ""
local i = 0
for num,ims in pairs(t) do
i = i + 1
print(i .. " th reports\n")
if do_not_use_latex_for_pdf_file and (#ims == 1) and (ims[1].ext:lower() == ".pdf") then
copy_file(ims[1].file,num .. ".pdf")
else
local b,m = write_tex(num,ims)
if b == false then msg = msg .. "error in " .. num .. ".tex, " .. m .. "\n" end
b = execute_tex(num)
if b == false then msg = msg .. "perhaps error occured when running latex for " .. num .. "\n" end
end
end
if msg ~= "" then print("\n\n" .. msg) end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment