awacke1 commited on
Commit
90a9144
·
verified ·
1 Parent(s): 0d25c7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -7
app.py CHANGED
@@ -6,7 +6,7 @@ from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
6
  from reportlab.lib import colors
7
  import io
8
  import re
9
- from pdf2image import convert_from_bytes
10
  from PIL import Image
11
 
12
  # Initial markdown content
@@ -249,6 +249,23 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
249
  buffer.seek(0)
250
  return buffer.getvalue()
251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
  # Streamlit UI
253
  st.title("🚀 Cutting-Edge ML Outline Generator")
254
 
@@ -270,13 +287,12 @@ if 'markdown_content' not in st.session_state:
270
  with st.spinner("Generating PDF..."):
271
  pdf_bytes = create_main_pdf(st.session_state.markdown_content, base_font_size, auto_size)
272
 
273
- # Display PDF preview as image
274
  st.subheader("PDF Preview")
275
- try:
276
- images = convert_from_bytes(pdf_bytes, dpi=200) # Convert PDF to image
277
- st.image(images[0], caption="PDF Page 1", use_column_width=True)
278
- except Exception as e:
279
- st.error(f"Failed to render PDF preview: {e}")
280
  st.info("Download the PDF to view it locally.")
281
 
282
  # Download button
 
6
  from reportlab.lib import colors
7
  import io
8
  import re
9
+ import fitz # PyMuPDF
10
  from PIL import Image
11
 
12
  # Initial markdown content
 
249
  buffer.seek(0)
250
  return buffer.getvalue()
251
 
252
+ # Function to convert PDF bytes to image using fitz (from backup.03302025-720pm.app.py)
253
+ def pdf_to_image(pdf_bytes):
254
+ try:
255
+ # Open PDF from bytes
256
+ doc = fitz.open(stream=pdf_bytes, filetype="pdf")
257
+ # Get the first page
258
+ page = doc[0]
259
+ # Render page to pixmap with a zoom factor for clarity
260
+ pix = page.get_pixmap(matrix=fitz.Matrix(2.0, 2.0)) # 2x zoom
261
+ # Convert to PIL Image
262
+ img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
263
+ doc.close()
264
+ return img
265
+ except Exception as e:
266
+ st.error(f"Failed to render PDF preview: {e}")
267
+ return None
268
+
269
  # Streamlit UI
270
  st.title("🚀 Cutting-Edge ML Outline Generator")
271
 
 
287
  with st.spinner("Generating PDF..."):
288
  pdf_bytes = create_main_pdf(st.session_state.markdown_content, base_font_size, auto_size)
289
 
290
+ # Display PDF preview using fitz
291
  st.subheader("PDF Preview")
292
+ pdf_image = pdf_to_image(pdf_bytes)
293
+ if pdf_image:
294
+ st.image(pdf_image, caption="PDF Page 1", use_column_width=True)
295
+ else:
 
296
  st.info("Download the PDF to view it locally.")
297
 
298
  # Download button