Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
matbgn committed May 16, 2021
0 parents commit f0d626e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
test.pdf
venv
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:3.6-slim-buster
RUN apt-get update && apt-get install -y sane-utils libsane-hpaio imagemagick tesseract-ocr-fra tesseract-ocr-deu pngquant nano
WORKDIR /usr/src/app
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
RUN sed -i 's/rights="none" pattern="PDF"/rights="read|write" pattern="PDF"/' /etc/ImageMagick-6/policy.xml
CMD ["python", "./main.py"]
9 changes: 9 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
= Auto-scan to automatically scan documents from HP, OCR files, optimize it and finally send it by email as attachment
:icons: font
ifdef::env-github[]
:tip-caption: :bulb:
:note-caption: :information_source:
:important-caption: :heavy_exclamation_mark:
:caution-caption: :fire:
:warning-caption: :warning:
endif::[]
12 changes: 12 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import subprocess
import datetime
from send_email import *

ts_now = '{:%Y-%m-%d_%H%M%S}'.format(datetime.datetime.now())
file_with_ts = 'attachment_' + ts_now + '.pdf'

subprocess.run(["scan-pdf/src/scan-pdf", "--flatbed", "--color-mode", "color", file_with_ts])

subprocess.run(["ocrmypdf", "-r", "--rotate-pages-threshold", "6", "-O", "3", file_with_ts, file_with_ts])

send_email(file_with_ts, ts_now)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ocrmypdf>=12.0.1<12.1.0
python-dotenv>=0.17.1<0.18.0
49 changes: 49 additions & 0 deletions send_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import os
from dotenv import load_dotenv


def send_email(filename: str, ts: str) -> str:
load_dotenv()

gmail_user = os.environ['RPI_EMAIL']
gmail_password = os.environ['RPI_PASS']

sent_from = gmail_user

message = MIMEMultipart('mixed')
message['From'] = 'RPI Scanner <{sender}>'.format(sender = sent_from)
message['To'] = os.environ['EMAIL_RECIPIENTS']
message['CC'] = ''
message['Subject'] = 'Scan ' + ts

msg_content = '<h4>Hi There,<br> This is an automatic HP scanner message.</h4>\n'
body = MIMEText(msg_content, 'html')
message.attach(body)

attachmentPath = "./" + filename

try:
with open(attachmentPath, "rb") as attachment:
p = MIMEApplication(attachment.read(),_subtype="pdf")
p.add_header('Content-Disposition', "attachment; filename= %s" % attachmentPath.split("/")[-1])
message.attach(p)
except Exception as e:
print(str(e))

msg_full = message.as_string()

try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.sendmail(sent_from,
message['To'].split(";") + (message['CC'].split(";") if message['CC'] else []),
msg_full)
server.close()
print('Email sent!')
except:
print('Something went wrong...')

0 comments on commit f0d626e

Please sign in to comment.