print pdf with chrome
This commit is contained in:
parent
b82d1bed1f
commit
47c1c5ceb8
21
Dockerfile
21
Dockerfile
@ -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" ]
|
||||||
|
@ -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')
|
||||||
|
24
app/hooks.py
24
app/hooks.py
@ -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,
|
||||||
@ -122,23 +121,26 @@ 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)
|
os.remove(fname)
|
||||||
|
@ -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
|
||||||
|
@ -41,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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user