Skip to content

Commit

Permalink
Use ghostscript to compress PDF output on RTD
Browse files Browse the repository at this point in the history
  • Loading branch information
abidingabi committed Oct 24, 2023
1 parent eef780c commit 7d3de95
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ build:
python: "3.11"
apt_packages:
- graphviz
- ghostscript
jobs:
post_build:
- ./scripts/rtd-compress-pdf.sh

formats:
- pdf
Expand Down
27 changes: 27 additions & 0 deletions scripts/rtd-compress-pdf.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7d3de95

Please sign in to comment.