3

I was wondering whether it is possible to protect pdf files (e.g. control printing/copying/... access) with LuaLaTeX without using third-party software like QPDF or those listed in the pdfcrypt package.

Some older post says its possible with the \special command, but its example uses XeTeX and while testing with LuaLaTeX its not really working as expected. They indeed say probably there is no equivalent for LuaLaTeX:

\special{pdf:encrypt ownerpw (abc) userpw (xyz) length 128 perm 2052}
\documentclass{article}
\begin{document}
This is a test.
\end{document}

Is it maybe possible with LuaLaTeXs backend primitives like \pdfextension?

I'd be grateful if someone can show me a MWE if its possible with LuaLaTeX only.

Thanks,
Dave

Dave
  • 111

1 Answers1

3

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}

Dave
  • 111