Browse Source

download PDF from memory

Falko Schindler 1 year ago
parent
commit
4212984700
1 changed files with 2 additions and 6 deletions
  1. 2 6
      examples/generate_pdf/main.py

+ 2 - 6
examples/generate_pdf/main.py

@@ -1,13 +1,10 @@
 #!/usr/bin/env python3
 from io import BytesIO
-from pathlib import Path
 
 import cairo
 
 from nicegui import ui
 
-PDF_PATH = Path('output.pdf')
-
 
 def generate_svg() -> str:
     output = BytesIO()
@@ -25,7 +22,7 @@ def generate_pdf() -> bytes:
     return output.getvalue()
 
 
-def draw(surface: cairo.SVGSurface) -> None:
+def draw(surface: cairo.Surface) -> None:
     context = cairo.Context(surface)
     context.select_font_face('Arial', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
     context.set_font_size(20)
@@ -37,7 +34,6 @@ def draw(surface: cairo.SVGSurface) -> None:
 
 def update() -> None:
     preview.content = generate_svg()
-    PDF_PATH.write_bytes(generate_pdf())
 
 
 with ui.row():
@@ -46,6 +42,6 @@ with ui.row():
         email = ui.input('E-Mail', placeholder='Enter your E-Mail address', on_change=update)
     preview = ui.html().classes('border-2 border-gray-500')
     update()
-    ui.button('Download PDF', on_click=lambda: ui.download(PDF_PATH)).bind_visibility_from(name, 'value')
+    ui.button('PDF', on_click=lambda: ui.download(generate_pdf(), 'output.pdf')).bind_visibility_from(name, 'value')
 
 ui.run()