LuisBlanche commited on
Commit
3a40b82
1 Parent(s): 29f3572

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -11
app.py CHANGED
@@ -4,7 +4,6 @@ from io import BytesIO
4
  import mechanicalsoup
5
  import pandas as pd
6
  import requests
7
- from pdf2image import convert_from_bytes
8
  from reportlab.lib import colors
9
  from reportlab.lib.enums import TA_CENTER
10
  from reportlab.lib.pagesizes import A4
@@ -280,6 +279,23 @@ css = """
280
  }
281
  """
282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283
 
284
  def fetch_votes(deputy_name):
285
  pdfposter = PDFPoster(deputy_name)
@@ -299,15 +315,14 @@ def generate_poster(deputy_name, message_1, message_2, vote_list):
299
  pdfposter = PDFPoster(deputy_name)
300
  pdfposter.retrieve_deputy_data()
301
  pdfbuffer = pdfposter.generate_poster(vote_list, message_1, message_2)
302
- images = convert_from_bytes(pdfbuffer.getvalue())
303
-
304
- image_paths = []
305
- for i, image in enumerate(images):
306
- image_path = f"./static/{deputy_name}_page_{i+1}.png"
307
- image.save(image_path, "PNG")
308
- image_paths.append(image_path)
309
 
310
- return image_paths, pdfbuffer.getvalue()
 
 
 
311
 
312
 
313
  with gr.Blocks(css=css) as demo:
@@ -343,7 +358,7 @@ with gr.Blocks(css=css) as demo:
343
  )
344
 
345
  generate_button = gr.Button("Générer l'affiche ! ", scale=0)
346
- images_output = gr.Gallery(label="Image")
347
  pdf_output = gr.File(label="Télécharger le PDF")
348
 
349
  fetch_button.click(fn=fetch_votes, inputs=deputy_name, outputs=vote_list)
@@ -351,7 +366,7 @@ with gr.Blocks(css=css) as demo:
351
  generate_button.click(
352
  fn=generate_poster,
353
  inputs=[deputy_name, message_1, message_2, vote_list],
354
- outputs=[images_output, pdf_output],
355
  )
356
 
357
  demo.queue().launch()
 
4
  import mechanicalsoup
5
  import pandas as pd
6
  import requests
 
7
  from reportlab.lib import colors
8
  from reportlab.lib.enums import TA_CENTER
9
  from reportlab.lib.pagesizes import A4
 
279
  }
280
  """
281
 
282
+ def convert_pdf_to_image(pdf_buffer):
283
+ from reportlab.pdfgen.canvas import Canvas
284
+ from reportlab.graphics.renderPM import drawToPIL
285
+ from reportlab.graphics.shapes import Drawing
286
+
287
+ buffer = BytesIO()
288
+ canvas = Canvas(buffer, pagesize=A4)
289
+
290
+ # Draw some content on the canvas
291
+ drawing = Drawing(200, 100)
292
+ canvas.drawText("Preview of the PDF")
293
+ canvas.showPage()
294
+ canvas.save()
295
+
296
+ buffer.seek(0)
297
+ pil_image = drawToPIL(drawing)
298
+ return pil_image
299
 
300
  def fetch_votes(deputy_name):
301
  pdfposter = PDFPoster(deputy_name)
 
315
  pdfposter = PDFPoster(deputy_name)
316
  pdfposter.retrieve_deputy_data()
317
  pdfbuffer = pdfposter.generate_poster(vote_list, message_1, message_2)
318
+ pdf_path = f"./static/{deputy_name}.pdf"
319
+ with open(pdf_path, "wb") as f:
320
+ f.write(pdf_buffer.getvalue())
 
 
 
 
321
 
322
+ # Generate a preview image of the first page
323
+ pdf_buffer.seek(0)
324
+ preview_image = convert_pdf_to_image(pdf_buffer)
325
+ return preview_image, pdf_path
326
 
327
 
328
  with gr.Blocks(css=css) as demo:
 
358
  )
359
 
360
  generate_button = gr.Button("Générer l'affiche ! ", scale=0)
361
+ image_preview = gr.Image(label="Image")
362
  pdf_output = gr.File(label="Télécharger le PDF")
363
 
364
  fetch_button.click(fn=fetch_votes, inputs=deputy_name, outputs=vote_list)
 
366
  generate_button.click(
367
  fn=generate_poster,
368
  inputs=[deputy_name, message_1, message_2, vote_list],
369
+ outputs=[image_preview, pdf_output],
370
  )
371
 
372
  demo.queue().launch()