DrishtiSharma commited on
Commit
2495b88
·
verified ·
1 Parent(s): f5c7a63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -8
app.py CHANGED
@@ -204,14 +204,28 @@ def download_pdf(patent_number):
204
  st.error(f"Failed to download patent PDF: {e}")
205
  st.stop()
206
 
207
- def preview_pdf(pdf_path):
208
- """Generate and display the first page of the PDF as an image."""
 
 
 
 
 
 
 
209
  try:
210
  doc = fitz.open(pdf_path) # Open PDF
211
- first_page = doc[0] # Extract the first page
212
- pix = first_page.get_pixmap() # Render page to a Pixmap (image)
213
- temp_image_path = os.path.join(tempfile.gettempdir(), "pdf_preview.png")
214
- pix.save(temp_image_path) # Save the image temporarily
 
 
 
 
 
 
 
215
  return temp_image_path
216
  except Exception as e:
217
  st.error(f"Error generating PDF preview: {e}")
@@ -268,10 +282,10 @@ if __name__ == "__main__":
268
  # Generate PDF preview only if not already displayed
269
  if not st.session_state.get("pdf_preview_displayed", False):
270
  st.write("🖼️ Generating PDF preview...")
271
- preview_image_path = preview_pdf(pdf_path)
272
  if preview_image_path:
273
  st.session_state.pdf_preview = preview_image_path
274
- st.image(preview_image_path, caption="First Page Preview", use_container_width=True)
275
  st.session_state["pdf_preview_displayed"] = True
276
  else:
277
  st.warning("Failed to generate PDF preview.")
 
204
  st.error(f"Failed to download patent PDF: {e}")
205
  st.stop()
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).
213
+ Returns:
214
+ str: Path to the resized image preview.
215
+ """
216
  try:
217
  doc = fitz.open(pdf_path) # Open PDF
218
+ first_page = doc[0] # Get the first page
219
+ pix = first_page.get_pixmap() # Render page to Pixmap (image)
220
+
221
+ # Resize image using scale factor
222
+ new_width = int(pix.width * scale_factor)
223
+ new_height = int(pix.height * scale_factor)
224
+ resized_pix = fitz.Pixmap(pix, new_width, new_height)
225
+
226
+ # Save resized image temporarily
227
+ temp_image_path = os.path.join(tempfile.gettempdir(), "pdf_preview_resized.png")
228
+ resized_pix.save(temp_image_path)
229
  return temp_image_path
230
  except Exception as e:
231
  st.error(f"Error generating PDF preview: {e}")
 
282
  # Generate PDF preview only if not already displayed
283
  if not st.session_state.get("pdf_preview_displayed", False):
284
  st.write("🖼️ Generating PDF preview...")
285
+ preview_image_path = preview_pdf(pdf_path, scale_factor=0.5) # Added scale_factor
286
  if preview_image_path:
287
  st.session_state.pdf_preview = preview_image_path
288
+ st.image(preview_image_path, caption="First Page Preview", use_column_width=False)
289
  st.session_state["pdf_preview_displayed"] = True
290
  else:
291
  st.warning("Failed to generate PDF preview.")