-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
68 lines (54 loc) · 1.99 KB
/
app.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
from fpdf import FPDF
from datetime import datetime
import os
from PIL import Image
from PyPDF2 import PdfFileReader, PdfFileWriter
import img2pdf
from flask import Flask, request
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
def merge_pdfs(paths, output):
pdf_writer = PdfFileWriter()
for path in paths:
pdf_reader = PdfFileReader(path)
for page in range(pdf_reader.getNumPages()):
# Add each page to the writer object
pdf_writer.addPage(pdf_reader.getPage(page))
# Write out the merged PDF
with open(output, 'wb') as out:
pdf_writer.write(out)
class newEmail(Resource):
def post(self):
data = request.form['name']
image_files = request.files.getlist("file[]")
pdf_filer = []
try:
pdf = FPDF()
pdf.compress = False
pdf.add_page()
pdf.set_font('Arial', '', 14)
pdf.ln(10)
pdf.write(5, data)
pdf.output('header.pdf', 'F')
print('Liste: ')
print(pdf_filer)
pdf_filer.append('header.pdf')
for img in image_files:
kind = os.path.splitext(img.filename)[1]
if (kind == '.pdf'):
img.save(img.filename)
pdf_filer.append(img.filename)
else:
filename = img.filename+'.pdf'
pdf_file = open(filename,'wb')
pdf_file.write(img2pdf.convert(img))
pdf_filer.append(img.filename+'.pdf')
pdf_file.close()
merge_pdfs(pdf_filer, output='result/'+ str(datetime.today().strftime('%Y-%m-%d-%H%M%S')) + '_bilag.pdf')
return {'message': 'Success'}
except:
return {'message': 'Error'}
api.add_resource(newEmail, '/')
if __name__ == '__main__':
app.run(threaded=True, port=5000)