-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f0d626e
Showing
6 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.env | ||
test.pdf | ||
venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...') |