-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path课件打印服务器.py
66 lines (54 loc) · 1.66 KB
/
课件打印服务器.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
from flask import Flask, request
import json
import re
import requests
import os
import time
from PIL import Image
import tqdm
import shutil
import logging
app = Flask(__name__)
log = logging.getLogger('werkzeug')
log.disabled = True
def outpdf(title, urls):
'''下载图片合并为pdf'''
os.makedirs('tmp/'+title)
PathList = []
print('开始下载图片')
for index, url in tqdm.tqdm(enumerate(urls)): # 图片下载
path = 'tmp/' + title + '/' + str(index)+'.png'
r = requests.get(url, stream=True)
PathList.append(path)
open(path, 'wb').write(r.content)
file = Image.open(PathList[0])
ImageList = []
print('正在转换图片')
for index in range(1, len(PathList)): # 图片读取
image = Image.open(PathList[index])
if image.mode in ('RGBA', 'LA') or (image.mode == 'P' and 'transparency' in image.info):
image = image.convert('RGB')
ImageList.append(image)
# pdf合成
print('开始合并为pdf')
file.save(title+'.pdf', 'PDF', resolution=100.0,
save_all=True, append_images=ImageList)
print('完成pdf导出')
shutil.rmtree("tmp") # 执行完成删除缓存
@app.route('/print', methods=['post'])
def index():
data = json.loads(request.get_data())
try:
outpdf(data['title'], data['link'])
return json.dumps({'status': 'ok'})
except:
shutil.rmtree("tmp")
return json.dumps({'status': 'err'})
@app.route('/', methods=['get'])
def pulse():
# 服务器心跳检测
return json.dumps({'status': 'ok'})
if __name__ == '__main__':
app.run(
host='0.0.0.0',
port=63321)