commit de8669c45b30ccf1e20b10087ca625f7ce6e8590 Author: William Grando Date: Thu May 29 19:03:16 2025 -0300 init do projeto. diff --git a/cups/Dockerfile.dockerfile b/cups/Dockerfile.dockerfile new file mode 100644 index 0000000..a8f58c4 --- /dev/null +++ b/cups/Dockerfile.dockerfile @@ -0,0 +1,74 @@ +FROM debian:bookworm-slim + +ENV DEBIAN_FRONTEND=noninteractive +ENV CUPS_VERSION=2.4.12 +ENV TZ "America/Sao_Paulo" +ENV USERNAME admin +ENV PASSWORD password + +# 1) Instala dependências de build + runtime, incluindo CA para HTTPS +RUN apt-get update && apt-get install -y --no-install-recommends \ + ca-certificates \ + git \ + build-essential autoconf automake libtool pkg-config \ + libssl-dev libgnutls28-dev \ + zlib1g zlib1g-dev \ + libavahi-client-dev \ + printer-driver-all \ + printer-driver-cups-pdf \ + printer-driver-foo2zjs \ + foomatic-db-compressed-ppds \ + openprinting-ppds \ + libreoffice \ + libreoffice-common \ + libnss-mdns \ + hpijs-ppds \ + file \ + hp-ppd \ + hplip \ + dos2unix \ + cups cups-client cups-filters cups-browsed \ + curl avahi-daemon dbus \ + iputils-ping \ + vim \ + && apt-get clean && rm -rf /var/lib/apt/lists/* + +RUN cd /opt && \ + wget https://github.com/OpenPrinting/cups/releases/download/v${CUPS_VERSION}/cups-${CUPS_VERSION}-source.tar.gz && \ + tar -xzf cups-${CUPS_VERSION}-source.tar.gz && \ + cd cups-${CUPS_VERSION} && \ + ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --with-rcdir=/etc/init.d && \ + make -j$(nproc) && \ + make install && \ + cd / && rm -rf /opt/* + +RUN sed -i 's/Listen localhost:631/Listen 0.0.0.0:631/' /etc/cups/cupsd.conf && \ + sed -i 's/Browsing Off/Browsing On/' /etc/cups/cupsd.conf && \ + sed -i 's//\n Allow All/' /etc/cups/cupsd.conf && \ + sed -i 's//\n Allow All\n Require user @SYSTEM/' /etc/cups/cupsd.conf && \ + sed -i 's//\n Allow All/' /etc/cups/cupsd.conf && \ + echo "ServerAlias *" >> /etc/cups/cupsd.conf && \ + echo "DefaultEncryption Never" >> /etc/cups/cupsd.conf + +RUN echo 'application/vnd.openxmlformats-officedocument.wordprocessingml.document docx' >> /etc/cups/raw.types && \ + echo 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx' >> /etc/cups/raw.types + +RUN echo 'application/vnd.openxmlformats-officedocument.wordprocessingml.document application/pdf 100 doc2pdf' >> /etc/cups/raw.convs && \ + echo 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/pdf 100 doc2pdf' >> /etc/cups/raw.convs + +RUN cp -a /etc/cups /etc/cups.default + +# 4) Volumes para persistência de config, spool e logs +VOLUME ["/etc/cups", "/var/spool/cups", "/var/log/cups"] + +# 5) Expõe a porta da interface web/admin do CUPS +EXPOSE 631 5353/udp + +COPY doc2pdf.sh /usr/lib/cups/filter/doc2pdf +RUN dos2unix /usr/lib/cups/filter/doc2pdf \ + && chmod 755 /usr/lib/cups/filter/doc2pdf + +COPY entrypoint.sh / +RUN chmod +x /entrypoint.sh + +CMD ["/entrypoint.sh"] \ No newline at end of file diff --git a/cups/README.md b/cups/README.md new file mode 100644 index 0000000..87f53af --- /dev/null +++ b/cups/README.md @@ -0,0 +1,34 @@ +# Cups +### 📌 Sobre o Projeto +Uma compilação simples do cups com algumas alterações que para o meu contexto eram importantes, tais como: +1. Versão mais atualizada +2. Filtro para leitura de arquivos docx/xlsx +3. Suporte a Nomes para instalar impressoras ex: imp.escritorio +4. Pacote de drivers mais completo +--- + +### 🚀 Como Usar + +#### Via docker compose (Meu preferido.) + +```yaml +--- +services: + cups: + image: grandow/cups:2.4.12-office + container_name: cups + restart: unless-stopped + ulimits: + nofile: + soft: "65536" + hard: "65536" + ports: + - "631:631" + - "5353:5353/udp" + environment: + - USERNAME=admin + - PASSWORD=cups + - TZ="America/Sao_Paulo" + volumes: + - "./cups/:/etc/cups/" +``` \ No newline at end of file diff --git a/cups/doc2pdf.sh b/cups/doc2pdf.sh new file mode 100644 index 0000000..e85c4a7 --- /dev/null +++ b/cups/doc2pdf.sh @@ -0,0 +1,58 @@ +#!/bin/bash +# /usr/lib/cups/filter/doc2pdf +set -e + +JOB_ID="$1" +USER="$2" +TITLE="$3" +COPIES="$4" +OPTIONS="$5" +FILE="$6" + +# Função para extrair extensão a partir do document-format (se existir) +ext_from_options() { + echo "$OPTIONS" \ + | sed -n 's/.*document-format=\([^ ,]*\).*/\1/p' \ + | awk -F/ '{print $2}' \ + | sed -e 's/+xml//g' -e 's/xlsx/spreadsheetml.sheet/' \ + -e 's/wordprocessingml.document/docx/' \ + -e 's/spreadsheetml.sheet/xlsx/' +} + +# Se o CUPS passou o arquivo via stdin, ou se FILE não termina em .docx/.xlsx +if [ "$FILE" = "-" ] || ! echo "$FILE" | grep -E '\.(docx|xlsx)$' >/dev/null; then + # detecta extensão: primeiro tenta pelas OPTIONS + EXT=$(ext_from_options) + # se ainda vazio, usa file --mime-type + if [ -z "$EXT" ]; then + MIME=$(file --mime-type -b "$FILE") + case "$MIME" in + application/vnd.openxmlformats-officedocument.wordprocessingml.document) EXT=docx ;; + application/vnd.openxmlformats-officedocument.spreadsheetml.sheet) EXT=xlsx ;; + *) EXT="" ;; + esac + fi + + if [ -z "$EXT" ]; then + echo "Não consegui determinar extensão para conversão (mime: $MIME, options: $OPTIONS)" >&2 + exit 1 + fi + + TMPFILE="/tmp/job-${JOB_ID}.$EXT" + cat "${FILE:-/dev/stdin}" > "$TMPFILE" + FILE="$TMPFILE" +fi + +# Por fim, converte DOCX/XLSX válidos +case "${FILE##*.}" in + docx|xlsx) + libreoffice --headless --convert-to pdf:"writer_pdf_Export" \ + --outdir /tmp "$FILE" + PDF="/tmp/$(basename "${FILE%.*}.pdf")" + cat "$PDF" + ;; + *) + echo "Formato não suportado: ${FILE##*.}" >&2 + exit 1 + ;; +esac \ No newline at end of file diff --git a/cups/docker-compose.yaml b/cups/docker-compose.yaml new file mode 100644 index 0000000..4a94f46 --- /dev/null +++ b/cups/docker-compose.yaml @@ -0,0 +1,18 @@ +--- +services: + cups: + image: grandow/cups:2.4.12-office + container_name: cups + restart: unless-stopped + ulimits: + nofile: + soft: "65536" + hard: "65536" + ports: + - "631:631" + environment: + - USERNAME=admin + - PASSWORD=cups + - TZ="America/Sao_Paulo" + volumes: + - "/srv/waprint/cups/:/etc/cups/" \ No newline at end of file diff --git a/cups/entrypoint.sh b/cups/entrypoint.sh new file mode 100644 index 0000000..97840e4 --- /dev/null +++ b/cups/entrypoint.sh @@ -0,0 +1,33 @@ +#!/bin/sh +if [ $(grep -ci $USERNAME /etc/shadow) -eq 0 ]; then + useradd -r -G lpadmin -M $USERNAME + + # add password + echo $USERNAME:$PASSWORD | chpasswd + + # add tzdata + ln -fs /usr/share/zoneinfo/$TZ /etc/localtime + dpkg-reconfigure --frontend noninteractive tzdata +fi + +# restore default cups config in case user does not have any +if [ ! -f /etc/cups/cupsd.conf ]; then + cp -p /etc/cups.default/cupsd.conf /etc/cups/ + cp -p /etc/cups.default/printers.conf /etc/cups/ + #cp -rpn /etc/cups.default/* /etc/cups/ +fi + +if [ -d /etc/cups.default/ppd ]; then + mkdir -p /etc/cups/ppd + cp -rp /etc/cups.default/ppd/* /etc/cups/ppd/ +fi + +# Inicia o tail dos logs em background +tail -F /var/log/cups/access_log /var/log/cups/error_log & + +# Inicia o CUPS em foreground +/usr/sbin/cupsd -f & +CUPSD_PID=$! + +# Espera o CUPS morrer ou o tail encerrar +wait $CUPSD_PID \ No newline at end of file