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
|
||||
EXPOSE 631
|
||||
EXPOSE 5000
|
||||
# Add user and disable sudo password checking
|
||||
RUN useradd \
|
||||
--groups=sudo,lp,lpadmin \
|
||||
@ -56,11 +57,29 @@ RUN useradd \
|
||||
print \
|
||||
&& 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 ./
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
RUN pip install --no-cache-dir waitress
|
||||
|
||||
COPY ./app .
|
||||
COPY *.crt /usr/local/share/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>'
|
||||
return html
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(port=5000)
|
||||
# if __name__ == "__main__":
|
||||
# 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 requests
|
||||
import cups
|
||||
from weasyprint import HTML, CSS
|
||||
from weasyprint.text.fonts import FontConfiguration
|
||||
from requests.adapters import Retry
|
||||
import uuid
|
||||
import subprocess
|
||||
|
||||
retries = Retry(total=5,
|
||||
backoff_factor=0.1,
|
||||
@ -122,23 +121,26 @@ def alarminator_api(parsed_body: dict):
|
||||
except Exception as e:
|
||||
logging.error("alarminator_api", e)
|
||||
|
||||
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())
|
||||
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):
|
||||
if os.environ.get('IS_DEV') and os.environ.get('IS_DEV') == "True":
|
||||
with open("{}.pdf".format(uuid.uuid4()),"wb") as f:
|
||||
f.write(pdf)
|
||||
generate_pdf(body, "{}.pdf".format(uuid.uuid4()))
|
||||
fname = "/tmp/{}.pdf".format(uuid.uuid4())
|
||||
try:
|
||||
conn = cups.Connection ()
|
||||
printer_arr = os.environ.get('printer',"DEFAULT").split(";")
|
||||
print_num = int(os.environ.get('print_num',0))
|
||||
if printer_arr.__len__() > 0:
|
||||
generate_pdf(body, fname)
|
||||
for printer in printer_arr:
|
||||
if 'ALARMDEPESCHE' in parsed_body:
|
||||
with open(fname,"wb") as f:
|
||||
f.write(pdf)
|
||||
for i in range(0, print_num):
|
||||
conn.printFile (printer, fname, "Alarmfax", {})
|
||||
os.remove(fname)
|
||||
|
@ -24,6 +24,7 @@ services:
|
||||
- printer=${printer}
|
||||
- print_num=${print_num}
|
||||
- MAPS_API_KEY=${MAPS_API_KEY}
|
||||
- BASIC_AUTH_PASSWORD=${BASIC_AUTH_PASSWORD}
|
||||
volumes:
|
||||
- ./cups:/etc/cups
|
||||
- ./cupsd.conf.txt:/etc/cups/cupsd.conf
|
||||
|
@ -41,7 +41,6 @@ tinycss2==1.3.0
|
||||
tzdata==2022.7
|
||||
tzlocal==4.2
|
||||
urllib3==1.26.14
|
||||
weasyprint==62.3
|
||||
webencodings==0.5.1
|
||||
Werkzeug==3.0.4
|
||||
xmltodict==0.12.0
|
||||
|
Loading…
x
Reference in New Issue
Block a user