Skip to content

Instantly share code, notes, and snippets.

@asinghvi17
Created May 7, 2022 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asinghvi17/d9741701049c12c038e47bb6f02d8493 to your computer and use it in GitHub Desktop.
Save asinghvi17/d9741701049c12c038e47bb6f02d8493 to your computer and use it in GitHub Desktop.
PDF metadata writing script for Julia
# 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