使用 LuaLaTeX 進行 PDF 安全/加密

使用 LuaLaTeX 進行 PDF 安全/加密

我想知道是否可以使用 LuaLaTeX 保護 pdf 檔案(例如控制列印/複製/...存取)不使用第三方軟體喜歡量子PDF或那些列在pdf密碼包裹。

有些年紀大了郵政說可以使用該\special命令,但其範例使用 XeTeX,並且在使用 LuaLaTeX 進行測試時,它並沒有真正按預期工作。他們確實說可能沒有 LuaLaTeX 的等價物:

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

LuaLaTeXs 後端原語是否可能\pdfextension

如果有人可以向我展示 MWE(如果只使用 LuaLaTeX 就可以),我將不勝感激。

謝謝,
戴夫

答案1

謝謝@用戶202729我能夠透過以下方式自動保護/加密 pdf 文件量子PDF文件) 在 LuaLaTeX 中。

在 PDF 的後處理過程中包裝運行階段,透過Lua的模式匹配讀取輸出目錄:

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

筆記:我不能確切地說是否涵蓋了所有參數格式,至少是所有格式@用戶202729已發布這裡被認可。

然後使用所需的參數執行 QPDF:

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")

完整 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}

相關內容