File size: 1,759 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
#!/usr/bin/env python3
"""Repair a damaged PDF using Ghostscript."""
import argparse, json, sys, subprocess, shutil
from pathlib import Path

def find_gs():
    for name in ['gs', 'gswin64c', 'gswin32c']:
        path = shutil.which(name)
        if path:
            return path
    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 repair(input_path, output_path):
    gs = find_gs()
    
    if not output_path:
        output_path = str(Path(input_path).with_stem(Path(input_path).stem + '_repaired'))
    
    cmd = [
        gs, '-o', output_path,
        '-sDEVICE=pdfwrite',
        '-dPDFSETTINGS=/prepress',
        '-dNOPAUSE', '-dQUIET', '-dBATCH',
        input_path
    ]
    
    result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
    if result.returncode != 0:
        raise RuntimeError(f"Ghostscript repair failed: {result.stderr}")
    
    return output_path

def main():
    parser = argparse.ArgumentParser(description='Repair a damaged PDF')
    parser.add_argument('--input', required=True)
    parser.add_argument('--output', required=True)
    args = parser.parse_args()
    try:
        result = repair(args.input, args.output)
        print(json.dumps({"success": True, "output": result, "message": "PDF repaired successfully"}))
    except Exception as e:
        print(json.dumps({"success": False, "output": "", "message": str(e)}))
        sys.exit(1)

if __name__ == '__main__':
    main()