From 7d3de95c7080490b53e93db1951803c09367b882 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 24 Oct 2023 19:04:42 -0400 Subject: [PATCH] Use ghostscript to compress PDF output on RTD --- .readthedocs.yaml | 4 ++++ scripts/rtd-compress-pdf.sh | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100755 scripts/rtd-compress-pdf.sh diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 30ea36d7..97731e1e 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -11,6 +11,10 @@ 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..c03402b0 --- /dev/null +++ b/scripts/rtd-compress-pdf.sh @@ -0,0 +1,27 @@ +#!/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 + # find the already built PDF + pdf_name=$(ls "$PDF_DIR") + + # move it into a temp directory + # readthedocs expects only one PDF output in the directory + # see https://docs.readthedocs.io/en/stable/reference/environment-variables.html#envvar-READTHEDOCS_OUTPUT + temp=$(mktemp -d) + mv "$PDF_DIR/$OUTPUT_FILENAME" "$temp" + + # actually compress the PDF + gs -sDEVICE=pdfwrite \ + -dCompatibilityLevel=1.4 \ + -dPDFSETTINGS=/ebook \ + -dNOPAUSE \ + -dBATCH -dColorImageResolution="$DPI" \ + -sOutputFile="$PDF_DIR/$pdf_name" "$temp/$pdf_name" +fi