How to compress PDF files

Target audience: Users of PDF files
Used tools: GostScript
What’s the purpose: How to make PDF-files smaller for delivery over the net

When we use documents in PDF format and need send them by email, we often wish to make them smaller as they make our mail boxes full. This will be explanation of how to make them smaller …

At the beginning must download GhostScript from

http://www.portableapps.com/bitcrowd/chromic_pdf/issues/305

and install somewhere – may use d:\
Then copy batch file comprPDF.bat in D:\Ghostscript\App\bin – here is utility gswin64c.exe which will make the job with ‘compression’ – which is by the way not real compression but the final result is decreasing of file size with saving the duality of the file !!!

Ghostscript PDF batch compression :

https://stackoverflow.com/questions/46195795/ghostscript-pdf-batch-compression

Here is the batch file – it’s name is comprPDF.bat:

@echo off
setlocal EnableDelayedExpansion

rem ghostscript executable name
set "ghostscript=D:\UTILz\Ghostscript\App\bin\gswin64c"

rem directories to scan for files. Folder D:\PDF is better to be empty before start comprPDF.bat !!!
set "filesDir[0]=D:\PDF"
REM set "filesDir[1]=FOLDER2"
REM set "filesDir[2]=FOLDER3"

rem extension of files to be scanned
set "ext=pdf"

rem new file be creation or input file overwrite
set "createNewPDFs=0"
rem file prefix for new files (if they should be created)
set "filepre=compr_"

rem loop over all directories defined in filesDir array
for /f "tokens=2 delims==" %%d in ('set filesDir[') do (
   if exist "%%~d" (
      pushd "%%~d"
      rem loop over all files in all (sub)directories with given extension
      for /f "delims=*" %%f in ('dir "*.%ext%" /b /s /a:-d') do (
         if [%createNewPDFs%] EQU [1] (
            REM %ghostscript% -sDEVICE=pdfwrite -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile="%%~dpf%filepre%%%~nxf" "%%~f"
			%ghostscript% -q -dNOPAUSE -dBATCH -dSAFER -dOverPrint=/simulate -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=150 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=150 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=150 -sOutputFile="%%~dpf%filepre%%%~nxf" "%%~f"			
		) else (
            REM %ghostscript% -sDEVICE=pdfwrite -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile="%TEMP%\%%~nxf" "%%~f"
			%ghostscript% -q -dNOPAUSE -dBATCH -dSAFER -dOverPrint=/simulate -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=150 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=150 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=150 -sOutputFile="%TEMP%\%%~nxf" "%%~f"			
            for %%t in ("%TEMP%\%%~nxf") do ( set "newSize=%%~zt" )
            for %%t in ("%%~f") do ( set "oldSize=%%~zt" )
            if [!newSize!] LSS [!oldSize!] (
               rem new file is smaller --> overwrite
               move /y "%TEMP%\%%~nxf" "%%~f"
            ) else (
               rem new file is greater --> delete it of the temp dir
               del "%TEMP%\%%~nxf"
            )
         )
      )
      popd
   )
)  

FIRST must remove -dCompatibilityLevel=1.4 becouse this give an error :

(explanations are in

https://github.com/bitcrowd/chromic_pdf/issues/305
released v1.16.0 with removed -dCompatibilityLevel=1.4 switch)

When exporting to pdf, gs version 10.03.0 issues a warning,

GPL Ghostscript 10.03.0: Can't use Object streams before PDF 1.5, ignoring WriteObjStms directive
GPL Ghostscript 10.03.0: Can't use an XRef stream before PDF 1.5, ignoring WriteXRefStm directive

SECOND replace GhostScript command with command suggestited by Alexey Pavlunin :

%ghostscript% -q -dNOPAUSE -dBATCH -dSAFER -dSimulateOverprint=true -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=150 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=150 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=150 -sOutputFile=output.pdf input.pdf

The result in batch file will be :

REM %ghostscript% -sDEVICE=pdfwrite -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile="%%~dpf%filepre%%%~nxf" "%%~f"
			%ghostscript% -q -dNOPAUSE -dBATCH -dSAFER -dOverPrint=/simulate -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=150 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=150 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=150 -sOutputFile="%%~dpf%filepre%%%~nxf" "%%~f"			

and

REM %ghostscript% -sDEVICE=pdfwrite -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile="%TEMP%\%%~nxf" "%%~f"
			%ghostscript% -q -dNOPAUSE -dBATCH -dSAFER -dOverPrint=/simulate -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=150 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=150 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=150 -sOutputFile="%TEMP%\%%~nxf" "%%~f"			

BUT there is another corection :
instead
-dSimulateOverprint=true
must be replaced with
-dOverPrint=/simulate
because it gives error :

**** -dSimulateOverprint={true|false} is no longer supported.    ****
**** It has been replaced by -dOverprint={enable|disable|

But simple replacing with -dOverprint=enable give another error :

Invalid value for option -dOverprint=enable, use -sNAME= to define string constants

Instead

-dOverprint=enable

we must use

-dOverPrint=/simulate !!!

Now all is OK with batch file and the result from sample input PDF file with size 162 MB (169 873 501 bytes) is 13.6 MB (14 360 660 bytes) with no visible difference in quality !!!

Published
Categorized as PDF

Leave a comment

Your email address will not be published. Required fields are marked *