Skip to content

Instantly share code, notes, and snippets.

Created May 7, 2022 14:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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
"-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 = do gs_exe
read(`$gs_exe $gs_cmds`, String)
write(path, pdf)
return true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment