There are two scripts I use. One is called pdf2png
– it takes a list of PDF files and places the PNGs (with the same base filenames) on my Desktop. I could use an imgur CLI application to automatically upload these, but that would not put them under StackExchange's account.
#!/usr/bin/env bash
for pdf in ${@}; do
dir=$(dirname $pdf)
base=$(basename -s .pdf $pdf)
pdfcrop "${dir}/${base}.pdf" > /dev/null
convert -density 600 "${dir}/${base}-crop.pdf" "${HOME}/Desktop/$base.png" > /dev/null
done
Another one (that I just made after reading some of these) is a Python script that takes a list of TeX files and just does the same thing, but (obviously) compiles them with arara
first.
#!/usr/bin/env python
from __future__ import print_function
import os
import sys
import subprocess
import shutil
import tempfile
import atexit
if '--debug' in sys.argv:
logmsg = lambda *a: print(*a, file=sys.stderr)
else:
logmsg = lambda *a: None
logerr = logmsg
DEVNULL = open(os.devnull, 'w')
atexit.register(DEVNULL.close)
def runcmd(*args):
"""Run a command with arguments"""
logmsg('>>> Running %s' % ' '.join(args))
subprocess.check_call(
['/usr/bin/env'] + list(map(str, args)),
stdout = DEVNULL)
def compile(source):
"""Compile SOURCE and return the path to a PNG rendition of it."""
if not os.path.exists(source):
logerr('%s does not exist' % source)
return None
oldsource = os.path.abspath(source)
workdir = tempfile.mkdtemp()
try:
shutil.copy(source, workdir)
os.chdir(workdir)
basename = os.path.splitext(source)[0]
pdfname = basename + '.pdf'
pngname = basename + '.png'
runcmd('arara', source)
runcmd('pdfcrop', pdfname, pdfname)
runcmd('convert', '-density', 300, pdfname, pngname)
shutil.copy(pngname, os.path.dirname(source))
except subprocess.CalledProcessError:
pngname = source
finally:
shutil.rmtree(workdir)
return pngname
for png_file in map(compile, sys.argv[1:]):
print(png_file)
Again, piping the output to xargs <some imgur uploader>
would work, but you wouldn't be using StackExchange's account.
Regarding cruft files, I've developed a short yasnippet to help with that:
# -*- mode: snippet; require-final-newline: nil -*-
# name: sxtmp
# key: sxtmp
# --
% arara: pdflatex
% arara: clean: { files: [ `
(let ((basename (file-name-base
(or (buffer-file-name) ""))))
(mapconcat (lambda (ext) (concat basename "." ext))
'("aux" "log" "out") ", "))` ] }
\documentclass{article}
\pagenumbering{gobble}
\usepackage{mwe}
\begin{document}
$0
\end{document}
pdfcrop
andconvert
also work very well from the command line. also, use the option-trim
with convert. (the incantation i use is a bit different from what's given in that answer. if this is useful, i will provide a detailed answer when i can get back to my office; may not be for a few days on account of snow.)