-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
47 lines (41 loc) · 1.49 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
import base64
import pdfkit
import os
from flask import Flask, request, jsonify
from flask_httpauth import HTTPBasicAuth
from dotenv import load_dotenv
app = Flask(__name__)
auth = HTTPBasicAuth()
load_dotenv() # load environment variables from .env file
@app.route('/', methods=['POST'])
@auth.login_required
def html_to_pdf():
content = request.get_json()
if content is None:
return jsonify({"error": "No JSON data provided in request"}), 400
html = content.get('html')
title = content.get('title', 'Default Title')
if html is None:
return jsonify({"error": "No 'html' key found in provided JSON data"}), 400
pdf = pdfkit.from_string(html, False, options={'title': title})
return jsonify({'pdf': base64.b64encode(pdf).decode('utf-8')})
@app.route('/about', methods=['GET'])
@app.route('/version', methods=['GET'])
def about():
return {
'service': 'HTML2PDF Microservice',
'source':'https://github.com/mctlisboa/html2pdf',
'version': '0.1.0',
'description': 'A microservice that converts HTML to PDF, built using Flask, pdfkit/wkhtmltopdf.',
'creator': {
'name': 'Marcos Lisboa',
'github': 'https://github.com/mctlisboa/',
},
}
@auth.verify_password
def verify_password(username, password):
if username == os.getenv('BASIC_AUTH_USERNAME') and password == os.getenv('BASIC_AUTH_PASSWORD'):
return True
return False
if __name__ == '__main__':
app.run(host='0.0.0.0', port=4000)