Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,7 +26,7 @@ import os
|
|
| 26 |
import requests
|
| 27 |
from urllib.parse import quote
|
| 28 |
from PIL import Image
|
| 29 |
-
import fitz # PyMuPDF
|
| 30 |
import tempfile
|
| 31 |
from reportlab.lib import colors
|
| 32 |
from reportlab.lib.pagesizes import letter, landscape
|
|
@@ -328,12 +328,22 @@ def extract_pdf_thumbnail(pdf_url, model_name, max_width=300, max_height=400):
|
|
| 328 |
tmp_path = tmp_file.name
|
| 329 |
|
| 330 |
# Open PDF and extract first page
|
| 331 |
-
pdf_document = fitz.open(tmp_path)
|
| 332 |
first_page = pdf_document[0]
|
| 333 |
|
| 334 |
# Render page as image (2x resolution for better quality)
|
| 335 |
mat = fitz.Matrix(2, 2)
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
|
| 338 |
# Convert to PIL Image
|
| 339 |
img_data = pix.tobytes("png")
|
|
|
|
| 26 |
import requests
|
| 27 |
from urllib.parse import quote
|
| 28 |
from PIL import Image
|
| 29 |
+
import fitz # type: ignore # PyMuPDF
|
| 30 |
import tempfile
|
| 31 |
from reportlab.lib import colors
|
| 32 |
from reportlab.lib.pagesizes import letter, landscape
|
|
|
|
| 328 |
tmp_path = tmp_file.name
|
| 329 |
|
| 330 |
# Open PDF and extract first page
|
| 331 |
+
pdf_document = fitz.open(tmp_path) # type: ignore
|
| 332 |
first_page = pdf_document[0]
|
| 333 |
|
| 334 |
# Render page as image (2x resolution for better quality)
|
| 335 |
mat = fitz.Matrix(2, 2)
|
| 336 |
+
# Try new API first, fallback to old API
|
| 337 |
+
try:
|
| 338 |
+
# New PyMuPDF API
|
| 339 |
+
pix = first_page.get_pixmap(matrix=mat)
|
| 340 |
+
except:
|
| 341 |
+
try:
|
| 342 |
+
# Alternative method
|
| 343 |
+
pix = first_page.get_pixmap(mat)
|
| 344 |
+
except:
|
| 345 |
+
# Fallback without matrix for very old versions
|
| 346 |
+
pix = first_page.get_pixmap()
|
| 347 |
|
| 348 |
# Convert to PIL Image
|
| 349 |
img_data = pix.tobytes("png")
|