Compare commits

..
4 Commits
Author SHA1 Message Date
Simon Zeyer d816c9d302 fix removed file on print hook 2024-10-16 18:23:22 +00:00
Simon Zeyer a4006721d3 sachverhalt as message 2024-10-16 14:09:08 +00:00
Simon Zeyer 47c1c5ceb8 print pdf with chrome 2024-10-16 13:36:43 +00:00
Simon Zeyer b82d1bed1f forgot requirements 2024-10-16 12:15:33 +00:00
5 changed files with 45 additions and 23 deletions
+20 -1
View File
@@ -46,6 +46,7 @@ RUN apt-get --yes --force-yes install ca-certificates cups cups-filters libcups
WORKDIR /usr/src/app WORKDIR /usr/src/app
EXPOSE 631 EXPOSE 631
EXPOSE 5000
# Add user and disable sudo password checking # Add user and disable sudo password checking
RUN useradd \ RUN useradd \
--groups=sudo,lp,lpadmin \ --groups=sudo,lp,lpadmin \
@@ -56,11 +57,29 @@ RUN useradd \
print \ print \
&& sed -i '/%sudo[[:space:]]/ s/ALL[[:space:]]*$/NOPASSWD:ALL/' /etc/sudoers && sed -i '/%sudo[[:space:]]/ s/ALL[[:space:]]*$/NOPASSWD:ALL/' /etc/sudoers
# Print PDF
RUN apt-get update && apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg \
--no-install-recommends \
&& curl -sSL https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list \
&& apt-get update && apt-get install -y \
google-chrome-stable \
--no-install-recommends
# It won't run from the root user.
RUN groupadd chrome && useradd -g chrome -s /bin/bash -G audio,video chrome \
&& mkdir -p /home/chrome && chown -R chrome:chrome /home/chrome
COPY requirements.txt ./ COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
RUN pip install --no-cache-dir waitress
COPY ./app . COPY ./app .
COPY *.crt /usr/local/share/ca-certificates/ COPY *.crt /usr/local/share/ca-certificates/
RUN update-ca-certificates RUN update-ca-certificates
CMD [ "sh","-c","/etc/init.d/cups start && python3 /usr/src/app/app.py" ] CMD [ "sh","-c","/etc/init.d/cups start && waitress-serve --port=5000 app:app && python3 exchange_connect.py" ]
+2 -2
View File
@@ -132,5 +132,5 @@ def files():
html = html + '</ul><form method="post" enctype="multipart/form-data"><input type="file" name="file"><input type=submit value=Upload></form>' html = html + '</ul><form method="post" enctype="multipart/form-data"><input type="file" name="file"><input type=submit value=Upload></form>'
return html return html
if __name__ == "__main__": # if __name__ == "__main__":
app.run(port=5000) # app.run(port=5000, host='0.0.0.0')
+21 -19
View File
@@ -2,10 +2,9 @@ import os
import logging import logging
import requests import requests
import cups import cups
from weasyprint import HTML, CSS
from weasyprint.text.fonts import FontConfiguration
from requests.adapters import Retry from requests.adapters import Retry
import uuid import uuid
import subprocess
retries = Retry(total=5, retries = Retry(total=5,
backoff_factor=0.1, backoff_factor=0.1,
@@ -40,8 +39,8 @@ def alarminator_api(parsed_body: dict):
req_string +="&alarmtime={}".format(parsed_body['Einsatzbeginn(Soll)'].split(" ")[1]) req_string +="&alarmtime={}".format(parsed_body['Einsatzbeginn(Soll)'].split(" ")[1])
if 'Auftragsnummer' in parsed_body and not (os.environ.get('IS_DEV') and os.environ.get('IS_DEV') == "True"): if 'Auftragsnummer' in parsed_body and not (os.environ.get('IS_DEV') and os.environ.get('IS_DEV') == "True"):
req_string +="&operationnumber={}".format(parsed_body['Auftragsnummer']) req_string +="&operationnumber={}".format(parsed_body['Auftragsnummer'])
if 'Notfallgeschehen' in parsed_body: if 'Sachverhalt' in parsed_body:
req_string +="&message={}".format(parsed_body['Notfallgeschehen']) req_string +="&message={}".format(parsed_body['Sachverhalt'])
if 'Einsatzziel' in parsed_body: if 'Einsatzziel' in parsed_body:
if 'Stadt' in parsed_body['Einsatzziel']: if 'Stadt' in parsed_body['Einsatzziel']:
req_string +="&community={}".format(parsed_body['Einsatzziel']['Stadt']) req_string +="&community={}".format(parsed_body['Einsatzziel']['Stadt'])
@@ -69,9 +68,9 @@ def alarminator_api(parsed_body: dict):
gear.append(r['Ressourcen']) gear.append(r['Ressourcen'])
req_string +="&gear={}".format(';'.join(gear)) req_string +="&gear={}".format(';'.join(gear))
req_string +="&district={}".format('district') # req_string +="&district={}".format('district')
req_string +="&floor={}".format('floor') # req_string +="&floor={}".format('floor')
req_string +="&section={}".format('section') # req_string +="&section={}".format('section')
req_string +="&keywordRaw={}".format(parsed_body['Einsatzstichwort']) req_string +="&keywordRaw={}".format(parsed_body['Einsatzstichwort'])
#req_string +="&keywordId={}".format('keywordId') #req_string +="&keywordId={}".format('keywordId')
req_string +="&keywordCategory={}".format(parsed_body['Einsatzstichwort'].split("(")[0]) req_string +="&keywordCategory={}".format(parsed_body['Einsatzstichwort'].split("(")[0])
@@ -122,27 +121,30 @@ def alarminator_api(parsed_body: dict):
except Exception as e: except Exception as e:
logging.error("alarminator_api", e) logging.error("alarminator_api", e)
def generate_pdf(html_body, filename):
f = "/tmp/{}.html".format(uuid.uuid4())
with open(f,"w") as _f:
_f.write(html_body)
subprocess.run(["/usr/bin/google-chrome-stable", "--headless", "--no-sandbox", "--disable-gpu", "--print-to-pdf="+filename, "--no-pdf-header-footer", "--print-to-pdf-no-header", "--no-margins", f])
if os.path.exists(f):
os.remove(f)
def cups_print(parsed_body: dict, body: str): def cups_print(parsed_body: dict, body: str):
html = HTML(string='<html><head></head><body>'+body+"</body></html>", base_url="")
font_config = FontConfiguration()
pdf = html.write_pdf(font_config=font_config,stylesheets=[CSS(string='body { font-family: serif !important }')])
fname = "/tmp/{}.pdf".format(uuid.uuid4())
if os.environ.get('IS_DEV') and os.environ.get('IS_DEV') == "True": if os.environ.get('IS_DEV') and os.environ.get('IS_DEV') == "True":
with open("{}.pdf".format(uuid.uuid4()),"wb") as f: generate_pdf(body, "{}.pdf".format(uuid.uuid4()))
f.write(pdf) fname = "/tmp/{}.pdf".format(uuid.uuid4())
try: try:
conn = cups.Connection () conn = cups.Connection ()
printer_arr = os.environ.get('printer',"DEFAULT").split(";") printer_arr = os.environ.get('printer',"DEFAULT").split(";")
print_num = int(os.environ.get('print_num',0)) print_num = int(os.environ.get('print_num',0))
if printer_arr.__len__() > 0:
generate_pdf(body, fname)
for printer in printer_arr: for printer in printer_arr:
if 'ALARMDEPESCHE' in parsed_body: if 'ALARMDEPESCHE' in parsed_body:
with open(fname,"wb") as f:
f.write(pdf)
for i in range(0, print_num): for i in range(0, print_num):
conn.printFile (printer, fname, "Alarmfax", {}) conn.printFile (printer, fname, "Alarmfax", {})
os.remove(fname)
except Exception as e: except Exception as e:
logging.error("cups_print", e)
finally:
if os.path.exists(fname): if os.path.exists(fname):
os.remove(fname) os.remove(fname)
logging.error("cups_print", e)
+1
View File
@@ -24,6 +24,7 @@ services:
- printer=${printer} - printer=${printer}
- print_num=${print_num} - print_num=${print_num}
- MAPS_API_KEY=${MAPS_API_KEY} - MAPS_API_KEY=${MAPS_API_KEY}
- BASIC_AUTH_PASSWORD=${BASIC_AUTH_PASSWORD}
volumes: volumes:
- ./cups:/etc/cups - ./cups:/etc/cups
- ./cupsd.conf.txt:/etc/cups/cupsd.conf - ./cupsd.conf.txt:/etc/cups/cupsd.conf
+1 -1
View File
@@ -11,6 +11,7 @@ defusedxml==0.7.1
dnspython==2.3.0 dnspython==2.3.0
exchangelib==4.9.0 exchangelib==4.9.0
Flask==3.0.3 Flask==3.0.3
Flask-BasicAuth==0.2.0
fonttools==4.54.1 fonttools==4.54.1
html5lib==1.1 html5lib==1.1
idna==3.4 idna==3.4
@@ -40,7 +41,6 @@ tinycss2==1.3.0
tzdata==2022.7 tzdata==2022.7
tzlocal==4.2 tzlocal==4.2
urllib3==1.26.14 urllib3==1.26.14
weasyprint==62.3
webencodings==0.5.1 webencodings==0.5.1
Werkzeug==3.0.4 Werkzeug==3.0.4
xmltodict==0.12.0 xmltodict==0.12.0