cdupland commited on
Commit
5adde2d
·
verified ·
1 Parent(s): 6b989e1

Display PDF page per page

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +15 -10
src/streamlit_app.py CHANGED
@@ -1,4 +1,5 @@
1
  import streamlit as st
 
2
  import base64
3
  import tempfile
4
  import os
@@ -6,6 +7,7 @@ from mistralai import Mistral
6
  from PIL import Image
7
  import io
8
  from dotenv import load_dotenv
 
9
 
10
  # Configuration de la page - DOIT être la première commande Streamlit
11
  st.set_page_config(page_title="OCR Facture avec Mistral", layout="wide")
@@ -167,16 +169,19 @@ def process_ocr(client, document_source):
167
  include_image_base64=True
168
  )
169
 
170
- def display_pdf(content):
171
- """
172
- Displays a PDF in Streamlit using an iframe.
173
-
174
- Args:
175
- content (bytes): The content of the PDF file.
176
- """
177
- base64_pdf = base64.b64encode(content).decode("utf-8")
178
- pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf"></iframe>'
179
- st.markdown(pdf_display, unsafe_allow_html=True)
 
 
 
180
 
181
  def main():
182
  """
 
1
  import streamlit as st
2
+ import streamlit.components.v1 as components
3
  import base64
4
  import tempfile
5
  import os
 
7
  from PIL import Image
8
  import io
9
  from dotenv import load_dotenv
10
+ from pdf2image import convert_from_bytes
11
 
12
  # Configuration de la page - DOIT être la première commande Streamlit
13
  st.set_page_config(page_title="OCR Facture avec Mistral", layout="wide")
 
169
  include_image_base64=True
170
  )
171
 
172
+ def display_pdf(content: bytes):
173
+ try:
174
+ images = convert_from_bytes(content)
175
+ for i, image in enumerate(images):
176
+ st.image(image, caption=f"Page {i+1}", use_container_width=True)
177
+ except Exception as e:
178
+ st.error(f"Impossible d'afficher le PDF : {e}")
179
+ st.download_button(
180
+ label="📥 Télécharger le PDF",
181
+ data=content,
182
+ file_name="document.pdf",
183
+ mime="application/pdf"
184
+ )
185
 
186
  def main():
187
  """