Created
May 7, 2022 14:27
-
-
Save asinghvi17/d9741701049c12c038e47bb6f02d8493 to your computer and use it in GitHub Desktop.
PDF metadata writing script for Julia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Anshul Singhvi | |
# Works as of 20220507 | |
# Basically, the function is used to edit the metadata of a PDF using Ghostscript | |
# This could be made pure-Julian with the right logic (searching for keywords, etc.) | |
# but that is annoying to do. | |
using Ghostscript_jll | |
const AUTHOR = Ref{String}("Anshul Singhvi") | |
function insert_pdf_description( | |
file, desc::String; | |
title = splitext(splitdir(path)[2])[1], | |
author = AUTHOR[], | |
keywords = [], | |
additional_gs_flags = String[] | |
) | |
@assert endswith(file, ".pdf") | |
path = abspath(file) | |
gs_options = Cmd(vcat(additional_gs_flags, [ | |
"-dBATCH", # run ghostscript in batch mode | |
"-dNOPAUSE", # run without pause, exit immediately after complete | |
"-dQUIET", # no output to stdout | |
"-dProcessDSCComments=false", # try to maintain metadata (this fails) see https://bugs.ghostscript.com/show_bug.cgi?id=693400 | |
"-sDEVICE=pdfwrite", # use the `pdfwrite` device | |
"-sOutputFile=-", # output to stdout | |
])) | |
gs_cmds = ``` $gs_options -f $path -c "[ /Subject ($desc) /Keywords ($(join(keywords, ", "))) /DOCINFO pdfmark"``` | |
pdf = Ghostscript_jll.gs() do gs_exe | |
read(`$gs_exe $gs_cmds`, String) | |
end | |
write(path, pdf) | |
return true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment