first commit for refactored project
This commit is contained in:
37
ax3Services/docConvertService/archive_pyServer.py
Normal file
37
ax3Services/docConvertService/archive_pyServer.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
import time
|
||||
from splitPdf import SplitPdf
|
||||
import logging
|
||||
|
||||
hostName = "localhost"
|
||||
serverPort = 5050
|
||||
sp = SplitPdf()
|
||||
|
||||
class MyServer(BaseHTTPRequestHandler):
|
||||
|
||||
def do_GET(self):
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
|
||||
def do_POST(self):
|
||||
print('post hit')
|
||||
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
|
||||
print('content_length', content_length)
|
||||
post_data = self.rfile.read(content_length) # <--- Gets the data itself
|
||||
print('post_data', post_data)
|
||||
logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
|
||||
str(self.path), str(self.headers), post_data.decode('utf-8'))
|
||||
self.wfile.write("POST request for {}".format(self.path).encode('utf-8'))
|
||||
|
||||
if __name__ == "__main__":
|
||||
webServer = HTTPServer((hostName, serverPort), MyServer)
|
||||
print("Server started http://%s:%s" % (hostName, serverPort))
|
||||
|
||||
try:
|
||||
webServer.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
webServer.server_close()
|
||||
print("Server stopped.")
|
||||
44
ax3Services/docConvertService/pyserv.py
Normal file
44
ax3Services/docConvertService/pyserv.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import bottle
|
||||
from bottle import run, get, post, request, route
|
||||
from bottle import HTTPResponse
|
||||
from splitPdf import SplitPdf
|
||||
from pathlib import Path
|
||||
import json
|
||||
|
||||
def fix_environ_middleware(app):
|
||||
def fixed_app(environ, start_response):
|
||||
environ['wsgi.url_scheme'] = 'https'
|
||||
environ['HTTP_X_FORWARDED_HOST'] = 'novodraft.com'
|
||||
return app(environ, start_response)
|
||||
return fixed_app
|
||||
|
||||
app = bottle.default_app()
|
||||
app.wsgi = fix_environ_middleware(app.wsgi)
|
||||
sp = SplitPdf()
|
||||
suc = {
|
||||
'ok': True
|
||||
}
|
||||
|
||||
@post('/parse-new-complaint/<id>')
|
||||
def newdoc(id='test'):
|
||||
newDir = f"{id}" # path[-1].split(".")[0]
|
||||
print('newDir', newDir)
|
||||
sp.make_dir(newDir)
|
||||
path_arg = f"../Documents/Complaints/{id}.pdf"
|
||||
print(f"../Documents/Complaints/{id}.pdf")
|
||||
sp.split_and_convert(path_arg, newDir)
|
||||
respBody = json.dumps({'Status': 'Success'})
|
||||
return bottle.HTTPResponse(status=200, body=respBody)
|
||||
|
||||
@post('/parse-new-disc-req/<id>')
|
||||
def newdoc(id='test'):
|
||||
newDir = f"{id}"
|
||||
print('newDir', newDir)
|
||||
sp.make_dir(newDir)
|
||||
path_arg = f"../Documents/Uploads/{id}.pdf"
|
||||
print("path_arg", path_arg)
|
||||
sp.split_and_convert(path_arg, newDir)
|
||||
respBody = json.dumps({'Status': 'Success'})
|
||||
return bottle.HTTPResponse(status=200, body=respBody)
|
||||
|
||||
run(app, host='127.0.0.1', port=8081, debug=True)
|
||||
49
ax3Services/docConvertService/splitPdf.js
Normal file
49
ax3Services/docConvertService/splitPdf.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import pdf2image from "pdf2image";
|
||||
|
||||
//The converter uses the same options as the convertPDF function
|
||||
var converter = pdf2image.compileConverter({
|
||||
density: 200,
|
||||
quality: 100,
|
||||
outputFormat: "%s_page_%d",
|
||||
outputType: "png",
|
||||
});
|
||||
|
||||
//Converts a single pdf
|
||||
converter.convertPDF("badRogs.pdf");
|
||||
|
||||
//Converts multiple pdfs
|
||||
//converter.convertPDFList(["example1.pdf", "example1.pdf"]);
|
||||
|
||||
/*
|
||||
const { fromPath } = require("pdf2pic");
|
||||
const pdf = require("pdf-page-counter");
|
||||
const fs = require("fs");
|
||||
|
||||
const split = () => {
|
||||
const { filename } = "./badRogs.pdf";
|
||||
const options = {
|
||||
density: 100,
|
||||
saveFilename: "file",
|
||||
savePath: "./converted",
|
||||
format: "png",
|
||||
width: 600,
|
||||
height: 600,
|
||||
};
|
||||
const storeAsImage = fromPath(`./converted/${filename}`, options);
|
||||
let dataBuffer = fs.readFileSync(`./converted/${filename}`);
|
||||
pdf(dataBuffer).then(function (data) {
|
||||
for (
|
||||
var pageToConvertAsImage = 1;
|
||||
pageToConvertAsImage <= data.numpages;
|
||||
pageToConvertAsImage++
|
||||
) {
|
||||
storeAsImage(pageToConvertAsImage).then((resolve) => {
|
||||
return resolve;
|
||||
});
|
||||
}
|
||||
res.send({
|
||||
filename: filename,
|
||||
});
|
||||
});
|
||||
};
|
||||
*/
|
||||
136
ax3Services/docConvertService/splitPdf.py
Normal file
136
ax3Services/docConvertService/splitPdf.py
Normal file
@@ -0,0 +1,136 @@
|
||||
import os
|
||||
import uuid
|
||||
from pdf2image import convert_from_path
|
||||
from pdf2image.exceptions import (
|
||||
PDFInfoNotInstalledError,
|
||||
PDFPageCountError,
|
||||
PDFSyntaxError,
|
||||
)
|
||||
|
||||
|
||||
class SplitPdf(object):
|
||||
def make_dir(self, newDir):
|
||||
print("newDir in module", newDir)
|
||||
self.directory = newDir
|
||||
self.parent_dir = "../Documents/Converted/"
|
||||
self.path = os.path.join(self.parent_dir, self.directory)
|
||||
os.mkdir(self.path)
|
||||
|
||||
def split_and_convert(self, pathArg, newDir):
|
||||
print("in split and convert: pathArg, newDir", pathArg, newDir)
|
||||
arr = [
|
||||
"a",
|
||||
"b",
|
||||
"c",
|
||||
"d",
|
||||
"e",
|
||||
"f",
|
||||
"g",
|
||||
"h",
|
||||
"i",
|
||||
"j",
|
||||
"k",
|
||||
"l",
|
||||
"m",
|
||||
"n",
|
||||
"o",
|
||||
"p",
|
||||
"q",
|
||||
"r",
|
||||
"s",
|
||||
"t",
|
||||
"u",
|
||||
"v",
|
||||
"w",
|
||||
"x",
|
||||
"y",
|
||||
"z",
|
||||
"aa",
|
||||
"ab",
|
||||
"ac",
|
||||
"ad",
|
||||
"ae",
|
||||
"af",
|
||||
"ag",
|
||||
"ah",
|
||||
"ai",
|
||||
"ak",
|
||||
"al",
|
||||
"am",
|
||||
"an",
|
||||
"ao",
|
||||
"ap",
|
||||
"aq",
|
||||
"ar",
|
||||
"as",
|
||||
"at",
|
||||
"au",
|
||||
"av",
|
||||
"aw",
|
||||
"ax",
|
||||
"ay",
|
||||
"az",
|
||||
"ba",
|
||||
"bb",
|
||||
"bc",
|
||||
"bd",
|
||||
"be",
|
||||
"bf",
|
||||
"bg",
|
||||
"bh",
|
||||
"bi",
|
||||
"bk",
|
||||
"bl",
|
||||
"bm",
|
||||
"bn",
|
||||
"bo",
|
||||
"bp",
|
||||
"bq",
|
||||
"br",
|
||||
"bs",
|
||||
"bt",
|
||||
"bu",
|
||||
"bv",
|
||||
"bw",
|
||||
"bx",
|
||||
"by",
|
||||
"bz",
|
||||
"za",
|
||||
"zb",
|
||||
"zc",
|
||||
"zd",
|
||||
"ze",
|
||||
"zf",
|
||||
"zg",
|
||||
"zh",
|
||||
"zi",
|
||||
"zj",
|
||||
"zk",
|
||||
"zl",
|
||||
"zm",
|
||||
"zn",
|
||||
"zo",
|
||||
"zp",
|
||||
"zq",
|
||||
"zr",
|
||||
"zs",
|
||||
"zt",
|
||||
"zu",
|
||||
"zv",
|
||||
"zw",
|
||||
"zx",
|
||||
"zy",
|
||||
"zz",
|
||||
]
|
||||
self.images = convert_from_path(pathArg, fmt="png")
|
||||
print(f"../Documents/Converted/{self.directory}")
|
||||
for i, image in enumerate(self.images):
|
||||
fname = f"{newDir}" + arr[i] + ".png"
|
||||
image.save(f"../Documents/Converted/{self.directory}/{fname}", "PNG")
|
||||
|
||||
|
||||
# split pdf to multiple png files
|
||||
# https://pypi.org/project/pdf2image/
|
||||
# https://pythonforundergradengineers.com/pdf-to-multiple-images.html
|
||||
# convert to js(?):
|
||||
# https://www.npmjs.com/package/pdf2image
|
||||
41
ax3Services/docConvertService/watcher.py
Normal file
41
ax3Services/docConvertService/watcher.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import sys
|
||||
import time
|
||||
import logging
|
||||
from splitPdf import SplitPdf
|
||||
from watchdog.observers import Observer
|
||||
from pathlib import Path
|
||||
|
||||
# from watchdog.events import LoggingEventHandler
|
||||
from watchdog.events import PatternMatchingEventHandler
|
||||
|
||||
watch_dir = f"{Path(__file__).parents[1]}/Documents/Uploads"
|
||||
|
||||
if __name__ == "__main__":
|
||||
sp = SplitPdf()
|
||||
|
||||
def on_created(event):
|
||||
print(f"Event: {event.src_path} was created.")
|
||||
path = str(event.src_path).split("/")
|
||||
print(">>>>>>>>>>>>>>>path", path[-1].split(".")[0])
|
||||
newDir = path[-1].split(".")[0]
|
||||
sp.make_dir(newDir)
|
||||
sp.split_and_convert(event.src_path, newDir)
|
||||
|
||||
patterns = ["*"]
|
||||
ignore_patterns = None
|
||||
ignore_directories = False
|
||||
case_sensitive = True
|
||||
path = watch_dir
|
||||
my_event_handler = PatternMatchingEventHandler(
|
||||
patterns, ignore_patterns, ignore_directories, case_sensitive
|
||||
)
|
||||
my_event_handler.on_created = on_created
|
||||
observer = Observer()
|
||||
observer.schedule(my_event_handler, path, recursive=True)
|
||||
observer.start()
|
||||
try:
|
||||
while True:
|
||||
time.sleep(1)
|
||||
finally:
|
||||
observer.stop()
|
||||
observer.join()
|
||||
Reference in New Issue
Block a user