main.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from io import BytesIO
  2. from pathlib import Path
  3. import cairo
  4. from nicegui import ui
  5. def generate_svg() -> str:
  6. output = BytesIO()
  7. surface = cairo.SVGSurface(output, 200, 150)
  8. draw(surface)
  9. surface.finish()
  10. return output.getvalue().decode('utf-8')
  11. def generate_pdf() -> bytes:
  12. output = BytesIO()
  13. surface = cairo.PDFSurface(output, 200, 150)
  14. draw(surface)
  15. surface.finish()
  16. return output.getvalue()
  17. def draw(surface: cairo.SVGSurface):
  18. context = cairo.Context(surface)
  19. context.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
  20. context.set_font_size(20)
  21. context.move_to(10, 40)
  22. context.show_text(f"{name_input.value}")
  23. context.move_to(10, 80)
  24. context.show_text(f"{email_input.value}")
  25. def update():
  26. preview.content = generate_svg()
  27. Path('output.pdf').write_bytes(generate_pdf())
  28. with ui.row().classes('items-stretch gap-12'):
  29. with ui.column():
  30. name_input = ui.input("Name", placeholder="Enter your name", on_change=update)
  31. email_input = ui.input("E-Mail", placeholder="Enter your E-Mail address", on_change=update)
  32. with ui.column():
  33. preview = ui.html().classes('w-[400px] h-[300px] border-2 border-gray-500')
  34. with ui.column():
  35. ui.button("Download PDF", on_click=lambda: ui.download('output.pdf'))\
  36. .bind_visibility_from(preview, 'content')
  37. ui.run()