Spaces:
Sleeping
Sleeping
DrishtiSharma
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -206,7 +206,7 @@ def download_pdf(patent_number):
|
|
206 |
|
207 |
def preview_pdf(pdf_path, scale_factor=0.5):
|
208 |
"""
|
209 |
-
Generate and
|
210 |
Args:
|
211 |
pdf_path (str): Path to the PDF file.
|
212 |
scale_factor (float): Factor to reduce the image size (default is 0.5).
|
@@ -214,23 +214,21 @@ def preview_pdf(pdf_path, scale_factor=0.5):
|
|
214 |
str: Path to the resized image preview.
|
215 |
"""
|
216 |
try:
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
#
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
temp_image_path = os.path.join(tempfile.gettempdir(), "pdf_preview_resized.png")
|
231 |
-
resized_pix.save(temp_image_path) # Save the resized image
|
232 |
return temp_image_path
|
233 |
-
|
234 |
except Exception as e:
|
235 |
st.error(f"Error generating PDF preview: {e}")
|
236 |
return None
|
|
|
206 |
|
207 |
def preview_pdf(pdf_path, scale_factor=0.5):
|
208 |
"""
|
209 |
+
Generate and display a resized preview of the first page of the PDF.
|
210 |
Args:
|
211 |
pdf_path (str): Path to the PDF file.
|
212 |
scale_factor (float): Factor to reduce the image size (default is 0.5).
|
|
|
214 |
str: Path to the resized image preview.
|
215 |
"""
|
216 |
try:
|
217 |
+
# Open the PDF and extract the first page
|
218 |
+
doc = fitz.open(pdf_path)
|
219 |
+
first_page = doc[0]
|
220 |
+
|
221 |
+
# Apply scaling using a transformation matrix
|
222 |
+
matrix = fitz.Matrix(scale_factor, scale_factor) # Scale down the image
|
223 |
+
pix = first_page.get_pixmap(matrix=matrix) # Generate scaled image
|
224 |
+
|
225 |
+
# Save the preview image
|
226 |
+
temp_image_path = os.path.join(tempfile.gettempdir(), "pdf_preview.png")
|
227 |
+
pix.save(temp_image_path)
|
228 |
+
|
229 |
+
doc.close()
|
|
|
|
|
230 |
return temp_image_path
|
231 |
+
|
232 |
except Exception as e:
|
233 |
st.error(f"Error generating PDF preview: {e}")
|
234 |
return None
|