Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,18 +14,64 @@ import zipfile
|
|
| 14 |
import io
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
import time
|
|
|
|
| 17 |
|
| 18 |
# อ่าน .env (ถ้ามี)
|
| 19 |
load_dotenv()
|
| 20 |
REPO_ID = os.getenv("REPO_ID", "protae5544/WorkerManagement")
|
| 21 |
ORIGINAL_REPO_ID = os.getenv("ORIGINAL_REPO_ID", None)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# ตั้งค่าฟอนต์
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# ฟังก์ชันแปลงวันที่เป็นภาษาไทย
|
| 31 |
def thai_date_time(timestamp):
|
|
@@ -109,19 +155,25 @@ def create_receipt_pdf(request_number, english_name, foreign_reference_number, i
|
|
| 109 |
c = canvas.Canvas(local_path, pagesize=A4)
|
| 110 |
|
| 111 |
# ใช้ฟอนต์ไทยถ้ามี ไม่งั้นใช้ Helvetica
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
| 116 |
c.setFont("Helvetica-Bold", 16)
|
| 117 |
-
font_available = False
|
| 118 |
|
| 119 |
# Header
|
| 120 |
c.drawString(2*cm, 28*cm, "กระทรวงแรงงาน" if font_available else "Ministry of Labour")
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
c.setFont("Helvetica", 12)
|
| 126 |
|
| 127 |
c.drawString(2*cm, 27.5*cm, "ใบเสร็จรับเงิน (ต้นฉบับ)" if font_available else "Receipt (Original)")
|
|
@@ -138,16 +190,22 @@ def create_receipt_pdf(request_number, english_name, foreign_reference_number, i
|
|
| 138 |
c.drawString(2*cm, 21*cm, "เลขประจำตัวนายจ้าง: 0415567000061" if font_available else "Employer ID: 0415567000061")
|
| 139 |
|
| 140 |
# รายการ
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
| 144 |
c.setFont("Helvetica-Bold", 12)
|
| 145 |
|
| 146 |
c.drawString(2*cm, 20*cm, "รายการ" if font_available else "Items")
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
| 151 |
c.setFont("Helvetica", 10)
|
| 152 |
|
| 153 |
c.drawString(2*cm, 19.5*cm, "1. ค่าธรรมเนียมในการยื่นคำขอ ฉบับละ 100 บาท: 100.00" if font_available else "1. Application fee 100 Baht each: 100.00")
|
|
@@ -170,9 +228,12 @@ def create_receipt_pdf(request_number, english_name, foreign_reference_number, i
|
|
| 170 |
print(f"Error creating QR code: {e}")
|
| 171 |
|
| 172 |
# Timestamp
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
| 176 |
c.setFont("Helvetica", 8)
|
| 177 |
|
| 178 |
print_time = thai_date_time(time.time())
|
|
@@ -417,11 +478,11 @@ with gr.Blocks(
|
|
| 417 |
lines=1
|
| 418 |
)
|
| 419 |
|
|
|
|
| 420 |
attachments = gr.File(
|
| 421 |
-
label="📎 แนบไฟล์
|
| 422 |
file_count="multiple",
|
| 423 |
-
file_types=[".jpg", ".png", ".pdf", ".jpeg"]
|
| 424 |
-
max_file_size="5MB"
|
| 425 |
)
|
| 426 |
|
| 427 |
submit_btn = gr.Button(
|
|
|
|
| 14 |
import io
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
import time
|
| 17 |
+
import requests
|
| 18 |
|
| 19 |
# อ่าน .env (ถ้ามี)
|
| 20 |
load_dotenv()
|
| 21 |
REPO_ID = os.getenv("REPO_ID", "protae5544/WorkerManagement")
|
| 22 |
ORIGINAL_REPO_ID = os.getenv("ORIGINAL_REPO_ID", None)
|
| 23 |
|
| 24 |
+
# ดาวน์โหลดฟอนต์จาก Space อื่น
|
| 25 |
+
def download_fonts():
|
| 26 |
+
try:
|
| 27 |
+
# ดาวน์โหลดฟอนต์จาก protae5544/worberkit2024poe
|
| 28 |
+
font_urls = {
|
| 29 |
+
'THSarabunNew.ttf': 'https://huggingface.co/spaces/protae5544/worberkit2024poe/resolve/main/fontCSS/subset-THSarabunNew.woff2',
|
| 30 |
+
'THSarabunNew-Bold.ttf': 'https://huggingface.co/spaces/protae5544/worberkit2024poe/resolve/main/fontCSS/subset-THSarabunNew-Bold.woff2'
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# ใช้ฟอนต์ที่มีอยู่แล้วในระบบ หรือดาวน์โหลดจาก Google Fonts
|
| 34 |
+
google_fonts = {
|
| 35 |
+
'THSarabunNew.ttf': 'https://fonts.gstatic.com/s/sarabun/v13/DtVjJx26TKEqsc-lWcmw-wKE7wM.ttf',
|
| 36 |
+
'THSarabunNew-Bold.ttf': 'https://fonts.gstatic.com/s/sarabun/v13/DtVmJx26TKEqsc-lWcmw5wq0hwOKBwXcb0A.ttf'
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
for filename, url in google_fonts.items():
|
| 40 |
+
if not os.path.exists(filename):
|
| 41 |
+
try:
|
| 42 |
+
response = requests.get(url, timeout=10)
|
| 43 |
+
if response.status_code == 200:
|
| 44 |
+
with open(filename, 'wb') as f:
|
| 45 |
+
f.write(response.content)
|
| 46 |
+
print(f"Downloaded font: {filename}")
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"Error downloading {filename}: {e}")
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"Error in download_fonts: {e}")
|
| 52 |
+
|
| 53 |
# ตั้งค่าฟอนต์
|
| 54 |
+
def setup_fonts():
|
| 55 |
+
try:
|
| 56 |
+
# ดาวน์โหลดฟอนต์ก่อน
|
| 57 |
+
download_fonts()
|
| 58 |
+
|
| 59 |
+
# ลงทะเบียนฟอนต์
|
| 60 |
+
if os.path.exists('THSarabunNew.ttf'):
|
| 61 |
+
pdfmetrics.registerFont(TTFont('THSarabun', 'THSarabunNew.ttf'))
|
| 62 |
+
print("Registered THSarabun font")
|
| 63 |
+
|
| 64 |
+
if os.path.exists('THSarabunNew-Bold.ttf'):
|
| 65 |
+
pdfmetrics.registerFont(TTFont('THSarabunBold', 'THSarabunNew-Bold.ttf'))
|
| 66 |
+
print("Registered THSarabunBold font")
|
| 67 |
+
|
| 68 |
+
return True
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"Warning: Could not load fonts: {e}")
|
| 71 |
+
return False
|
| 72 |
+
|
| 73 |
+
# เริ่มต้นฟอนต์
|
| 74 |
+
font_available = setup_fonts()
|
| 75 |
|
| 76 |
# ฟังก์ชันแปลงวันที่เป็นภาษาไทย
|
| 77 |
def thai_date_time(timestamp):
|
|
|
|
| 155 |
c = canvas.Canvas(local_path, pagesize=A4)
|
| 156 |
|
| 157 |
# ใช้ฟอนต์ไทยถ้ามี ไม่งั้นใช้ Helvetica
|
| 158 |
+
if font_available:
|
| 159 |
+
try:
|
| 160 |
+
c.setFont("THSarabunBold", 18)
|
| 161 |
+
except:
|
| 162 |
+
c.setFont("Helvetica-Bold", 16)
|
| 163 |
+
font_available = False
|
| 164 |
+
else:
|
| 165 |
c.setFont("Helvetica-Bold", 16)
|
|
|
|
| 166 |
|
| 167 |
# Header
|
| 168 |
c.drawString(2*cm, 28*cm, "กระทรวงแรงงาน" if font_available else "Ministry of Labour")
|
| 169 |
|
| 170 |
+
if font_available:
|
| 171 |
+
try:
|
| 172 |
+
c.setFont("THSarabun", 14)
|
| 173 |
+
except:
|
| 174 |
+
c.setFont("Helvetica", 12)
|
| 175 |
+
font_available = False
|
| 176 |
+
else:
|
| 177 |
c.setFont("Helvetica", 12)
|
| 178 |
|
| 179 |
c.drawString(2*cm, 27.5*cm, "ใบเสร็จรับเงิน (ต้นฉบับ)" if font_available else "Receipt (Original)")
|
|
|
|
| 190 |
c.drawString(2*cm, 21*cm, "เลขประจำตัวนายจ้าง: 0415567000061" if font_available else "Employer ID: 0415567000061")
|
| 191 |
|
| 192 |
# รายการ
|
| 193 |
+
if font_available:
|
| 194 |
+
try:
|
| 195 |
+
c.setFont("THSarabunBold", 14)
|
| 196 |
+
except:
|
| 197 |
+
c.setFont("Helvetica-Bold", 12)
|
| 198 |
+
else:
|
| 199 |
c.setFont("Helvetica-Bold", 12)
|
| 200 |
|
| 201 |
c.drawString(2*cm, 20*cm, "รายการ" if font_available else "Items")
|
| 202 |
|
| 203 |
+
if font_available:
|
| 204 |
+
try:
|
| 205 |
+
c.setFont("THSarabun", 12)
|
| 206 |
+
except:
|
| 207 |
+
c.setFont("Helvetica", 10)
|
| 208 |
+
else:
|
| 209 |
c.setFont("Helvetica", 10)
|
| 210 |
|
| 211 |
c.drawString(2*cm, 19.5*cm, "1. ค่าธรรมเนียมในการยื่นคำขอ ฉบับละ 100 บาท: 100.00" if font_available else "1. Application fee 100 Baht each: 100.00")
|
|
|
|
| 228 |
print(f"Error creating QR code: {e}")
|
| 229 |
|
| 230 |
# Timestamp
|
| 231 |
+
if font_available:
|
| 232 |
+
try:
|
| 233 |
+
c.setFont("THSarabun", 8)
|
| 234 |
+
except:
|
| 235 |
+
c.setFont("Helvetica", 8)
|
| 236 |
+
else:
|
| 237 |
c.setFont("Helvetica", 8)
|
| 238 |
|
| 239 |
print_time = thai_date_time(time.time())
|
|
|
|
| 478 |
lines=1
|
| 479 |
)
|
| 480 |
|
| 481 |
+
# แก้ไข File component - ลบ max_file_size
|
| 482 |
attachments = gr.File(
|
| 483 |
+
label="📎 แนบไฟล์",
|
| 484 |
file_count="multiple",
|
| 485 |
+
file_types=[".jpg", ".png", ".pdf", ".jpeg"]
|
|
|
|
| 486 |
)
|
| 487 |
|
| 488 |
submit_btn = gr.Button(
|