Spaces:
Sleeping
Sleeping
Update app.py
Browse files135 unmodified lines1361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941957 unmodified lines203204205206207208209210211212213214215216217218219220221222135 unmodified linesreturn None, f"❌ 簡化模式錯誤: {str(e)}"def convert_pdf_to_cmyk_profiled(input_path):"""使用 Ghostscript 轉換 PDF 為 CMYK"""# 檢查 Ghostscriptgs_path, gs_version = check_ghostscript()if gs_path is None:return None, """❌ 系統未安裝 Ghostscript請按照以下步驟安裝:1. 創建 packages.txt 檔案,內容:ghostscript2. Space 會自動重新建置"""# 生成輸出檔名base_name = Path(input_path).stemoutput_file = f"/tmp/{base_name}_cmyk.pdf"# 檢查 ICC Profile 是否存在icc_profile_paths = ["/usr/share/color/icc/JapanColor2011Coated.icc","/app/JapanColor2011Coated.icc","./JapanColor2011Coated.icc"]icc_profile = Nonefor path in icc_profile_paths:if os.path.exists(path):icc_profile = pathbreak# Ghostscript 命令command = [gs_path,"-dSAFER","-dBATCH","-dNOPAUSE","-dNOCACHE","-sDEVICE=pdfwrite","-sProcessColorModel=DeviceCMYK","-sColorConversionStrategy=CMYK","-dOverrideICC=true",f"-sOutputFile={output_file}",input_path]# 如果找到 ICC Profile,添加到命令中if icc_profile:command.insert(-2, f"-sOutputICCProfile={icc_profile}")profile_name = "Japan Color 2011 Coated"else:profile_name = "預設 CMYK"try:result = subprocess.run(7 unmodified linesif os.path.exists(output_file) and os.path.getsize(output_file) > 0:file_size_kb = round(os.path.getsize(output_file) / 1024, 2)return output_file, f"""✅ 轉換成功!📦 檔案大小: {file_size_kb} KB🔧 使用工具: Ghostscript {gs_version}🎨 色彩模式: CMYK (DeviceCMYK)📐 ICC Profile: {profile_name}"""else:return None, "❌ 轉換失敗:輸出檔案為空"except subprocess.TimeoutExpired:return None, "⏱️ 轉換超時:處理時間超過 120 秒"except subprocess.CalledProcessError as e:return None, f"❌ Ghostscript 錯誤:\n{e.stderr}"except Exception as e:return None, f"❌ 未預期的錯誤: {str(e)}"135 unmodified lines1361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811827 unmodified lines190191192193194195196197198199200201202203204205206207208209210211135 unmodified linesreturn None, f"❌ 簡化模式錯誤: {str(e)}"def convert_pdf_to_cmyk_profiled(input_path):"""使用 Ghostscript 和 .ps 設定檔轉換 PDF 為 CMYK (穩定方法)"""# 檢查 Ghostscriptgs_path, gs_version = check_ghostscript()if gs_path is None:return None, """❌ 系統未安裝 Ghostscript請按照以下步驟安裝:1. 創建 packages.txt 檔案,內容:ghostscript2. Space 會自動重新建置"""# 檢查必要的設定檔和 ICC Profile 是否存在settings_file = "cmyk_settings.ps"icc_profile = "JapanColor2011Coated.icc"if not os.path.exists(settings_file):return None, f"❌ 找不到色彩設定檔 {settings_file}"if not os.path.exists(icc_profile):return None, f"❌ 找不到 ICC Profile 檔案 {icc_profile}"# 生成輸出檔名base_name = Path(input_path).stemoutput_file = f"/tmp/{base_name}_cmyk_profiled.pdf"# 這是最穩定的 Ghostscript 命令command = [gs_path,"-dSAFER","-dBATCH","-dNOPAUSE","-sDEVICE=pdfwrite",# -I. 參數告訴 Ghostscript 在目前目錄尋找 .ps 和 .icc 檔案"-I.",f"-sOutputFile={output_file}",# 在輸入 PDF 之前載入我們的顏色設定檔settings_file,input_path]profile_name = "Japan Color 2011 Coated"try:result = subprocess.run(7 unmodified linesif os.path.exists(output_file) and os.path.getsize(output_file) > 0:file_size_kb = round(os.path.getsize(output_file) / 1024, 2)return output_file, f"""✅ 轉換成功!📦 檔案大小: {file_size_kb} KB🔧 使用工具: Ghostscript {gs_version}🎨 色彩模式: CMYK (DeviceCMYK)📐 ICC Profile: {profile_name}"""else:# 如果 Ghostscript 沒有報錯但檔案是空的error_message = result.stderr if result.stderr else "未知原因"return None, f"❌ 轉換失敗:輸出檔案為空。\nGS 訊息: {error_message}"except subprocess.TimeoutExpired:return None, "⏱️ 轉換超時:處理時間超過 120 秒"except subprocess.CalledProcessError as e:# 顯示更詳細的 Ghostscript 錯誤return None, f"❌ Ghostscript 執行錯誤:\n{e.stderr}"except Exception as e:return None, f"❌ 未預期的錯誤: {str(e)}"
app.py+22-33