I would like to reduce the size of some PDF files. There are many ways to do so, but most of them don't work for my purposes. For example, pdftk, cpdf, and pdfoptsize all fail to reduce the sizes of my files. Ghostscript can reduce the file size, but only at an unacceptable cost in terms of legibility of the figures. There seem to be some great APIs for size reduction, but I don't want to pay. So I would like to automate the "Reduce File Size" option in Acrobat, which works well. Is there a way to do this in Python or from the command line?
I am running Windows 10 with Acrobat DC; I also have access to Acrobat X. I can set up a "Batch Processing" job in Acrobat, but even then, I would need to run it from Python or from the command line.
I can use the Acrobat API from Python, but I don't see how to use it to run the "Reduce File Size" command. I can set the PDSaveCollectGarbage
flag, but it doesn't help. Here is a minimal example of a Python script that opens and resaves a file -- it illustrates the extent of my knowledge in this area:
import os
from win32com.client.dynamic import Dispatch
src = os.path.abspath('original.pdf')
PDSaveFull = 0x01
PDSaveCollectGarbage = 0x20
SAVEFLAG = PDSaveFull | PDSaveCollectGarbage
try:
app = Dispatch("AcroExch.AVDoc")
if app.Open(src, src):
pddoc = app.GetPDDoc()
pddoc.Save(SAVEFLAG, os.path.abspath('./new.pdf'))
except Exception as e:
print(str(e))
finally:
app.Close(-1)