Spaces:
Sleeping
Sleeping
File size: 2,400 Bytes
80a3675 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #!/usr/bin/env python3
"""Compress PDF using Ghostscript."""
import argparse, json, sys, subprocess, shutil
from pathlib import Path
QUALITY_MAP = {
'low': '/prepress',
'medium': '/ebook',
'high': '/screen',
}
def find_gs():
"""Find Ghostscript executable."""
for name in ['gs', 'gswin64c', 'gswin32c']:
path = shutil.which(name)
if path:
return path
# Common paths
import platform
if platform.system() == 'Windows':
import glob
for pattern in ['C:/Program Files/gs/*/bin/gswin64c.exe', 'C:/Program Files (x86)/gs/*/bin/gswin32c.exe']:
matches = glob.glob(pattern)
if matches:
return matches[0]
return 'gs'
def compress(input_path, output_path, level='medium'):
gs = find_gs()
quality = QUALITY_MAP.get(level, '/ebook')
if not output_path:
output_path = str(Path(input_path).with_stem(Path(input_path).stem + '_compressed'))
cmd = [
gs, '-sDEVICE=pdfwrite', '-dCompatibilityLevel=1.4',
f'-dPDFSETTINGS={quality}',
'-dNOPAUSE', '-dQUIET', '-dBATCH',
f'-sOutputFile={output_path}',
input_path
]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
if result.returncode != 0:
raise RuntimeError(f"Ghostscript error: {result.stderr}")
return output_path
def main():
parser = argparse.ArgumentParser(description='Compress PDF')
parser.add_argument('--input', required=True)
parser.add_argument('--output', required=True)
parser.add_argument('--level', default='medium', choices=['low', 'medium', 'high'])
args = parser.parse_args()
try:
result = compress(args.input, args.output, args.level)
original_size = Path(args.input).stat().st_size
compressed_size = Path(result).stat().st_size
reduction = (1 - compressed_size / original_size) * 100 if original_size > 0 else 0
print(json.dumps({
"success": True, "output": result,
"message": f"PDF compressed by {reduction:.1f}% ({original_size} -> {compressed_size} bytes)"
}))
except Exception as e:
print(json.dumps({"success": False, "output": "", "message": str(e)}))
sys.exit(1)
if __name__ == '__main__':
main()
|