Skip to content

Instantly share code, notes, and snippets.

@iliyang
Created June 23, 2024 21:43
Show Gist options
  • Save iliyang/b087f61389a076fd5df210ffe02987d7 to your computer and use it in GitHub Desktop.
Save iliyang/b087f61389a076fd5df210ffe02987d7 to your computer and use it in GitHub Desktop.
A latexmkrc file to compress PDFs with non-JPEG images on Overleaf after compilation
#!/usr/bin/perl
## Put this (Perl) file in the root of your Overleaf project. It overrides the
## `pdflatex` compilation command to run GhostScript as a post-process which will
## compress non-JPEG images to JPEG in the output PDF file. The JPEG quality
## settings are set to high to minimize the loss of image quality. This should
## work well even for images that contain a lot of high-frequency noise.
## To disable, simply rename the file to something other than latexmkrc.
##
## Created by Iliyan Georgiev.
## Quality settings
$PDF_VERSION = 1.4;
$IMG_DOWNSAMPLE = 'true';
$IMG_DOWNSAMPLE_DPI = 600;
$IMG_DOWNSAMPLE_THRESHOLD = 1.0;
$IMG_COMPRESSION_QFACTOR = 0.15; # lower is better (try 0.08 for higher quality)
$PASSTHROUGH_JPEGS = 'true';
## Don't modify these, they are specified by Overleaf
$GS_INPUT_FILE = '%B.pdf';
$GS_OUTPUT_FILE = '%B-embed.pdf';
## You can try setting below: -dNEWPDF=true
## This fails in the current GhostScript v9.55 on Overleaf
$GS_COMMAND = "gs
-sDEVICE=pdfwrite
-sOutputFile=$GS_OUTPUT_FILE
-dCompatibilityLevel=$PDF_VERSION
-dPDFSETTINGS=/default
-dFastWebView=true
-dEmbedAllFonts=true
-dSubsetFonts=true
-dCompressFonts=true
-dDetectDuplicateImages=true
-dDoThumbnails=false
-dAutoRotatePages=/None
-dDownsampleColorImages=$IMG_DOWNSAMPLE
-dColorImageDownsampleType=/Bicubic
-dColorImageDownsampleThreshold=$IMG_DOWNSAMPLE_THRESHOLD
-dColorImageResolution=$IMG_DOWNSAMPLE_DPI
-dDownsampleGrayImages=$IMG_DOWNSAMPLE
-dGrayImageDownsampleType=/Bicubic
-dGrayImageDownsampleThreshold=$IMG_DOWNSAMPLE_THRESHOLD
-dGrayImageResolution=$IMG_DOWNSAMPLE_DPI
-dDownsampleMonoImages=$IMG_DOWNSAMPLE
-dMonoImageDownsampleType=/Subsample
-dMonoImageDownsampleThreshold=$IMG_DOWNSAMPLE_THRESHOLD
-dMonoImageResolution=$IMG_DOWNSAMPLE_DPI
-dConvertCMYKImagesToRGB=true
-dColorImageFilter=/DCTEncode
-dGrayImageFilter=/DCTEncode
-dMonoImageFilter=/CCITTFaxEncode
-dColorConversionStrategy=/LeaveColorUnchanged
-dPassThroughJPEGImages=$PASSTHROUGH_JPEGS
-dProcessColorModel=/DeviceRGB
-dRenderIntent=1
-dDefaultRenderingIntent=/Perceptual
-dUseArtBox
-dNEWPDF=false
-dNOPAUSE
-dBATCH
-c \"<< /ColorACSImageDict << /VSamples [ 1 1 1 1 ] /HSamples [ 1 1 1 1 ] /QFactor $IMG_COMPRESSION_QFACTOR /Blend 1 /ColorTransform 1 >> >> setdistillerparams\"
-f $GS_INPUT_FILE";
## Remove new-line characters from GhostScript command
$GS_COMMAND =~ s/\n//g;
## Set the pdflatex command to be used by Overleaf
$pdflatex = "pdflatex %O %S; $GS_COMMAND; mv %B-embed.pdf %B.pdf";
## For debugging
# print "$GS_COMMAND\n";
# print "$pdflatex\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment