62 lines
1.5 KiB
Python
Executable file
62 lines
1.5 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
from jinja2 import Template
|
|
from pyppeteer import launch
|
|
from quart import Quart, request, render_template, url_for, redirect
|
|
from subprocess import run
|
|
import asyncio
|
|
import os
|
|
|
|
app = Quart(__name__)
|
|
|
|
|
|
def run_check(*args, **kwargs):
|
|
return run(*args, **kwargs, check=True)
|
|
|
|
|
|
async def gen_pdf(name):
|
|
browser = await launch(args=["--no-sandbox"])
|
|
page = await browser.newPage()
|
|
await page.goto("file://" + os.path.join(os.getcwd(), "namensschild.html"))
|
|
await page.evaluate(f"setName('{name}')")
|
|
await page.pdf(path=f"static/{name}.pdf", width="180px", height="60px")
|
|
await browser.close()
|
|
|
|
|
|
@app.route("/")
|
|
async def index():
|
|
return await render_template("index.html.j2")
|
|
|
|
|
|
@app.route("/generate")
|
|
async def generate():
|
|
name = request.args["name"]
|
|
await gen_pdf(name)
|
|
run_check(
|
|
["inkscape", "-T", "-E", f"static/{name}.eps", f"static/{name}.pdf",]
|
|
)
|
|
run_check(
|
|
[
|
|
"pstoedit",
|
|
"-q",
|
|
"-flat",
|
|
"0.0001",
|
|
"-dt",
|
|
"-f",
|
|
"dxf: -polyaslines -mm",
|
|
f"static/{name}.eps",
|
|
f"static/{name}.dxf",
|
|
]
|
|
)
|
|
|
|
with open("namensschild.scad.j2", "r") as f:
|
|
template = Template(f.read())
|
|
with open(f"static/{name}.scad", "w") as f:
|
|
f.write(template.render(name=name))
|
|
run_check(["openscad", "-o", f"static/{name}.stl", f"static/{name}.scad"])
|
|
|
|
return redirect(url_for("static", filename=f"{name}.stl"), code=302)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=True)
|