diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 30ea36d7..e16a7f3f 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -11,6 +11,9 @@ build: python: "3.11" apt_packages: - graphviz + - ghostscript + jobs: + post_build: ./scripts/rtd-compress-pdf.sh formats: - pdf diff --git a/scripts/rtd-compress-pdf.sh b/scripts/rtd-compress-pdf.sh new file mode 100755 index 00000000..c7a3c8e8 --- /dev/null +++ b/scripts/rtd-compress-pdf.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env sh + +# Compresses the PDF output using ghostscript on Read the Docs. + +OUTPUT_FILENAME="gm0.pdf" +DPI=300 +PDF_DIR="$READTHEDOCS_OUTPUT/pdf" + +# Check if the PDF build is enabled based on if the PDF output directory exists +if [ -d "$PDF_DIR" ]; then + existing_pdf=$(ls "$PDF_DIR") + + # actually compress the PDF + gs -sDEVICE=pdfwrite \ + -dCompatibilityLevel=1.4 \ + -dPDFSETTINGS=/ebook \ + -dNOPAUSE \ + -dBATCH -dColorImageResolution="$DPI" \ + -sOutputFile="$PDF_DIR/$OUTPUT_FILENAME" "$PDF_DIR/$existing_pdf" + + # readthedocs expects only one PDF output in the directory + # see https://docs.readthedocs.io/en/stable/reference/environment-variables.html#envvar-READTHEDOCS_OUTPUT + rm "$PDF_DIR/$existing_pdf" +fi