main.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python3
  2. from io import BytesIO
  3. import cairo
  4. from nicegui import ui
  5. def generate_svg() -> str:
  6. output = BytesIO()
  7. surface = cairo.SVGSurface(output, 300, 200)
  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, 300, 200)
  14. draw(surface)
  15. surface.finish()
  16. return output.getvalue()
  17. def draw(surface: cairo.Surface) -> None:
  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(name.value)
  23. context.move_to(10, 80)
  24. context.show_text(email.value)
  25. def update() -> None:
  26. preview.content = generate_svg()
  27. with ui.row():
  28. with ui.column():
  29. name = ui.input('Name', placeholder='Enter your name', on_change=update)
  30. email = ui.input('E-Mail', placeholder='Enter your E-Mail address', on_change=update)
  31. preview = ui.html().classes('border-2 border-gray-500')
  32. update()
  33. ui.button('PDF', on_click=lambda: ui.download(generate_pdf(), 'output.pdf')).bind_visibility_from(name, 'value')
  34. ui.run()