Reducing the Size of Large PDFs
leancrew.com
leancrew.com
It cuts some of our PDFs to 10x smaller, mostly by removing unused fonts (but doubtless also some other magic).
The developer asks for donations for production use from those who can afford it.
https://github.com/pts/pdfsizeopt
Send donations to the author of pdfsizeopt:
https://flattr.com/submit/auto?user_id=pts&url=https://githu...
In particular, matplotlib plots that use points (markers) blow up in size quite a bit after processing through ghostscript, due to the way matplotlib re-uses spline information to draw the e.g. circles, where as ghostscript seemingly cannot / chooses not to (?). I recall something to do with xobject re-use...
I've also found that if you use type 42 fonts (helpful if submitting to a conference where the submission system doesn't accept type 3 fonts), matplotlib will not subset the font, resulting in increased file sizes.
So I use a similar ghostscript script, but one that also checks if the resulting file is actually smaller. If it's bigger, it just uses the original PDF. For files with lots of points, I've found that rasterizing just the points artist is a good solution (everything else in the plot is still vector), which allows for ghostscript to subset the type 42 fonts without the file-size explosion due to the points. Still, I wish there was a good way or script to e.g. just subset fonts in a PDF file, as well as processing a PDF to remove redundant fonts.
When including many PDF plots into a large LaTeX document, each PDF still comes with embedded fonts, which can increase the file size of the final PDF. Most of the fonts end up being duplicates. For this, I use a custom matplotlib backend that creates a PDF file with no text, together with a PGF file that specifies the position of each text. LaTeX then handles all the text rendering (which results in nice looking figures!), so each font is only included once in the final PDF.
Also I read that matplotlib 3.5 has some sort of improved support for type 42 subsetting. I haven't had a chance to try it out yet but this could be a welcome improvement!
And good point, the PGF works just as well (results should be identical), but since all the plot information has to be compiled by latex, it ends up ballooning the compilation time of the tex doc and the matplotlib PGF page suggests that you can run into memory issues as well. I was doing this for a thesis with 50+ plots and so still wanted compilation to be fast.
I've suggested this as an improvement to matplotlib, but unlikely to be merged since maybe it's a bit hacky (although it's very similar to what Inkscape's export to LaTeX option does): https://github.com/matplotlib/matplotlib/issues/22297 (the backend file can be found here: https://github.com/matplotlib/matplotlib/files/7921801/backe...)
And the gs script is below:
#!/bin/bash
set -e
set -o pipefail
if [ -z $1 ]; then
echo "Supply input output"
exit 1;
fi
if [ -z $2 ]; then
outfile="$(basename ${1} .pdf)-small.pdf"
if [ -f $outfile ]; then
echo "WARNING ${outfile} already exists."
echo "Supply input output"
exit 1;
fi
else
outfile="${2}"
fi
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -dQUIET -dDetectDuplicateImages=true -r150 -sOutputFile="${outfile}" "${1}"
pre_b=$(wc -c "${1}" | cut -d' ' -f1)
post_b=$(wc -c "${outfile}" | cut -d' ' -f1)
if (( $pre_b <= $post_b )); then
echo "Original is smaller ($pre_b -> $post_b). copying..."
cp "${1}" "${outfile}"
fiI probably wouldn't recommend doing this for anything serious (since it might negatively affect the a11y of the file), but in the right situations it can be a quick and simple way to massively reduce a PDF's filesize without noticeable side-effects.
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dColorConversionStrategy=/Gray -dProcessColorModel=/DeviceGray -dNOPAUSE -dQUIET -dBATCH -sOutputFile=file-out.pdf file-in.pdfTypically I batch downsize and convert to gray, then create a smaller PDF from these downsized/gray'ed scans.
I'll definitely take notes from that blog and from this HN thread for I'm sure some will come in handy.
qpdf --recompress-flate --object-streams=generate --compression-level=9 in.pdf out.pdf
I can never trust this kind of "simple tool" that magically reduces file sizes without telling me what it's doing. Personally, I think PDF is too complicated a format to have this kind of simple file-size-reduction tool.
Also (from https://www.adobe.com/acrobat/online/compress-pdf.html), they sell a tool that gives you more control:
”How do I compress a PDF without losing quality?
The Acrobat online PDF compressor balances an optimized file size against the expected quality of images, fonts, and other file content. Just drag and drop a PDF into the PDF compression tool above and let Acrobat reduce the size of your PDF files without compromising quality.
For more refined control of optimization settings, you can try Adobe Acrobat Pro DC for free for seven days. Acrobat Pro DC for the Mac or Windows operating system lets you customize PPI settings for color, grayscale, and monochrome image quality. You can also use PDF editor tools, edit scans with OCR functionality, convert PDFs to Microsoft PowerPoint and other file formats, convert PNGs and other image file formats, organize and rotate PDF pages, split PDFs, and more.”
High res images are going to look pretty good in PDF/print. SVG is going to look amazing, infinitely zoom able and sharp at every level.
You’d think raster images are bigger, right? Well, SVGs of graphs can be huge.. imagine a scatter plot of a million data points. That’s roughly same sized PNG as the scatter plot with 1000 points at the same resolution. But, a million points is going to be both slow to draw and take a lot of space!
PDF, IMO, is a horrible format, it's almost unparseable. I think we have to suck it up though and admit PDF won, and just deal with it by using tools like in OP's post to try to make the best of a bad situation.
GPL Ghostscript 9.10: Missing glyph CID=84, glyph=0054 in the font Helvetica-Light