Spaces:
Sleeping
Sleeping
| #!/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() | |