forked from ZhangChengX/PDFBoT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
176 lines (135 loc) · 5.22 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from flask import Flask, flash, request, redirect, render_template
import config
import os
import wget
import subprocess
from extractTextFromHTML import getTextFromHTML
from extractTextFrom2colHTML import getTextFrom2HTML
import pikepdf
from werkzeug.utils import secure_filename
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
app = Flask(__name__,template_folder='templates',static_url_path='/static')
app.secret_key = config.secret_key
app.config['UPLOAD_FOLDER'] = config.UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = config.MAX_CONTENT_LENGTH
@app.route('/pdfbot')
def pdfbotEX():
return render_template('upload.html')
@app.route('/')
def upload_form():
return render_template('upload.html')
@app.route('/upload_file', methods=['POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No file selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
print('file name = '+str(filename))
abPath = os.path.join(os.getcwd(),app.config['UPLOAD_FOLDER'])
file.save(os.path.join(abPath, filename))
flash('File successfully uploaded')
redirectURL = '/pdftohtml/'+str(os.path.join(app.config['UPLOAD_FOLDER'], filename))
htmlFilePath = pdftohtml_test(str(os.path.join(app.config['UPLOAD_FOLDER'], filename)))
text = getTextFrom2HTML(htmlFilePath)
print('redirect url = '+str(redirectURL))
print('text == '+str(text))
return render_template('render_text.html', text=text)
# return redirect(redirectURL)
else:
flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif')
return redirect(request.url)
@app.route('/pdftohtml_test/<path:subpath>')
def pdftohtml_test(subpath):
# for test
filePath = subpath
# unlock pdf
pdf=pikepdf.open(filePath)
unlockedFile = filePath.replace(' ','_').replace('-','_').replace('.pdf','_unlocked.pdf')
pdf.save(unlockedFile)
filePath = unlockedFile
fileName = os.path.basename(filePath)
print('file name = '+str(fileName))
if not os.path.exists('/' + filePath.replace('.pdf', '')):
commandtemp = 'mkdir ' + filePath.replace('.pdf', '')
subprocess.call(commandtemp, shell=True)
command = 'pdf2htmlEX --zoom 1.3 --embed fi ' + filePath + ' --dest-dir ' + filePath.replace(
'.pdf', '')
print('command 002 = ' + str(command))
process = subprocess.call(command, shell=True)
htmlFilePath = filePath.replace('.pdf', '')+"/"+fileName.replace('.pdf', '.html')
print('html file path = ' + str(htmlFilePath))
return htmlFilePath
@app.route('/downloadPDFtest/<path:subpath>')
def downloadPDF(subpath):
testURL = 'https://www.aclweb.org/anthology/P15-1109.pdf'
fileName = os.path.basename(subpath)
downloadedPDFpath = 'output/tempOutput/'+str(fileName).replace('(','_').replace(')','_')
inputfilePath = subpath
currDir = os.getcwd()
outputfile = str(currDir)+'/'+downloadedPDFpath
wget.download(subpath,outputfile)
return downloadedPDFpath
@app.route('/convertPDFtoHTML/<path:subpath>')
def convertPDFtoHTML(subpath):
filePath = subpath
# download the pdf file and clean the file name
filePath = downloadPDF(subpath)
print('file path pikepdf = ' + str(filePath))
# unlock pdf
pdf=pikepdf.open(filePath)
unlockedFile = filePath.replace(' ','_').replace('-','_').replace('.pdf','_unlocked.pdf')
pdf.save(unlockedFile)
filePath = unlockedFile
print('file path 02 = ' + str(filePath))
fileName = os.path.basename(filePath)
print('file name = '+str(fileName))
if not os.path.exists('/' + filePath.replace('.pdf', '')):
commandtemp = 'mkdir ' + filePath.replace('.pdf', '')
subprocess.call(commandtemp, shell=True)
command = 'pdf2htmlEX --zoom 1.3 --embed fi ' + filePath + ' --dest-dir ' + filePath.replace(
'.pdf', '')
process = subprocess.call(command, shell=True)
htmlFilePath = filePath.replace('.pdf', '')+"/"+fileName.replace('.pdf', '.html')
print('html file path = ' + str(htmlFilePath))
return htmlFilePath
@app.route('/pdf2htmlEX/<path:subpath>')
# @app.route('/<path:subpath>')
def getContext(subpath):
print('subpath = '+str(subpath))
# convert pdf to html and return the path of html file
htmlFilePath = convertPDFtoHTML(subpath)
return htmlFilePath
@app.route('/pdftotext/<path:subpath>')
def pdftotext(subpath):
print('subpath = '+str(subpath))
htmlFilePath = convertPDFtoHTML(subpath)
text = getTextFromHTML(htmlFilePath)
string = ''
for key in text:
string = string+'\n\n'+text[key]
return string.replace(' ',' ')
# auto = "single", "double", "auto"
@app.route('/pdftotextAuto/<auto>/<path:subpath>')
def pdftotextAuto(auto, subpath):
print('subpath = ' + str(subpath))
# subpath= 'testPDF/single/01.pdf'
# subpath = 'testPDF/double/01.pdf'
htmlFilePath = convertPDFtoHTML(subpath)
text = getTextFrom2HTML(htmlFilePath, auto=auto)
print('text 00 = '+str(text))
string = ''
for txt in text:
print('txt = '+str(txt))
string = string + '\n\n' + txt
return string.replace(' ', ' ')
if __name__ == '__main__':
app.run()