Skip to content

Instantly share code, notes, and snippets.

@wizofe
Forked from smoitra87/python2pdf.sh
Last active August 15, 2019 15:04
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 wizofe/c89bfbfaae3f3fb2a5df164cd7786d8a to your computer and use it in GitHub Desktop.
Save wizofe/c89bfbfaae3f3fb2a5df164cd7786d8a to your computer and use it in GitHub Desktop.
Convert python files to pdf and concatenate all pdfs. Requires pygmentize, pdflatex and pdftk
#!/bin/sh
# Run as ./python2pdf.sh $(ls *.py)
# For Debian the requirements are
# sudo apt install pdftk python3-pygments texlive-latex-base texlive-fonts-recommended texlive-fonts-extra texlive-latex-extra
outdir=${HOME}/tmp
mkdir ${outdir}
allpdfs=""
for f in "$@" ; do
filename=$(basename "$f")
filename="${filename%.*}"
pygmentize -f tex -O title=$( echo $filename | tr '_' '-').py -O full -O style=default -o ${outdir}/$filename.tex $f
pdflatex -jobname=$filename -output-directory=${outdir} ${outdir}/$filename.tex
allpdfs="$allpdfs ${outdir}/$filename.pdf"
done
pdftk $allpdfs cat output ${outdir}/combined.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment