This repository has been archived on 2020-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
namensschilder/server.py

62 lines
1.6 KiB
Python
Raw Normal View History

2019-03-27 20:30:59 +01:00
#!/usr/bin/env python3
from jinja2 import Template
from pyppeteer import launch
from quart import Quart, request, render_template, url_for, redirect
2019-03-27 20:30:59 +01:00
from subprocess import run
import asyncio
import os
app = Quart(__name__)
2019-03-27 20:30:59 +01:00
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"document.querySelector('#name').innerText = '{name}'")
await page.pdf(path=f"static/{name}.pdf", width="180px", height="60px")
await browser.close()
2019-03-27 20:30:59 +01:00
@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)