Thanks to @user202729 I was able to automate securing/encrypting pdf files via QPDF (Documentation) within LuaLaTeX.
During post-processing of the PDF in the wrapup_run phase, the output directory is read via Lua's pattern matching:
local outdir = nil
for i = 0, #arg, 1 do
if string.find(arg[i], "%-+output%-d[irectory]*%=?%s?") then
outdir = string.gsub(arg[i], "%-+output%-d[irectory]*%=?%s?", "")
end
end
Note: I cannot say exactly whether all argument formats are covered by this, at least all formats that @user202729 has posted here are recognised.
QPDF is then executed with the desired arguments:
os.execute((outdir ~= nil and "cd " .. outdir .. " &&" or "") .. "qpdf " .. tex.jobname .. ".pdf --encrypt userpw ownerpw 256 --accessibility=y --assemble=y --extract=y --form=y --modify-other=y --print=full -- " .. tex.jobname .. "_secure.pdf")
Full MWE (Windows 10):
\documentclass{book}
\usepackage{luacode}
\begin{luacode}
luatexbase.add_to_callback("wrapup_run", function()
local outdir = nil
for i = 0, #arg, 1 do
if string.find(arg[i], "%-+output%-d[irectory]%=?%s?") then
outdir = string.gsub(arg[i], "%-+output%-d[irectory]%=?%s?", "")
end
end
os.execute((outdir ~= nil and "cd " .. outdir .. " &&" or "") .. "qpdf " .. tex.jobname .. ".pdf --encrypt userpw ownerpw 256 --accessibility=y --assemble=y --extract=y --form=y --modify-other=y --print=full -- " .. tex.jobname .. "_secure.pdf")
end, "Callback to secure pdf")
\end{luacode}
\begin{document}
Hello World!
\end{document}