Created
May 28, 2025 12:34
-
-
Save kolibril13/e3a52d7a3a2d64f66a67a59f5bb685c9 to your computer and use it in GitHub Desktop.
create a pdf via marimo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /// script | |
# requires-python = ">=3.13" | |
# dependencies = [ | |
# "marimo", | |
# "typst==0.13.2", | |
# ] | |
# /// | |
import marimo | |
__generated_with = "0.13.13" | |
app = marimo.App(width="columns") | |
@app.cell(column=0) | |
def _(): | |
import random | |
from datetime import datetime | |
from pathlib import Path | |
import tempfile | |
import typst | |
import marimo as mo | |
return Path, datetime, mo, random, tempfile, typst | |
@app.cell | |
def _(Path, datetime, random, tempfile, typst): | |
# Generate invoice data | |
invoice_number = f"{random.randint(10, 99)}/{datetime.now().year}" | |
amount = random.choice([450, 700, 850, 1200]) | |
today = datetime.now() | |
# Use system temp directory | |
temp_dir = Path(tempfile.gettempdir()) | |
typ_path = temp_dir / "simple_typst.typ" | |
pdf_path = temp_dir / "simple_typst_output.pdf" | |
# Typst source content | |
typst_content = f""" | |
#import "@preview/classy-german-invoice:0.3.1": invoice | |
#show: invoice( | |
"{invoice_number}", | |
datetime(year: {today.year}, month: {today.month}, day: {today.day}), | |
( | |
( | |
description: "Beratung zu ABC", | |
price: {amount}, | |
), | |
), | |
( | |
name: "Max Mustermann", | |
street: "Beispielstraße 42", | |
zip: "12345", | |
city: "Musterstadt", | |
tax_nr: "00/000/00000", | |
), | |
( | |
name: "Firma Beispiel GmbH", | |
street: "Hauptstraße 1", | |
zip: "54321", | |
city: "Beispielhausen", | |
), | |
( | |
name: "Max Mustermann", | |
bank: "Musterbank", | |
iban: "DE89370400440532013000", | |
bic: "MUSTDEFFXXX", | |
gender: (account_holder: "Kontoinhaber") | |
), | |
vat: 0.0, | |
kleinunternehmer: true, | |
) | |
""" | |
# Write the Typst file | |
typ_path.write_text(typst_content) | |
# Compile to PDF | |
pdf_bytes = typst.compile(str(typ_path)) | |
# Save PDF | |
pdf_path.write_bytes(pdf_bytes) | |
return (pdf_path,) | |
@app.cell | |
def _(): | |
return | |
@app.cell(column=1) | |
def _(mo, pdf_path): | |
mo.pdf(src=pdf_path) | |
return | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment