DrishtiSharma commited on
Commit
aa2e930
·
verified ·
1 Parent(s): 7902b5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
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 resize the first page of the PDF for a compact preview.
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
- doc = fitz.open(pdf_path) # Open the PDF document
218
- first_page = doc[0] # Extract the first page
219
- pix = first_page.get_pixmap() # Get the pixmap (image) of the page
220
-
221
- # Resize the image manually using width and height scaling
222
- new_width = int(pix.width * scale_factor)
223
- new_height = int(pix.height * scale_factor)
224
- resized_pix = fitz.Pixmap(pix, 0) # Clone the pixmap
225
-
226
- # Manually downscale the image using `fitz.Pixmap` constructor
227
- resized_pix = fitz.Pixmap(fitz.csRGB, pix, new_width, new_height)
228
-
229
- # Save resized image temporarily
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